Skip to main content
summaryrefslogtreecommitdiffstats
blob: 48e2819521470a38bf656721dc8c889aa5b1414e (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
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * $Id: ClassFileMatchLocator.java 19915 2009-04-19 17:23:21Z stephan $
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Fraunhofer FIRST - extended API and implementation
 *     Technical University Berlin - extended API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.core.search.matching;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.*;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.search.*;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.env.*;
import org.eclipse.jdt.internal.compiler.lookup.*;
import org.eclipse.jdt.internal.core.BinaryType;
import org.eclipse.jdt.internal.core.*;
import org.eclipse.jdt.internal.core.search.indexing.IIndexConstants;

public class ClassFileMatchLocator implements IIndexConstants {

private static final long TARGET_ANNOTATION_BITS = 
	TagBits.AnnotationForType |
	TagBits.AnnotationForParameter |
	TagBits.AnnotationForPackage |
	TagBits.AnnotationForMethod |
	TagBits.AnnotationForLocalVariable |
	TagBits.AnnotationForField |
	TagBits.AnnotationForConstructor |
	TagBits.AnnotationForAnnotationType;
private static final char[] JAVA_LANG_ANNOTATION_ELEMENTTYPE = CharOperation.concatWith(TypeConstants.JAVA_LANG_ANNOTATION_ELEMENTTYPE, '.');
public static char[] convertClassFileFormat(char[] name) {
	return CharOperation.replaceOnCopy(name, '/', '.');
}

private  boolean checkAnnotation(IBinaryAnnotation annotation, TypeReferencePattern pattern) {
	if (checkTypeName(pattern.simpleName, pattern.qualification, convertClassFileFormat(Signature.toCharArray(annotation.getTypeName())), pattern.isCaseSensitive, pattern.isCamelCase)) {
		return true;
	}
	IBinaryElementValuePair[] valuePairs = annotation.getElementValuePairs();
	if (valuePairs != null) {
		for (int j=0, vpLength=valuePairs.length; j<vpLength; j++) {
			IBinaryElementValuePair valuePair = valuePairs[j];
			Object pairValue = valuePair.getValue();
			if (pairValue instanceof IBinaryAnnotation) {
				if (checkAnnotation((IBinaryAnnotation) pairValue, pattern)) {
					return true;
				}
			}
		}
	}
	return false;
}
private boolean checkAnnotations(TypeReferencePattern pattern, IBinaryAnnotation[] annotations, long tagBits) {
	if (annotations != null) {
		for (int a=0, length=annotations.length; a<length; a++) {
			IBinaryAnnotation annotation = annotations[a];
			if (checkAnnotation(annotation, pattern)) {
				return true;
			}
		}
	}
	if ((tagBits & TagBits.AllStandardAnnotationsMask) != 0 && checkStandardAnnotations(tagBits, pattern)) {
		return true;
	}
	return false;
}
private boolean checkAnnotationTypeReference(char[] fullyQualifiedName, TypeReferencePattern pattern) {
	return checkTypeName(pattern.simpleName, pattern.qualification, fullyQualifiedName, pattern.isCaseSensitive, pattern.isCamelCase);
}
private boolean checkDeclaringType(IBinaryType enclosingBinaryType, char[] simpleName, char[] qualification, boolean isCaseSensitive, boolean isCamelCase) {
	if (simpleName == null && qualification == null) return true;
	if (enclosingBinaryType == null) return true;

	char[] declaringTypeName = convertClassFileFormat(enclosingBinaryType.getName());
	return checkTypeName(simpleName, qualification, declaringTypeName, isCaseSensitive, isCamelCase);
}
private boolean checkParameters(char[] methodDescriptor, char[][] parameterSimpleNames, char[][] parameterQualifications, boolean isCaseSensitive, boolean isCamelCase) {
	char[][] arguments = Signature.getParameterTypes(methodDescriptor);
	int parameterCount = parameterSimpleNames.length;
	if (parameterCount != arguments.length) return false;
	for (int i = 0; i < parameterCount; i++)
		if (!checkTypeName(parameterSimpleNames[i], parameterQualifications[i], Signature.toCharArray(arguments[i]), isCaseSensitive, isCamelCase))
			return false;
	return true;
}
private boolean checkStandardAnnotations(long annotationTagBits, TypeReferencePattern pattern) {
	if ((annotationTagBits & TagBits.AllStandardAnnotationsMask) == 0) {
		return false;
	}
	if ((annotationTagBits & TagBits.AnnotationTargetMASK) != 0) {
		char[][] compoundName = TypeConstants.JAVA_LANG_ANNOTATION_TARGET;
		if (checkAnnotationTypeReference(CharOperation.concatWith(compoundName, '.'), pattern) ||
			((annotationTagBits & TARGET_ANNOTATION_BITS) != 0 && checkAnnotationTypeReference(JAVA_LANG_ANNOTATION_ELEMENTTYPE, pattern))) {
			return true;
		}
	}
	if ((annotationTagBits & TagBits.AnnotationRetentionMASK) != 0) {
		char[][] compoundName = TypeConstants.JAVA_LANG_ANNOTATION_RETENTION;
		if (checkAnnotationTypeReference(CharOperation.concatWith(compoundName, '.'), pattern) ||
			checkAnnotationTypeReference(CharOperation.concatWith(TypeConstants.JAVA_LANG_ANNOTATION_RETENTIONPOLICY, '.'), pattern)) {
			return true;
		}
	}
	if ((annotationTagBits & TagBits.AnnotationDeprecated) != 0) {
		char[][] compoundName = TypeConstants.JAVA_LANG_DEPRECATED;
		if (checkAnnotationTypeReference(CharOperation.concatWith(compoundName, '.'), pattern)) {
			return true;
		}
	}
	if ((annotationTagBits & TagBits.AnnotationDocumented) != 0) {
		char[][] compoundName = TypeConstants.JAVA_LANG_ANNOTATION_DOCUMENTED;
		if (checkAnnotationTypeReference(CharOperation.concatWith(compoundName, '.'), pattern)) {
			return true;
		}
	}
	if ((annotationTagBits & TagBits.AnnotationInherited) != 0) {
		char[][] compoundName = TypeConstants.JAVA_LANG_ANNOTATION_INHERITED;
		if (checkAnnotationTypeReference(CharOperation.concatWith(compoundName, '.'), pattern)) {
			return true;
		}
	}
	if ((annotationTagBits & TagBits.AnnotationOverride) != 0) {
		char[][] compoundName = TypeConstants.JAVA_LANG_OVERRIDE;
		if (checkAnnotationTypeReference(CharOperation.concatWith(compoundName, '.'), pattern)) {
			return true;
		}
	}
	if ((annotationTagBits & TagBits.AnnotationSuppressWarnings) != 0) {
		char[][] compoundName = TypeConstants.JAVA_LANG_SUPPRESSWARNINGS;
		if (checkAnnotationTypeReference(CharOperation.concatWith(compoundName, '.'), pattern)) {
			return true;
		}
	}
	if ((annotationTagBits & TagBits.AnnotationSafeVarargs) != 0) {
		char[][] compoundName = TypeConstants.JAVA_LANG_SAFEVARARGS;
		if (checkAnnotationTypeReference(CharOperation.concatWith(compoundName, '.'), pattern)) {
			return true;
		}
	}
	if ((annotationTagBits & TagBits.AnnotationPolymorphicSignature) != 0) {
		char[][] compoundName = TypeConstants.JAVA_LANG_INVOKE_METHODHANDLE_$_POLYMORPHICSIGNATURE;
		if (checkAnnotationTypeReference(CharOperation.concatWith(compoundName, '.'), pattern)) {
			return true;
		}
	}
	return false;
}
private boolean checkTypeName(char[] simpleName, char[] qualification, char[] fullyQualifiedTypeName, boolean isCaseSensitive, boolean isCamelCase) {
	// NOTE: if case insensitive then simpleName & qualification are assumed to be lowercase
	char[] wildcardPattern = PatternLocator.qualifiedPattern(simpleName, qualification);
	if (wildcardPattern == null) return true;
	return CharOperation.match(wildcardPattern, fullyQualifiedTypeName, isCaseSensitive);
}
/**
 * Locate declaration in the current class file. This class file is always in a jar.
 */
public void locateMatches(MatchLocator locator, ClassFile classFile, IBinaryType info) throws CoreException {
	SearchPattern pattern = locator.pattern;

	// check annotations references
	matchAnnotations(pattern, locator, classFile, info);

	// check class definition
	BinaryType binaryType = (BinaryType) classFile.getType();
	if (matchBinary(pattern, info, null)) {
		binaryType = new ResolvedBinaryType((JavaElement) binaryType.getParent(), binaryType.getElementName(), binaryType.getKey());
		locator.reportBinaryMemberDeclaration(null, binaryType, null, info, SearchMatch.A_ACCURATE);
		return;
	}

	// Define arrays to store methods/fields from binary type if necessary
	IBinaryMethod[] binaryMethods = info.getMethods();
	int bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length;
	IBinaryMethod[] unresolvedMethods = null;
	char[][] binaryMethodSignatures = null;
	boolean hasUnresolvedMethods = false;

	// Get fields from binary type info
	IBinaryField[] binaryFields = info.getFields();
	int bFieldsLength = binaryFields == null ? 0 : binaryFields.length;
	IBinaryField[] unresolvedFields = null;
	boolean hasUnresolvedFields = false;
	
	// Report as many accurate matches as possible
	int accuracy = SearchMatch.A_ACCURATE;
	boolean mustResolve = pattern.mustResolve;
	if (mustResolve) {
		BinaryTypeBinding binding = locator.cacheBinaryType(binaryType, info);
		if (binding != null) {
			// filter out element not in hierarchy scope
			if (!locator.typeInHierarchy(binding)) return;

			// Search matches on resolved methods
			MethodBinding[] availableMethods = binding.availableMethods();
			int aMethodsLength = availableMethods == null ? 0 : availableMethods.length;
			hasUnresolvedMethods = bMethodsLength != aMethodsLength;
			for (int i = 0; i < aMethodsLength; i++) {
				MethodBinding method = availableMethods[i];
				char[] methodSignature = method.genericSignature();
				if (methodSignature == null) methodSignature = method.signature();

				// Report the match if possible
				int level = locator.patternLocator.resolveLevel(method);
				if (level != PatternLocator.IMPOSSIBLE_MATCH) {
					IMethod methodHandle = binaryType.getMethod(
						new String(method.isConstructor() ? binding.compoundName[binding.compoundName.length-1] : method.selector),
						CharOperation.toStrings(Signature.getParameterTypes(convertClassFileFormat(methodSignature))));
					accuracy = level == PatternLocator.ACCURATE_MATCH ? SearchMatch.A_ACCURATE : SearchMatch.A_INACCURATE;
					locator.reportBinaryMemberDeclaration(null, methodHandle, method, info, accuracy);
				}

				// Remove method from unresolved list
				if (hasUnresolvedMethods) {
					if (binaryMethodSignatures == null) { // Store binary method signatures to avoid multiple computation
						binaryMethodSignatures = new char[bMethodsLength][];
						for (int j=0; j<bMethodsLength; j++) {
							IBinaryMethod binaryMethod = binaryMethods[j];
							char[] signature = binaryMethod.getGenericSignature();
							if (signature == null) signature = binaryMethod.getMethodDescriptor();
							binaryMethodSignatures[j] = signature;
						}
					}
					for (int j=0; j<bMethodsLength; j++) {
						if (CharOperation.equals(binaryMethods[j].getSelector(), method.selector) && CharOperation.equals(binaryMethodSignatures[j], methodSignature)) {
							if (unresolvedMethods == null) {
								System.arraycopy(binaryMethods, 0, unresolvedMethods = new IBinaryMethod[bMethodsLength], 0, bMethodsLength);
							}
							unresolvedMethods[j] = null;
							break;
						}
					}
				}
			}

			// Search matches on resolved fields
			FieldBinding[] availableFields = binding.availableFields();
			int aFieldsLength = availableFields == null ? 0 : availableFields.length;
			hasUnresolvedFields = bFieldsLength != aFieldsLength;
			for (int i = 0; i < aFieldsLength; i++) {
				FieldBinding field = availableFields[i];

				// Report the match if possible
				int level = locator.patternLocator.resolveLevel(field);
				if (level != PatternLocator.IMPOSSIBLE_MATCH) {
					IField fieldHandle = binaryType.getField(new String(field.name));
					accuracy = level == PatternLocator.ACCURATE_MATCH ? SearchMatch.A_ACCURATE : SearchMatch.A_INACCURATE;
					locator.reportBinaryMemberDeclaration(null, fieldHandle, field, info, accuracy);
				}

				// Remove the field from unresolved list
				if (hasUnresolvedFields) {
					for (int j=0; j<bFieldsLength; j++) {
						if ( CharOperation.equals(binaryFields[j].getName(), field.name)) {
							if (unresolvedFields == null) {
								System.arraycopy(binaryFields, 0, unresolvedFields = new IBinaryField[bFieldsLength], 0, bFieldsLength);
							}
							unresolvedFields[j] = null;
							break;
						}
					}
				}
			}

			// If all methods/fields were accurate then returns now
			if (!hasUnresolvedMethods && !hasUnresolvedFields) {
				return;
			}
		}
		accuracy = SearchMatch.A_INACCURATE;
	}

	// Report inaccurate methods
	if (mustResolve) binaryMethods = unresolvedMethods;
	bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length;
	for (int i=0; i < bMethodsLength; i++) {
		IBinaryMethod method = binaryMethods[i];
		if (method == null) continue; // impossible match or already reported as accurate
		if (matchBinary(pattern, method, info)) {
			char[] name;
			if (method.isConstructor()) {
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=329727
				// We don't need the enclosing type name for the constructor name
				name = info.getSourceName();
			} else {
				name = method.getSelector();
			}
			String selector = new String(name);
			char[] methodSignature = binaryMethodSignatures == null ? null : binaryMethodSignatures[i];
			if (methodSignature == null) {
				methodSignature = method.getGenericSignature();
				if (methodSignature == null) methodSignature = method.getMethodDescriptor();
			}
			String[] parameterTypes = CharOperation.toStrings(Signature.getParameterTypes(convertClassFileFormat(methodSignature)));
			IMethod methodHandle = binaryType.getMethod(selector, parameterTypes);
			methodHandle = new ResolvedBinaryMethod(binaryType, selector, parameterTypes, methodHandle.getKey());
			locator.reportBinaryMemberDeclaration(null, methodHandle, null, info, accuracy);
		}
	}

	// Report inaccurate fields
	if (mustResolve) binaryFields =  unresolvedFields;
	bFieldsLength = binaryFields == null ? 0 : binaryFields.length;
	for (int i=0; i<bFieldsLength; i++) {
		IBinaryField field = binaryFields[i];
		if (field == null) continue; // impossible match or already reported as accurate
		if (matchBinary(pattern, field, info)) {
			String fieldName = new String(field.getName());
			IField fieldHandle = binaryType.getField(fieldName);
			fieldHandle = new ResolvedBinaryField(binaryType, fieldName, fieldHandle.getKey());
			locator.reportBinaryMemberDeclaration(null, fieldHandle, null, info, accuracy);
		}
	}
}
/*
 * Look for annotations references
 */
private void matchAnnotations(SearchPattern pattern, MatchLocator locator, ClassFile classFile, IBinaryType binaryType) throws CoreException {
	// Only process TypeReference patterns
	switch (pattern.kind) {
		case TYPE_REF_PATTERN:
			break;
		case OR_PATTERN:
			SearchPattern[] patterns = ((OrPattern) pattern).patterns;
			for (int i = 0, length = patterns.length; i < length; i++) {
				matchAnnotations(patterns[i], locator, classFile, binaryType);
			}
			// $FALL-THROUGH$ - fall through default to return
		default:
			return;
	}
	TypeReferencePattern typeReferencePattern  = (TypeReferencePattern) pattern;

	// Look for references in class annotations
	IBinaryAnnotation[] annotations = binaryType.getAnnotations();
	BinaryType classFileBinaryType = (BinaryType) classFile.getType();
	BinaryTypeBinding binaryTypeBinding = null;
	if (checkAnnotations(typeReferencePattern, annotations, binaryType.getTagBits())) {
		classFileBinaryType = new ResolvedBinaryType((JavaElement) classFileBinaryType.getParent(), classFileBinaryType.getElementName(), classFileBinaryType.getKey());
		TypeReferenceMatch match = new TypeReferenceMatch(classFileBinaryType, SearchMatch.A_ACCURATE, -1, 0, false, locator.getParticipant(), locator.currentPossibleMatch.resource);
		// TODO 3.4 M7 (frederic) - bug 209996: see how create the annotation handle from the binary and put it in the local element
		match.setLocalElement(null);
		locator.report(match);
	}

	// Look for references in methods annotations
	IBinaryMethod[] methods = binaryType.getMethods();
	if (methods != null) {
		for (int i = 0, max = methods.length; i < max; i++) {
			IBinaryMethod method = methods[i];
			if (checkAnnotations(typeReferencePattern, method.getAnnotations(), method.getTagBits())) {
					binaryTypeBinding = locator.cacheBinaryType(classFileBinaryType, binaryType);
					IMethod methodHandle = classFileBinaryType.getMethod(
						new String(method.isConstructor() ? binaryTypeBinding.compoundName[binaryTypeBinding.compoundName.length-1] : method.getSelector()),
						CharOperation.toStrings(Signature.getParameterTypes(convertClassFileFormat(method.getMethodDescriptor()))));
					TypeReferenceMatch match = new TypeReferenceMatch(methodHandle, SearchMatch.A_ACCURATE, -1, 0, false, locator.getParticipant(), locator.currentPossibleMatch.resource);
					// TODO 3.4 M7 (frederic) - bug 209996: see how create the annotation handle from the binary and put it in the local element
					match.setLocalElement(null);
					locator.report(match);
			}
		}
	}
	
	// Look for references in fields annotations
	IBinaryField[] fields = binaryType.getFields();
	if (fields != null) {
		for (int i = 0, max = fields.length; i < max; i++) {
			IBinaryField field = fields[i];
			if (checkAnnotations(typeReferencePattern, field.getAnnotations(), field.getTagBits())) {
					IField fieldHandle = classFileBinaryType.getField(new String(field.getName()));
					TypeReferenceMatch match = new TypeReferenceMatch(fieldHandle, SearchMatch.A_ACCURATE, -1, 0, false, locator.getParticipant(), locator.currentPossibleMatch.resource);
					// TODO 3.4 M7 (frederic) - bug 209996: see how create the annotation handle from the binary and put it in the local element
					match.setLocalElement(null);
					locator.report(match);
			}
		}
	}
}
/**
 * Finds out whether the given binary info matches the search pattern.
 * Default is to return false.
 */
boolean matchBinary(SearchPattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) {
	switch (pattern.kind) {
		case CONSTRUCTOR_PATTERN :
			return matchConstructor((ConstructorPattern) pattern, binaryInfo, enclosingBinaryType);
		case FIELD_PATTERN :
			return matchField((FieldPattern) pattern, binaryInfo, enclosingBinaryType);
		case METHOD_PATTERN :
			return matchMethod((MethodPattern) pattern, binaryInfo, enclosingBinaryType);
		case SUPER_REF_PATTERN :
			return matchSuperTypeReference((SuperTypeReferencePattern) pattern, binaryInfo, enclosingBinaryType);
		case TYPE_DECL_PATTERN :
//{ObjectTeams: for now, treat TEAM and ROLE_DECL_PATTERNS as TYPE_DECL_PATTERNS
//TODO (carp): may need something different for ROLE_DECL_PATTERN!
		case TEAM_DECL_PATTERN :
		case ROLE_DECL_PATTERN :
//carp}
			return matchTypeDeclaration((TypeDeclarationPattern) pattern, binaryInfo, enclosingBinaryType);
		case OR_PATTERN :
			SearchPattern[] patterns = ((OrPattern) pattern).patterns;
			for (int i = 0, length = patterns.length; i < length; i++)
				if (matchBinary(patterns[i], binaryInfo, enclosingBinaryType)) return true;
	}
	return false;
}
boolean matchConstructor(ConstructorPattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) {
	if (!pattern.findDeclarations) return false; // only relevant when finding declarations
	if (!(binaryInfo instanceof IBinaryMethod)) return false;

	IBinaryMethod method = (IBinaryMethod) binaryInfo;
	if (!method.isConstructor()) return false;
	if (!checkDeclaringType(enclosingBinaryType, pattern.declaringSimpleName, pattern.declaringQualification, pattern.isCaseSensitive(), pattern.isCamelCase()))
		return false;
	if (pattern.parameterSimpleNames != null) {
		char[] methodDescriptor = convertClassFileFormat(method.getMethodDescriptor());
		if (!checkParameters(methodDescriptor, pattern.parameterSimpleNames, pattern.parameterQualifications, pattern.isCaseSensitive(), pattern.isCamelCase()))
			return false;
	}
	return true;
}
boolean matchField(FieldPattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) {
	if (!pattern.findDeclarations) return false; // only relevant when finding declarations
	if (!(binaryInfo instanceof IBinaryField)) return false;

	IBinaryField field = (IBinaryField) binaryInfo;
	if (!pattern.matchesName(pattern.name, field.getName())) return false;
	if (!checkDeclaringType(enclosingBinaryType, pattern.declaringSimpleName, pattern.declaringQualification, pattern.isCaseSensitive(), pattern.isCamelCase()))
		return false;

	char[] fieldTypeSignature = Signature.toCharArray(convertClassFileFormat(field.getTypeName()));
	return checkTypeName(pattern.typeSimpleName, pattern.typeQualification, fieldTypeSignature, pattern.isCaseSensitive(), pattern.isCamelCase());
}
boolean matchMethod(MethodPattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) {
	if (!pattern.findDeclarations) return false; // only relevant when finding declarations
	if (!(binaryInfo instanceof IBinaryMethod)) return false;

	IBinaryMethod method = (IBinaryMethod) binaryInfo;
	if (!pattern.matchesName(pattern.selector, method.getSelector())) return false;
	if (!checkDeclaringType(enclosingBinaryType, pattern.declaringSimpleName, pattern.declaringQualification, pattern.isCaseSensitive(), pattern.isCamelCase()))
		return false;

	// look at return type only if declaring type is not specified
	boolean checkReturnType = pattern.declaringSimpleName == null && (pattern.returnSimpleName != null || pattern.returnQualification != null);
	boolean checkParameters = pattern.parameterSimpleNames != null;
	if (checkReturnType || checkParameters) {
		char[] methodDescriptor = convertClassFileFormat(method.getMethodDescriptor());
		if (checkReturnType) {
			char[] returnTypeSignature = Signature.toCharArray(Signature.getReturnType(methodDescriptor));
			if (!checkTypeName(pattern.returnSimpleName, pattern.returnQualification, returnTypeSignature, pattern.isCaseSensitive(), pattern.isCamelCase()))
				return false;
		}
		if (checkParameters &&  !checkParameters(methodDescriptor, pattern.parameterSimpleNames, pattern.parameterQualifications, pattern.isCaseSensitive(), pattern.isCamelCase()))
			return false;
	}
	return true;
}
boolean matchSuperTypeReference(SuperTypeReferencePattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) {
	if (!(binaryInfo instanceof IBinaryType)) return false;

	IBinaryType type = (IBinaryType) binaryInfo;
	if (pattern.superRefKind != SuperTypeReferencePattern.ONLY_SUPER_INTERFACES) {
		char[] vmName = type.getSuperclassName();
		if (vmName != null) {
			char[] superclassName = convertClassFileFormat(vmName);
			if (checkTypeName(pattern.superSimpleName, pattern.superQualification, superclassName, pattern.isCaseSensitive(), pattern.isCamelCase()))
				return true;
		}
	}

	if (pattern.superRefKind != SuperTypeReferencePattern.ONLY_SUPER_CLASSES) {
		char[][] superInterfaces = type.getInterfaceNames();
		if (superInterfaces != null) {
			for (int i = 0, max = superInterfaces.length; i < max; i++) {
				char[] superInterfaceName = convertClassFileFormat(superInterfaces[i]);
				if (checkTypeName(pattern.superSimpleName, pattern.superQualification, superInterfaceName, pattern.isCaseSensitive(), pattern.isCamelCase()))
					return true;
			}
		}
	}
	return false;
}
boolean matchTypeDeclaration(TypeDeclarationPattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) {
	if (!(binaryInfo instanceof IBinaryType)) return false;

	IBinaryType type = (IBinaryType) binaryInfo;
	char[] fullyQualifiedTypeName = convertClassFileFormat(type.getName());
	boolean qualifiedPattern = pattern instanceof QualifiedTypeDeclarationPattern;
	if (pattern.enclosingTypeNames == null || qualifiedPattern) {
		char[] simpleName = (pattern.getMatchMode() == SearchPattern.R_PREFIX_MATCH)
			? CharOperation.concat(pattern.simpleName, IIndexConstants.ONE_STAR)
			: pattern.simpleName;
		char[] pkg = qualifiedPattern ? ((QualifiedTypeDeclarationPattern)pattern).qualification : pattern.pkg;
		if (!checkTypeName(simpleName, pkg, fullyQualifiedTypeName, pattern.isCaseSensitive(), pattern.isCamelCase())) return false;
	} else {
		char[] enclosingTypeName = CharOperation.concatWith(pattern.enclosingTypeNames, '.');
		char[] patternString = pattern.pkg == null
			? enclosingTypeName
			: CharOperation.concat(pattern.pkg, enclosingTypeName, '.');
		if (!checkTypeName(pattern.simpleName, patternString, fullyQualifiedTypeName, pattern.isCaseSensitive(), pattern.isCamelCase())) return false;
	}

	int kind  = TypeDeclaration.kind(type.getModifiers());
	switch (pattern.typeSuffix) {
		case CLASS_SUFFIX:
			return kind == TypeDeclaration.CLASS_DECL;
		case INTERFACE_SUFFIX:
			return kind == TypeDeclaration.INTERFACE_DECL;
		case ENUM_SUFFIX:
			return kind == TypeDeclaration.ENUM_DECL;
		case ANNOTATION_TYPE_SUFFIX:
			return kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
		case CLASS_AND_INTERFACE_SUFFIX:
			return kind == TypeDeclaration.CLASS_DECL || kind == TypeDeclaration.INTERFACE_DECL;
		case CLASS_AND_ENUM_SUFFIX:
			return kind == TypeDeclaration.CLASS_DECL || kind == TypeDeclaration.ENUM_DECL;
		case INTERFACE_AND_ANNOTATION_SUFFIX:
			return kind == TypeDeclaration.INTERFACE_DECL || kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
		case TYPE_SUFFIX: // nothing
	}
	return true;
}
}

Back to the top