Skip to content

Commit e8e035b

Browse files
committed
Fix search for methods in classes in jars
Fixes 1 Signed-off-by: David Thompson <[email protected]>
1 parent 3023d62 commit e8e035b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/core/search/matching/DOMJavaSearchDelegate.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void locateMatches(MatchLocator locator, IJavaProject javaProject, Possib
143143
org.eclipse.jdt.core.ICompilationUnit[] unitArray = new org.eclipse.jdt.core.ICompilationUnit[possibleMatches.length];
144144

145145
Map<org.eclipse.jdt.core.ICompilationUnit, PossibleMatch> cuToMatch = new HashMap<>();
146-
for (int i = 0; i < possibleMatches.length; i++) {
146+
for (int i = start; i < start + length; i++) {
147147
var currentPossibleMatch = possibleMatches[i];
148148
locator.currentPossibleMatch = currentPossibleMatch;
149149
if (!skipMatch(locator, javaProject, currentPossibleMatch)) {
@@ -565,10 +565,25 @@ private void locateMatchesForBinary(PossibleMatch possibleMatch, MatchLocator lo
565565
ILog.get().error(ce.getMessage(), ce);
566566
}
567567
if (info != null) {
568+
// Redoes the getMethodBinding logic to avoid requiring a CompilationUnitScope
569+
if (locator.patternLocator instanceof MethodLocator methodLocator) {
570+
methodLocator.matchLocator = new DOMMatchLocator(locator.pattern, locator.requestor, locator.scope, locator.progressMonitor);
571+
methodLocator.matchLocator.lookupEnvironment = locator.lookupEnvironment;
572+
}
573+
/**
574+
* @see MatchLocator#process(PossibleMatch, boolean)
575+
*/
576+
boolean oldMayBeGeneric = locator.patternLocator.mayBeGeneric;
577+
locator.patternLocator.mayBeGeneric = false;
568578
try {
569579
new ClassFileMatchLocator().locateMatches(locator, classFile, info);
570580
} catch (CoreException e) {
571581
ILog.get().error(e.getMessage(), e);
582+
} finally {
583+
locator.patternLocator.mayBeGeneric = oldMayBeGeneric;
584+
if (locator.patternLocator instanceof MethodLocator methodLocator) {
585+
methodLocator.matchLocator = locator;
586+
}
572587
}
573588
}
574589
} else if (possibleMatch.openable instanceof ModularClassFile modularClassFile) { // no source

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/core/search/matching/DOMMatchLocator.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212

1313
import java.util.HashSet;
1414
import java.util.Set;
15+
import java.util.stream.Stream;
1516

1617
import org.eclipse.core.runtime.IProgressMonitor;
18+
import org.eclipse.jdt.core.compiler.CharOperation;
1719
import org.eclipse.jdt.core.dom.IModuleBinding;
1820
import org.eclipse.jdt.core.search.IJavaSearchScope;
1921
import org.eclipse.jdt.core.search.SearchPattern;
2022
import org.eclipse.jdt.core.search.SearchRequestor;
23+
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
24+
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
25+
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
2126

2227
public class DOMMatchLocator extends MatchLocator {
2328

@@ -34,4 +39,34 @@ public void registerModuleBinding(IModuleBinding binding) {
3439
}
3540
}
3641

42+
@Override
43+
public MethodBinding getMethodBinding(MethodPattern methodPattern) {
44+
char[][] compositeTypeName = Stream.of(methodPattern.declaringType.getFullyQualifiedName().split("\\.")).map(String::toCharArray).toArray(char[][]::new);
45+
ReferenceBinding typeRefBinding = lookupEnvironment.askForType(compositeTypeName, lookupEnvironment.module);
46+
for (MethodBinding method : typeRefBinding.methods()) {
47+
int numParams = method.parameters.length;
48+
if (numParams != methodPattern.parameterCount) {
49+
continue;
50+
}
51+
for (int i = 0; i < numParams; i++) {
52+
StringBuilder patternParam = new StringBuilder();
53+
if (methodPattern.parameterQualifications[i] != null && methodPattern.parameterQualifications[i].length > 0) {
54+
patternParam.append(methodPattern.parameterQualifications[i]);
55+
patternParam.append('.');
56+
}
57+
patternParam.append(methodPattern.parameterSimpleNames[i]);
58+
char[] patternParamChar = patternParam.toString().replaceAll("\\.", "/").toCharArray();
59+
TypeBinding typeBinding = method.parameters[i];
60+
char[] potentialMatchParamType = typeBinding.constantPoolName();
61+
if (!CharOperation.equals(patternParamChar, potentialMatchParamType)) {
62+
continue;
63+
}
64+
}
65+
if (CharOperation.equals(method.selector, methodPattern.selector)) {
66+
return method;
67+
}
68+
}
69+
return null;
70+
}
71+
3772
}

0 commit comments

Comments
 (0)