Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4e06855c5285d92c301b5f986c340b116a6bf8c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*******************************************************************************
 * Copyright (c) 2004, 2008 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.jsf.facesconfig.ui.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageDeclaration;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jst.jsf.common.ui.IFileFolderConstants;
import org.eclipse.jst.jsf.common.ui.internal.utils.JavaModelUtil;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PartInitException;

/**
 * This utility class defines typical operations on java class, such as check
 * public constructor, and check type is primitive or not, etc.
 * 
 * @author Jane Cantz, Xiao-guang Zhang
 */
public class JavaClassUtils {
	/**
	 * Determines if a string contains any illegal characters
	 * 
	 * @param text -
	 *            the string to test
	 * @return boolean - true if an illegal character is found, otherwise false
	 */
	public static boolean hasIllegalCharacters(String text) {
		if (text.length() == 0
				|| !Character.isJavaIdentifierStart(text.charAt(0))) {
			return true;
		}
		for (int i = 1; i < text.length(); i++) {
			if (!Character.isJavaIdentifierPart(text.charAt(i))) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Test for constructor.
	 * <p>
	 * This test determines if the class has any constructors
	 * </p>
	 * 
	 * @param methods -
	 *            the IMethod array to examine
	 * @return boolean - true, if method has no constructors
	 */
	public static boolean hasNoConstructor(IMethod[] methods) {
		for (int m = 0; m < methods.length; m++) {
			try {
				if (methods[m].isConstructor()) {
					return false;
				}
			} catch (JavaModelException e) {
                // suppress: this is possible; fall through
			}
		}
		return true;
	}

	/**
	 * check whether the method is public or not.
	 * 
	 * @param method
	 * @return true if method is public
	 */
	public static boolean isPublicMethod(IMethod method) {
		int accessFlags = 0;

		try {
			accessFlags = method.getFlags();
		} catch (JavaModelException e) {
			return false;
		}

		boolean isPublic = Flags.isPublic(accessFlags);
		if ((!Flags.isPrivate(accessFlags))
				&& (!Flags.isProtected(accessFlags))
				&& (!Flags.isPublic(accessFlags))) {
			// No specific modifier was set, so assume to be public
			isPublic = true;
		}
		if (!isPublic) {
			return false;
		}
		return true;
	}

	/**
	 * Test for constructor.
	 * <p>
	 * This test has the following conditions:
	 * </p>
	 * <ul>
	 * <li>method takes 0 arguments and the method name is the class name:
	 * <ul>
	 * <li>takes 0 arguments</li>
	 * <li>the method name is the class name</li>
	 * <li>the method returns void</code></li>
	 * </ul>
	 * </li>
	 * </ul>
	 * 
	 * @param methods -
	 *            the IMethod array to examine
	 * @return boolean - true, if method is a constructor
	 */
	public static boolean hasPublicConstructor(IMethod[] methods) {
		for (int m = 0; m < methods.length; m++) {
			try {
				if (methods[m].isConstructor() && isPublicMethod(methods[m])) {
					// Constructor must have the following 0 arguments
					String[] params = methods[m].getParameterTypes();
					if (params.length == 0) {
						// And must return a void
						String rtn = methods[m].getReturnType();
						if (rtn.equals(Signature.SIG_VOID)) {
							return true;
						}
						break;
					}
				}
			} catch (JavaModelException e) {
				// Nothing to do.
			}
		}
		return false;
	}

	/**
	 * Determines if a datatype is primitive type or part of java.lang or
	 * java.util package. If not, it is considered to be a bean reference
	 * 
	 * @param classType -
	 *            the parent class compilation unit
	 * @param signatureName -
	 *            the datatype of the property
	 * @return boolean - true, if the datatype is primitive or part of java.lang
	 *         or java.util package
	 */
	public static boolean isPrimitiveType(IType classType, String signatureName) {
		while (signatureName.startsWith("[")) { //$NON-NLS-1$
			signatureName = signatureName.substring(1);
		}
		int kind = Signature.getTypeSignatureKind(signatureName);
		if (kind == Signature.BASE_TYPE_SIGNATURE
				|| signatureName.equals(Signature.SIG_VOID)) {
			// These are true primitive types
			return true;
		}

		String qualifiedName = getQualifiedTypeNameInTypeHierarchy(classType,
				signatureName);

		if ((qualifiedName.startsWith("java.lang")) || (qualifiedName.startsWith("java.util"))) //$NON-NLS-1$ //$NON-NLS-2$
		{
			return true;
		}
		return false;
	}

	/**
	 * get the type from the input class name
	 * 
	 * @param project
	 * @param className
	 * @return - can be null.
	 */
	public static IType getType(IProject project, String className) {
		if (project == null) {
			return null;
		}
		IType cunit = null;
		if (className.length() > 0) {

			IJavaProject jProject = JavaCore.create(project);
			try {
				cunit = jProject.findType(className);
			} catch (JavaModelException e) {
                // suppress: fall-through and return null
			}
		}
		return cunit;
	}

	/**
	 * open the type in the editor.
	 * 
	 * @param type
	 * @return true if the type could opened in an editor
	 */
	public static boolean openType(IType type) {
		if (type == null || !type.exists()) {
			return false;
		}

		try {
			IEditorPart editorPart = JavaUI.openInEditor(type
					.getPrimaryElement());
			if (editorPart != null) {
				JavaUI.revealInEditor(editorPart, type.getPrimaryElement());
				return true;
			}
		} catch (PartInitException e) {
			// ignore this error.
		} catch (JavaModelException e) {
			// ignore this error.
		}
		return false;
	}

	/**
	 * get package name from java source file
	 * 
	 * @param javaFile
	 * @return - can be null.
	 */
	public static String getPackageName(IFile javaFile) {
		if (javaFile == null) {
			return null;
		}
		String ext = "." + javaFile.getFileExtension(); //$NON-NLS-1$
		// See if the file is a java file
		if (!ext.equalsIgnoreCase(IFileFolderConstants.EXT_JAVA)) {
			return null;
		}
		String packagename = new String();
		ICompilationUnit cunit = JavaCore.createCompilationUnitFrom(javaFile);
		try {
			IPackageDeclaration[] packages = cunit.getPackageDeclarations();
			if (packages.length == 0) {
				packagename = new String();
			} else {
				packagename = packages[0].getElementName();
			}
		} catch (JavaModelException jme) {
            // suppress: fall-through and return an empty string?? TODO:?
		}
		return packagename;
	}

	/**
	 * copy the array to the list.
	 * 
	 * @param methodList
	 * @param methods
	 */
	private static void copyToMethodList(List methodList, IMethod[] methods) {
		if (methods != null && methods.length > 0) {
			for (int i = 0; i < methods.length; i++) {
				if (!isDuplicateMethod(methodList, methods[i])) {
					methodList.add(methods[i]);
				}
			}
		}
	}

	/**
	 * check whether this method is duplicated or not in the existing method
	 * list.
	 * 
	 * @param methodList
	 * @param method
	 * @return
	 */
	private static boolean isDuplicateMethod(List methodList, IMethod method) {
		if (method == null || !method.exists()) {
			return false;
		}

		String[] paramTypes = method.getParameterTypes();
		String methodName = method.getElementName();

		for (Iterator iter = methodList.iterator(); iter.hasNext();) {
			IMethod existedMethod = (IMethod) iter.next();
			if (isSameMethodSignature(methodName, paramTypes, existedMethod)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Tests if a method equals to the given signature. Parameter types are only
	 * compared by the simple name, no resolving for the fully qualified type
	 * name is done. Constructors are only compared by parameters, not the name.
	 * 
	 * @param name
	 *            Name of the method
	 * @param paramTypes
	 *            The type signatures of the parameters e.g.
	 *            <code>{"QString;","I"}</code>
	 * @param curr 
	 * @return Returns <code>true</code> if the method has the given name and
	 *         parameter types and constructor state.
	 */
	public static boolean isSameMethodSignature(String name,
			String[] paramTypes, IMethod curr) {
		if (name.equals(curr.getElementName())) {
			String[] currParamTypes = curr.getParameterTypes();
			if (paramTypes.length == currParamTypes.length) {
				for (int i = 0; i < paramTypes.length; i++) {
					String t1 = Signature.getSimpleName(Signature
							.toString(paramTypes[i]));
					String t2 = Signature.getSimpleName(Signature
							.toString(currParamTypes[i]));
					if (!t1.equals(t2)) {
						return false;
					}
				}
				return true;
			}
		}
		return false;
	}

	/**
	 * get methods for the class Type including its super class
	 * 
	 * @param classType
	 * @return - can be null
	 * @throws JavaModelException
	 */
	public static IMethod[] getMethods(IType classType)
			throws JavaModelException {
		if (classType == null) {
			return null;
		}
		List methodList = new ArrayList();
		IMethod[] methods = classType.getMethods();
		copyToMethodList(methodList, methods);

		ITypeHierarchy typeHierarchy = classType.newSupertypeHierarchy(null);

		if (typeHierarchy != null) {
			IType[] superTypes = typeHierarchy.getAllSuperclasses(classType);

			if (superTypes != null && superTypes.length > 0) {
				for (int i = 0; i < superTypes.length; i++) {
					if (!superTypes[i].getFullyQualifiedName().equals(
							"java.lang.Object")) { //$NON-NLS-1$
						methods = superTypes[i].getMethods();

						copyToMethodList(methodList, methods);
					}
				}
			}
		}

		if (methodList != null && methodList.size() > 0) {
			IMethod[] validMethods = (IMethod[]) methodList
					.toArray(new IMethod[methodList.size()]);

			Arrays.sort(validMethods, new Comparator() {
				public int compare(Object o1, Object o2) {
					String name1 = ((IMethod) o1).getElementName();
					String name2 = ((IMethod) o2).getElementName();
					return name1.compareTo(name2);
				}
			});
			return validMethods;
		}
		return null;
	}

	/**
	 * resolve and get the qualified name for the incomplete typename
	 * 
	 * @param classType
	 * @param signatureName
	 * @return - at least equal to Signature.toString(signatureName).
	 */
	public static String getQualifiedTypeNameInTypeHierarchy(IType classType,
			String signatureName) {
		int arrayNum = 0;
		while (signatureName.startsWith("[")) { //$NON-NLS-1$
			arrayNum++;
			signatureName = signatureName.substring(1);
		}

		String qualifiedTypeName = Signature.toString(signatureName);
		int kind = Signature.getTypeSignatureKind(signatureName);
		if (kind == Signature.BASE_TYPE_SIGNATURE
				|| signatureName.equals(Signature.SIG_VOID)) {
			// Add back array identifiers
			while (arrayNum > 0) {
				qualifiedTypeName = qualifiedTypeName + "[]"; //$NON-NLS-1$
				arrayNum--;
			}
			return qualifiedTypeName;
		}

		String typeName = Signature.toString(signatureName);

		String foundName = getQualifiedTypeName(classType, typeName);
		// if found in current type
		if (foundName != null) {
			qualifiedTypeName = foundName;
		} else // else found in the type hierachy.
		{
			ITypeHierarchy typeHierarchy = null;
			try {
				typeHierarchy = classType.newSupertypeHierarchy(null);
			} catch (JavaModelException e) {
				// Nothing to do.
			}
			if (typeHierarchy != null) {
				IType[] superTypes = typeHierarchy.getAllSupertypes(classType);

				if (superTypes != null && superTypes.length > 0) {
					for (int i = 0; i < superTypes.length; i++) {
						if (!superTypes[i].getFullyQualifiedName().equals(
								"java.lang.Object")) { //$NON-NLS-1$
							foundName = getQualifiedTypeName(superTypes[i],
									typeName);
							if (foundName != null) {
								qualifiedTypeName = foundName;
								break;
							}
						}
					}
				}
			}
		}

		// Add back array identifiers
		while (arrayNum > 0) {
			qualifiedTypeName = qualifiedTypeName + "[]"; //$NON-NLS-1$
			arrayNum--;
		}
		return qualifiedTypeName;
	}

	/**
	 * resolve and get the qualified name for the incomplete typename
	 * 
	 * @param classType
	 * @param typeName
	 * @return can be null.
	 */
	public static String getQualifiedTypeName(IType classType, String typeName) {
		String qualifiedTypeName = null;

		try {
			String[][] resolvedNames = classType.resolveType(typeName);
			if (resolvedNames != null && resolvedNames.length > 0) {
				qualifiedTypeName = JavaModelUtil.concatenateName(
						resolvedNames[0][0], resolvedNames[0][1]);
			}
		} catch (JavaModelException e1) {
			// Nothing to do.
		}

		return qualifiedTypeName;
	}

	/**
	 * check whether subclass is sub class of supperclass
	 * 
	 * @param jProject
	 * @param subClass -
	 *            fully qualified name of sub class
	 * @param superClass -
	 *            fully qualified name of super class
	 * 
	 * @return true if subClass is a sub  of  superClass
	 */
	public static boolean isSubClassOf(IJavaProject jProject, String subClass,
			String superClass) {
		if (jProject == null || subClass == null || superClass == null) {
			return false;
		}

		try {
			IType subClassType = jProject.findType(subClass);

			if (subClassType != null) {
				ITypeHierarchy typeHierarchy = null;
				try {
					typeHierarchy = subClassType.newSupertypeHierarchy(null);
				} catch (JavaModelException e) {
					// Nothing to do.
				}
				IType[] superTypes = typeHierarchy
						.getAllSupertypes(subClassType);

				if (superTypes != null && superTypes.length > 0) {
					for (int i = 0; i < superTypes.length; i++) {
						if (superTypes[i].getFullyQualifiedName().equals(
								superClass)) {
							return true;
						}
					}
				}
			}
		} catch (JavaModelException e) {
			// Nothing to do.
		}
		return false;
	}
}

Back to the top