Skip to content

Commit c3b0655

Browse files
committed
Better findType in DOMPatternLocator
1 parent abb5586 commit c3b0655

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@
1111
package org.eclipse.jdt.internal.core.search.matching;
1212

1313
import java.util.Arrays;
14+
import java.util.List;
15+
import java.util.Objects;
16+
import java.util.function.Predicate;
1417

1518
import org.eclipse.core.runtime.CoreException;
1619
import org.eclipse.jdt.core.Signature;
1720
import org.eclipse.jdt.core.compiler.CharOperation;
18-
import org.eclipse.jdt.core.dom.AST;
1921
import org.eclipse.jdt.core.dom.ASTNode;
2022
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
2123
import org.eclipse.jdt.core.dom.ArrayType;
24+
import org.eclipse.jdt.core.dom.CompilationUnit;
2225
import org.eclipse.jdt.core.dom.FieldAccess;
2326
import org.eclipse.jdt.core.dom.IBinding;
2427
import org.eclipse.jdt.core.dom.ITypeBinding;
28+
import org.eclipse.jdt.core.dom.ImportDeclaration;
2529
import org.eclipse.jdt.core.dom.MethodInvocation;
2630
import org.eclipse.jdt.core.dom.Name;
2731
import org.eclipse.jdt.core.dom.ParameterizedType;
@@ -41,7 +45,7 @@
4145
import org.eclipse.jdt.internal.javac.dom.JavacTypeBinding;
4246

4347
public class DOMPatternLocator extends PatternLocator {
44-
protected AST currentAST;
48+
protected ASTNode currentNode;
4549

4650
public DOMPatternLocator(SearchPattern pattern) {
4751
super(pattern);
@@ -446,7 +450,7 @@ protected void updateMatch(ITypeBinding[] argumentsBinding, char[][] patternArgu
446450
break;
447451
}
448452
patternTypeName = Signature.toCharArray(patternTypeName);
449-
ITypeBinding patternBinding = currentAST != null ? currentAST.resolveWellKnownType(new String(patternTypeName)) : null;//locator.getType(patternTypeArgument, patternTypeName);
453+
ITypeBinding patternBinding = findType(new String(patternTypeName));//locator.getType(patternTypeArgument, patternTypeName);
450454

451455
// If have no binding for pattern arg, then we won't be able to refine accuracy
452456
if (patternBinding == null) {
@@ -612,6 +616,49 @@ public final void setCurrentMatch(SearchMatch match) {
612616
}
613617

614618
public void setCurrentNode(ASTNode node) {
615-
this.currentAST = node.getAST();
619+
this.currentNode = node;
620+
}
621+
622+
private ITypeBinding findType(String name) {
623+
if (this.currentNode == null) {
624+
return null;
625+
}
626+
ITypeBinding res = this.currentNode.getAST().resolveWellKnownType(name);
627+
if (res != null) {
628+
return res;
629+
}
630+
ASTNode cursor = this.currentNode;
631+
while (cursor != null) {
632+
if (cursor instanceof CompilationUnit unit) {
633+
var explicitlyImported = ((List<ImportDeclaration>)unit.imports())
634+
.stream()
635+
.filter(Predicate.not(ImportDeclaration::isStatic))
636+
.filter(Predicate.not(ImportDeclaration::isOnDemand))
637+
.filter(decl -> decl.getName().toString().endsWith('.' + name))
638+
.map(ImportDeclaration::getName)
639+
.map(Name::toString)
640+
.map(this.currentNode.getAST()::resolveWellKnownType)
641+
.filter(Objects::nonNull)
642+
.findFirst()
643+
.orElse(null);
644+
if (explicitlyImported != null) {
645+
return explicitlyImported;
646+
}
647+
var importedOnDemand = ((List<ImportDeclaration>)unit.imports())
648+
.stream()
649+
.filter(Predicate.not(ImportDeclaration::isStatic))
650+
.filter(ImportDeclaration::isOnDemand)
651+
.map(decl -> decl.getName().toString() + '.' + name)
652+
.map(this.currentNode.getAST()::resolveWellKnownType)
653+
.filter(Objects::nonNull)
654+
.findFirst()
655+
.orElse(null);
656+
if (importedOnDemand != null) {
657+
return importedOnDemand;
658+
}
659+
}
660+
cursor = cursor.getParent();
661+
}
662+
return this.currentNode.getAST().resolveWellKnownType("java.lang." + name);
616663
}
617664
}

0 commit comments

Comments
 (0)