Lines 14-19
Link Here
|
14 |
import org.eclipse.jdt.core.*; |
14 |
import org.eclipse.jdt.core.*; |
15 |
import org.eclipse.jdt.core.compiler.CharOperation; |
15 |
import org.eclipse.jdt.core.compiler.CharOperation; |
16 |
import org.eclipse.jdt.core.search.*; |
16 |
import org.eclipse.jdt.core.search.*; |
|
|
17 |
import org.eclipse.jdt.core.util.ByteCodeVisitorAdapter; |
18 |
import org.eclipse.jdt.core.util.ClassFormatException; |
19 |
import org.eclipse.jdt.core.util.IClassFileReader; |
20 |
import org.eclipse.jdt.core.util.ICodeAttribute; |
21 |
import org.eclipse.jdt.core.util.IConstantPoolEntry; |
22 |
import org.eclipse.jdt.core.util.IMethodInfo; |
17 |
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; |
23 |
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; |
18 |
import org.eclipse.jdt.internal.compiler.classfmt.FieldInfo; |
24 |
import org.eclipse.jdt.internal.compiler.classfmt.FieldInfo; |
19 |
import org.eclipse.jdt.internal.compiler.classfmt.MethodInfo; |
25 |
import org.eclipse.jdt.internal.compiler.classfmt.MethodInfo; |
Lines 24-29
Link Here
|
24 |
|
30 |
|
25 |
public class ClassFileMatchLocator implements IIndexConstants { |
31 |
public class ClassFileMatchLocator implements IIndexConstants { |
26 |
|
32 |
|
|
|
33 |
private static final class ReferenceMatcher extends ByteCodeVisitorAdapter { |
34 |
|
35 |
SearchPattern pattern; |
36 |
MatchLocator locator; |
37 |
IMethodInfo methodInfo; |
38 |
BinaryType binaryType; |
39 |
BinaryTypeBinding binding; |
40 |
|
41 |
public ReferenceMatcher( |
42 |
SearchPattern pattern, |
43 |
MatchLocator locator, |
44 |
IMethodInfo methodInfo, |
45 |
BinaryType binaryType, |
46 |
BinaryTypeBinding binding) { |
47 |
this.pattern = pattern; |
48 |
this.locator = locator; |
49 |
this.binaryType = binaryType; |
50 |
this.binding = binding; |
51 |
this.methodInfo = methodInfo; |
52 |
} |
53 |
|
54 |
public void _invokeinterface(int pc, |
55 |
int index, |
56 |
byte nargs, |
57 |
IConstantPoolEntry constantInterfaceMethodref) { |
58 |
matchMethod(constantInterfaceMethodref); |
59 |
} |
60 |
|
61 |
public void _invokespecial(int pc, int index, IConstantPoolEntry constantMethodref) { |
62 |
matchMethod(constantMethodref); |
63 |
} |
64 |
|
65 |
public void _invokestatic(int pc, int index, IConstantPoolEntry constantMethodref) { |
66 |
matchMethod(constantMethodref); |
67 |
} |
68 |
|
69 |
public void _invokevirtual(int pc, int index, IConstantPoolEntry constantMethodref) { |
70 |
matchMethod(constantMethodref); |
71 |
} |
72 |
|
73 |
private void matchMethod(IConstantPoolEntry entry) { |
74 |
int level = locator.patternLocator.resolveLevel(entry); |
75 |
if (level != PatternLocator.IMPOSSIBLE_MATCH) { |
76 |
// report match |
77 |
int accuracy = level == PatternLocator.ACCURATE_MATCH ? SearchMatch.A_ACCURATE : SearchMatch.A_INACCURATE; |
78 |
IMethod methodHandle = this.binaryType.getMethod( |
79 |
new String(this.methodInfo.isConstructor() ? this.binding.compoundName[this.binding.compoundName.length-1] : this.methodInfo.getName()), |
80 |
CharOperation.toStrings(Signature.getParameterTypes(convertClassFileFormat(this.methodInfo.getDescriptor())))); |
81 |
MethodReferenceMatch match = new MethodReferenceMatch( |
82 |
methodHandle, |
83 |
accuracy, |
84 |
-1, |
85 |
0, |
86 |
this.methodInfo.isConstructor(), |
87 |
false, |
88 |
false, |
89 |
locator.getParticipant(), |
90 |
null); |
91 |
try { |
92 |
locator.report(match); |
93 |
} catch (CoreException e) { |
94 |
// ignore |
95 |
} |
96 |
} |
97 |
} |
98 |
|
99 |
public void _getfield(int pc, int index, IConstantPoolEntry constantFieldref) { |
100 |
matchField(constantFieldref, true); |
101 |
} |
102 |
public void _getstatic(int pc, int index, IConstantPoolEntry constantFieldref) { |
103 |
matchField(constantFieldref, true); |
104 |
} |
105 |
public void _putfield(int pc, int index, IConstantPoolEntry constantFieldref) { |
106 |
matchField(constantFieldref, false); |
107 |
} |
108 |
public void _putstatic(int pc, int index, IConstantPoolEntry constantFieldref) { |
109 |
matchField(constantFieldref, false); |
110 |
} |
111 |
private void matchField(IConstantPoolEntry entry, boolean isReadAccess) { |
112 |
int level = locator.patternLocator.resolveLevel(entry); |
113 |
if (level != PatternLocator.IMPOSSIBLE_MATCH) { |
114 |
// report match |
115 |
int accuracy = level == PatternLocator.ACCURATE_MATCH ? SearchMatch.A_ACCURATE : SearchMatch.A_INACCURATE; |
116 |
IMethod methodHandle = this.binaryType.getMethod( |
117 |
new String(this.methodInfo.isConstructor() ? this.binding.compoundName[this.binding.compoundName.length-1] : this.methodInfo.getName()), |
118 |
CharOperation.toStrings(Signature.getParameterTypes(convertClassFileFormat(this.methodInfo.getDescriptor())))); |
119 |
FieldReferenceMatch match = new FieldReferenceMatch( |
120 |
methodHandle, |
121 |
accuracy, |
122 |
-1, |
123 |
0, |
124 |
isReadAccess, |
125 |
!isReadAccess, |
126 |
false, |
127 |
locator.getParticipant(), |
128 |
null); |
129 |
try { |
130 |
locator.report(match); |
131 |
} catch (CoreException e) { |
132 |
// ignore |
133 |
} |
134 |
} |
135 |
} |
136 |
} |
27 |
private static final long TARGET_ANNOTATION_BITS = |
137 |
private static final long TARGET_ANNOTATION_BITS = |
28 |
TagBits.AnnotationForType | |
138 |
TagBits.AnnotationForType | |
29 |
TagBits.AnnotationForParameter | |
139 |
TagBits.AnnotationForParameter | |
Lines 151-156
Link Here
|
151 |
// check annotations references |
261 |
// check annotations references |
152 |
matchAnnotations(pattern, locator, classFile, info); |
262 |
matchAnnotations(pattern, locator, classFile, info); |
153 |
|
263 |
|
|
|
264 |
if (searchForReferences(pattern)) { |
265 |
matchReferences(pattern, locator, classFile, info); |
266 |
} |
154 |
// check class definition |
267 |
// check class definition |
155 |
BinaryType binaryType = (BinaryType) classFile.getType(); |
268 |
BinaryType binaryType = (BinaryType) classFile.getType(); |
156 |
if (matchBinary(pattern, info, null)) { |
269 |
if (matchBinary(pattern, info, null)) { |
Lines 304-309
Link Here
|
304 |
} |
417 |
} |
305 |
} |
418 |
} |
306 |
} |
419 |
} |
|
|
420 |
private void matchReferences(SearchPattern pattern, MatchLocator locator, ClassFile classFile, IBinaryType info) throws CoreException { |
421 |
BinaryType binaryType = (BinaryType) classFile.getType(); |
422 |
BinaryTypeBinding binding = locator.cacheBinaryType(binaryType, info); |
423 |
if (binding == null) return; |
424 |
// filter out element not in hierarchy scope |
425 |
if (!locator.typeInHierarchy(binding)) return; |
426 |
int kind = ((InternalSearchPattern)pattern).kind; |
427 |
switch (kind) { |
428 |
case CONSTRUCTOR_PATTERN : |
429 |
case FIELD_PATTERN : |
430 |
case METHOD_PATTERN : |
431 |
case SUPER_REF_PATTERN : |
432 |
case PKG_REF_PATTERN : |
433 |
case TYPE_REF_PATTERN : |
434 |
matchReferences(kind, binaryType, pattern, locator, classFile, info, binding); |
435 |
break; |
436 |
case OR_PATTERN : |
437 |
SearchPattern[] patterns = ((OrPattern) pattern).patterns; |
438 |
for (int i = 0, length = patterns.length; i < length; i++) { |
439 |
matchReferences(patterns[i], locator, classFile, info); |
440 |
break; |
441 |
} |
442 |
} |
443 |
} |
444 |
/* |
445 |
* Match references |
446 |
*/ |
447 |
private void matchReferences( |
448 |
int kind, |
449 |
BinaryType binaryType, |
450 |
SearchPattern pattern, |
451 |
MatchLocator locator, |
452 |
ClassFile classFile, |
453 |
IBinaryType info, |
454 |
BinaryTypeBinding binding) throws CoreException { |
455 |
|
456 |
if (classFile != null) { |
457 |
IClassFileReader classFileReader = ToolFactory.createDefaultClassFileReader(classFile, IClassFileReader.METHOD_BODIES | IClassFileReader.METHOD_INFOS); |
458 |
if (classFileReader != null) { |
459 |
switch(kind) { |
460 |
case FIELD_PATTERN : |
461 |
case CONSTRUCTOR_PATTERN : |
462 |
case METHOD_PATTERN : |
463 |
IMethodInfo[] methodInfos = classFileReader.getMethodInfos(); |
464 |
for (int i = 0, max = methodInfos.length; i < max; i++) { |
465 |
IMethodInfo methodInfo = methodInfos[i]; |
466 |
matches(methodInfo, pattern, locator, binaryType, binding); |
467 |
} |
468 |
break; |
469 |
// case SUPER_REF_PATTERN : |
470 |
// case PKG_REF_PATTERN : |
471 |
// case TYPE_REF_PATTERN : |
472 |
} |
473 |
} |
474 |
} |
475 |
} |
476 |
private void matches(IMethodInfo methodInfo, SearchPattern pattern, MatchLocator locator, BinaryType binaryType, BinaryTypeBinding binding) { |
477 |
ICodeAttribute codeAttribute = methodInfo.getCodeAttribute(); |
478 |
if (codeAttribute != null) { |
479 |
try { |
480 |
ReferenceMatcher methodReferenceMatcher = new ReferenceMatcher(pattern, locator, methodInfo, binaryType, binding); |
481 |
codeAttribute.traverse(methodReferenceMatcher); |
482 |
} catch (ClassFormatException e) { |
483 |
// ignore |
484 |
} |
485 |
} |
486 |
} |
487 |
private boolean searchForReferences(SearchPattern pattern) { |
488 |
switch (((InternalSearchPattern)pattern).kind) { |
489 |
case CONSTRUCTOR_PATTERN : |
490 |
return ((ConstructorPattern) pattern).findReferences; |
491 |
case FIELD_PATTERN : |
492 |
return ((FieldPattern) pattern).findReferences; |
493 |
case METHOD_PATTERN : |
494 |
return ((MethodPattern) pattern).findReferences; |
495 |
case SUPER_REF_PATTERN : |
496 |
return true; |
497 |
case PKG_REF_PATTERN : |
498 |
return true; |
499 |
case OR_PATTERN : |
500 |
SearchPattern[] patterns = ((OrPattern) pattern).patterns; |
501 |
for (int i = 0, length = patterns.length; i < length; i++) { |
502 |
if (searchForReferences(patterns[i])) return true; |
503 |
} |
504 |
} |
505 |
return false; |
506 |
} |
507 |
|
307 |
/* |
508 |
/* |
308 |
* Look for annotations references |
509 |
* Look for annotations references |
309 |
*/ |
510 |
*/ |