Skip to content

Commit

Permalink
Find type references when fqqn is not used but is imported
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <stryker@redhat.com>
  • Loading branch information
Rob Stryker committed Nov 7, 2024
1 parent 2db37c4 commit b9c0918
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public int resolveLevel(Binding binding) {
return level;
}
@Override
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
if (binding instanceof IMethodBinding constructor) {
int level= matchConstructor(constructor);
if (level== IMPOSSIBLE_MATCH) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public int resolveLevel(Binding binding) {
return matchField((FieldBinding) binding, true);
}
@Override
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
if (binding == null) return INACCURATE_MATCH;
if(binding instanceof IVariableBinding variableBinding) {
if (variableBinding.isRecordComponent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public int resolveLevel(Binding binding) {
return matchLocalVariable((LocalVariableBinding) binding, true);
}
@Override
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
if (!(binding instanceof IVariableBinding)) {
return IMPOSSIBLE_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ public int resolveLevel(Binding binding) {
return (methodLevel & MATCH_LEVEL_MASK) > (declaringLevel & MATCH_LEVEL_MASK) ? declaringLevel : methodLevel; // return the weaker match
}
@Override
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
if (binding instanceof IMethodBinding method) {
boolean skipVerif = this.pattern.findDeclarations && this.mayBeGeneric;
int methodLevel = matchMethod(method, skipVerif);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public int resolveLevel(Binding binding) {
return IMPOSSIBLE_MATCH;
}
@Override
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
if( binding instanceof IPackageBinding ipb) {
String n = ipb.getName();
String patternName = new String(this.pattern.pkgName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,9 @@ protected int resolveLevelForType(char[] simpleNamePattern, char[] qualification
// return resolveLevelForType(qualifiedPattern(simpleNamePattern, qualificationPattern), type);
char[] qualifiedPattern = getQualifiedPattern(simpleNamePattern, qualificationPattern);
int level = resolveLevelForType(qualifiedPattern, binding);
if (level == ACCURATE_MATCH || binding == null) return level;
if (level == ACCURATE_MATCH || binding == null)
return level;

ITypeBinding type = binding.isArray() ? binding.getComponentType() : binding;
char[] sourceName = null;
if (type.isMember() || type.isLocal()) {
Expand All @@ -883,7 +885,11 @@ protected int resolveLevelForType(char[] simpleNamePattern, char[] qualification
} else if (qualificationPattern == null) {
sourceName = getQualifiedSourceName(binding).toCharArray();
}
if (sourceName == null) return IMPOSSIBLE_MATCH;
if (sourceName == null)
return IMPOSSIBLE_MATCH;
return resolveLevelForTypeSourceName(qualifiedPattern, sourceName, type);
}
protected int resolveLevelForTypeSourceName(char[] qualifiedPattern, char[] sourceName, ITypeBinding type) {
switch (this.matchMode) {
case SearchPattern.R_PREFIX_MATCH:
if (CharOperation.prefixEquals(qualifiedPattern, sourceName, this.isCaseSensitive)) {
Expand Down Expand Up @@ -1233,7 +1239,7 @@ public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, MatchLocator loca
// each subtype should override if needed
return IMPOSSIBLE_MATCH;
}
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
// each subtype should override if needed
return IMPOSSIBLE_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private <T extends ASTNode> boolean defaultVisitImplementationWithFunc(
Function<T, IBinding> bindingFunc) {
int level = levelFunc.apply(node);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(bindingFunc.apply(node), this.locator);
level = this.patternLocator.resolveLevel(node, bindingFunc.apply(node), this.locator);
}
this.nodeSet.addMatch(node, level);
return true;
Expand Down Expand Up @@ -155,7 +155,7 @@ public boolean visit(SimpleName node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
IBinding b = node.resolveBinding();
level = this.patternLocator.resolveLevel(b, this.locator);
level = this.patternLocator.resolveLevel(node, b, this.locator);
}
this.nodeSet.addMatch(node, level);
return level == 0;
Expand All @@ -172,8 +172,8 @@ public boolean visit(SingleVariableDeclaration node) {
public boolean visit(EnumConstantDeclaration node) {
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
int l1 = this.patternLocator.resolveLevel(node.resolveVariable(), this.locator);
int l2 = this.patternLocator.resolveLevel(node.resolveConstructorBinding(), this.locator);
int l1 = this.patternLocator.resolveLevel(node, node.resolveVariable(), this.locator);
int l2 = this.patternLocator.resolveLevel(node, node.resolveConstructorBinding(), this.locator);
level = Math.max(l1, l2);
}
this.nodeSet.addMatch(node, level);
Expand All @@ -186,7 +186,7 @@ public boolean visit(QualifiedName node) {
}
int level = this.patternLocator.match(node, this.nodeSet, this.locator);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveBinding(), this.locator);
level = this.patternLocator.resolveLevel(node, node.resolveBinding(), this.locator);
}
this.nodeSet.addMatch(node, level);
if( (level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.IMPOSSIBLE_MATCH ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public int resolveLevel(Binding binding) {
return level;
}
@Override
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
if (binding == null) return INACCURATE_MATCH;
if (!(binding instanceof ITypeBinding)) return IMPOSSIBLE_MATCH;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public int resolveLevel(Binding binding) {
}
}
@Override
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
if (binding == null) return INACCURATE_MATCH;
if (!(binding instanceof ITypeBinding)) return IMPOSSIBLE_MATCH;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public int resolveLevel(Binding binding) {
return matchTypeParameter((TypeVariableBinding) binding, true);
}
@Override
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
if (binding == null) return INACCURATE_MATCH;
if (!(binding instanceof ITypeBinding)) return IMPOSSIBLE_MATCH;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,13 +1002,47 @@ public String toString() {
return "Locator for " + this.pattern.toString(); //$NON-NLS-1$
}
@Override
public int resolveLevel(IBinding binding, MatchLocator locator) {
public int resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBinding binding, MatchLocator locator) {
if (binding == null) {
return INACCURATE_MATCH;
}
if (binding instanceof ITypeBinding typeBinding) {
return resolveLevelForType(this.pattern.simpleName, this.pattern.qualification, typeBinding);
int newLevel = resolveLevelForType(this.pattern.simpleName, this.pattern.qualification, typeBinding);
if( newLevel == IMPOSSIBLE_MATCH ) {
String qualNameFromBinding = typeBinding.getQualifiedName();
if( !qualNameFromBinding.contains(".") && this.pattern.qualification != null && this.pattern.qualification.length > 0 ) {
// we need to find out if we import this thing at all
org.eclipse.jdt.core.dom.CompilationUnit cu = findCU(node);
List imports = cu.imports();
for( Object id : imports) {
ImportDeclaration idd = (ImportDeclaration)id;
if( idd.getName() instanceof QualifiedName qn) {
if( qn.getName().toString().equals(qualNameFromBinding)) {
char[] qualifiedPattern = getQualifiedPattern(this.pattern.simpleName, this.pattern.qualification);
// we were imported as qualified name...
int level3 = resolveLevelForTypeSourceName(qualifiedPattern, qn.toString().toCharArray(), typeBinding);
if( level3 == ACCURATE_MATCH ) {
return INACCURATE_MATCH;
}
return level3;
}
}

}
}
}
return newLevel;
}
return IMPOSSIBLE_MATCH;
}

private org.eclipse.jdt.core.dom.CompilationUnit findCU(org.eclipse.jdt.core.dom.ASTNode node) {
if( node == null )
return null;
if( node instanceof org.eclipse.jdt.core.dom.CompilationUnit cu) {
return cu;
}
return findCU(node.getParent());
}

}

0 comments on commit b9c0918

Please sign in to comment.