|
11 | 11 | package org.eclipse.jdt.internal.core.search.matching; |
12 | 12 |
|
13 | 13 | import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Objects; |
| 16 | +import java.util.function.Predicate; |
14 | 17 |
|
15 | 18 | import org.eclipse.core.runtime.CoreException; |
16 | 19 | import org.eclipse.jdt.core.Signature; |
17 | 20 | import org.eclipse.jdt.core.compiler.CharOperation; |
18 | | -import org.eclipse.jdt.core.dom.AST; |
19 | 21 | import org.eclipse.jdt.core.dom.ASTNode; |
20 | 22 | import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; |
21 | 23 | import org.eclipse.jdt.core.dom.ArrayType; |
| 24 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
22 | 25 | import org.eclipse.jdt.core.dom.FieldAccess; |
23 | 26 | import org.eclipse.jdt.core.dom.IBinding; |
24 | 27 | import org.eclipse.jdt.core.dom.ITypeBinding; |
| 28 | +import org.eclipse.jdt.core.dom.ImportDeclaration; |
25 | 29 | import org.eclipse.jdt.core.dom.MethodInvocation; |
26 | 30 | import org.eclipse.jdt.core.dom.Name; |
27 | 31 | import org.eclipse.jdt.core.dom.ParameterizedType; |
|
41 | 45 | import org.eclipse.jdt.internal.javac.dom.JavacTypeBinding; |
42 | 46 |
|
43 | 47 | public class DOMPatternLocator extends PatternLocator { |
44 | | - protected AST currentAST; |
| 48 | + protected ASTNode currentNode; |
45 | 49 |
|
46 | 50 | public DOMPatternLocator(SearchPattern pattern) { |
47 | 51 | super(pattern); |
@@ -446,7 +450,7 @@ protected void updateMatch(ITypeBinding[] argumentsBinding, char[][] patternArgu |
446 | 450 | break; |
447 | 451 | } |
448 | 452 | 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); |
450 | 454 |
|
451 | 455 | // If have no binding for pattern arg, then we won't be able to refine accuracy |
452 | 456 | if (patternBinding == null) { |
@@ -612,6 +616,49 @@ public final void setCurrentMatch(SearchMatch match) { |
612 | 616 | } |
613 | 617 |
|
614 | 618 | 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); |
616 | 663 | } |
617 | 664 | } |
0 commit comments