Skip to content

Commit

Permalink
Various fixes to search test suite, mostly regarding matching Name ob…
Browse files Browse the repository at this point in the history
…jects

Signed-off-by: Rob Stryker <stryker@redhat.com>
  • Loading branch information
Rob Stryker committed Oct 23, 2024
1 parent 39a1c03 commit bc349f2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
import org.eclipse.jdt.core.dom.EnumDeclaration;
Expand All @@ -29,28 +32,9 @@
import org.eclipse.jdt.core.dom.VariableDeclaration;
import org.eclipse.jdt.core.search.FieldDeclarationMatch;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration;
import org.eclipse.jdt.internal.compiler.ast.CompactConstructorDeclaration;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
import org.eclipse.jdt.internal.compiler.ast.NameReference;
import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
import org.eclipse.jdt.internal.compiler.ast.Reference;
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.*;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
import org.eclipse.jdt.internal.compiler.lookup.ParameterizedFieldBinding;
import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.*;
import org.eclipse.jdt.internal.compiler.util.SimpleSet;
import org.eclipse.jdt.internal.core.JavaElement;

Expand Down Expand Up @@ -287,11 +271,42 @@ public int match(Name name, MatchingNodeSet nodeSet) {
if (this.pattern.findDeclarations) {
return IMPOSSIBLE_MATCH; // already caught by match(VariableDeclaration)
}

if (matchesName(this.pattern.name, name.toString().toCharArray())) {
if( this.pattern instanceof DeclarationOfAccessedFieldsPattern doafp) {
if( doafp.enclosingElement != null ) {
// we have an enclosing element to check
if( !isWithinRange(name, doafp.enclosingElement) ) {
return IMPOSSIBLE_MATCH;
}
// We need to report the declaration, not the usage
// TODO testDeclarationOfAccessedFields2
// IBinding b = name.resolveBinding();
}
}
return nodeSet.addMatch(name, this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH);
}
return IMPOSSIBLE_MATCH;
}

public boolean isWithinRange(org.eclipse.jdt.core.dom.ASTNode node, IJavaElement el) {
if( el instanceof ISourceReference isr) {
try {
ISourceRange r = isr.getSourceRange();
if( r != null ) {
int astStart = node.getStartPosition();
int astLen = node.getLength();
int rangeStart = r.getOffset();
int rangeLen = r.getLength();
return astStart >= rangeStart && (astStart + astLen) <= (rangeStart + rangeLen);
}
} catch(JavaModelException jme) {
// Ignore
}
}
return false;
}

@Override
protected void matchReportReference(ASTNode reference, IJavaElement element, IJavaElement localElement, IJavaElement[] otherElements,Binding elementBinding, int accuracy, MatchLocator locator) throws CoreException {
if (this.isDeclarationOfAccessedFieldsPattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,8 @@ private SearchMatch toMatch(org.eclipse.jdt.core.dom.ASTNode node, int accuracy,
}
}
if (node instanceof MethodInvocation method) {
return new MethodReferenceMatch(DOMASTNodeUtils.getEnclosingJavaElement(node.getParent()), accuracy, method.getName().getStartPosition(), method.getStartPosition() + method.getLength() - method.getName().getStartPosition(), false, method.resolveMethodBinding().isSynthetic(), false, insideDocComment(node), getParticipant(), resource);
IJavaElement enclosing = DOMASTNodeUtils.getEnclosingJavaElement(node.getParent());
return new MethodReferenceMatch(enclosing, accuracy, method.getName().getStartPosition(), method.getStartPosition() + method.getLength() - method.getName().getStartPosition(), false, method.resolveMethodBinding().isSynthetic(), false, insideDocComment(node), getParticipant(), resource);
}
if (node instanceof SuperMethodInvocation method) {
return new MethodReferenceMatch(DOMASTNodeUtils.getEnclosingJavaElement(node.getParent()), accuracy, method.getName().getStartPosition(), method.getStartPosition() + method.getLength() - method.getName().getStartPosition(), false, method.resolveMethodBinding().isSynthetic(), true, insideDocComment(node), getParticipant(), resource);
Expand All @@ -1427,17 +1428,20 @@ private SearchMatch toMatch(org.eclipse.jdt.core.dom.ASTNode node, int accuracy,
if (node instanceof Name name) {
IBinding b = name.resolveBinding();
IJavaElement enclosing = DOMASTNodeUtils.getEnclosingJavaElement(node);
if( b == null ) {
return new SearchMatch(enclosing, accuracy, node.getStartPosition(), node.getLength(), getParticipant(), resource);
}
if (b instanceof ITypeBinding) {
return new TypeReferenceMatch(enclosing, accuracy, node.getStartPosition(), node.getLength(), insideDocComment(node), getParticipant(), resource);
}
if (b instanceof IVariableBinding variable) {
if (variable.isField()) {
return new FieldReferenceMatch(DOMASTNodeUtils.getEnclosingJavaElement(node), accuracy, node.getStartPosition(), node.getLength(), true, true, insideDocComment(node), getParticipant(), resource);
return new FieldReferenceMatch(enclosing, accuracy, node.getStartPosition(), node.getLength(), true, true, insideDocComment(node), getParticipant(), resource);
}
return new LocalVariableReferenceMatch(DOMASTNodeUtils.getEnclosingJavaElement(node), accuracy, node.getStartPosition(), node.getLength(), true, true, insideDocComment(node), getParticipant(), resource);
return new LocalVariableReferenceMatch(enclosing, accuracy, node.getStartPosition(), node.getLength(), true, true, insideDocComment(node), getParticipant(), resource);
}
if (b instanceof IPackageBinding) {
return new PackageReferenceMatch(DOMASTNodeUtils.getEnclosingJavaElement(name), accuracy, name.getStartPosition(), name.getLength(), insideDocComment(name), getParticipant(), resource);
return new PackageReferenceMatch(enclosing, accuracy, name.getStartPosition(), name.getLength(), insideDocComment(name), getParticipant(), resource);
}
// more...?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,12 @@ protected int resolveLevelForType(char[] qualifiedPattern, ITypeBinding type) {
return prev;
}
// NOTE: if case insensitive search then qualifiedPattern is assumed to be lowercase

return CharOperation.match(qualifiedPattern, type.getQualifiedName().toCharArray(), this.isCaseSensitive)
? ACCURATE_MATCH
: IMPOSSIBLE_MATCH;
char[] qualifiedNameFromBinding = type.getQualifiedName().toCharArray();
if( qualifiedNameFromBinding == null || qualifiedNameFromBinding.length == 0 ) {
qualifiedNameFromBinding = type.getName().toCharArray();
}
boolean match1 = CharOperation.match(qualifiedPattern, qualifiedNameFromBinding, this.isCaseSensitive);
return match1 ? ACCURATE_MATCH : IMPOSSIBLE_MATCH;
}
/* (non-Javadoc)
* Resolve level for type with a given binding with all pattern information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public boolean visit(SimpleName node) {
}
int level = this.patternLocator.match(node, this.nodeSet);
if ((level & PatternLocator.MATCH_LEVEL_MASK) == PatternLocator.POSSIBLE_MATCH && (this.nodeSet.mustResolve || this.patternLocator.mustResolve)) {
level = this.patternLocator.resolveLevel(node.resolveBinding());
IBinding b = node.resolveBinding();
level = this.patternLocator.resolveLevel(b);
}
this.nodeSet.addMatch(node, level);
return level == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.core.dom.BreakStatement;
import org.eclipse.jdt.core.dom.LabeledStatement;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchPattern;
Expand Down Expand Up @@ -119,12 +121,65 @@ public int match(Name name, MatchingNodeSet nodeSet) {
if (name.getParent() instanceof AbstractTypeDeclaration) {
return IMPOSSIBLE_MATCH;
}
if( name.getParent() instanceof LabeledStatement ls && ls.getLabel() == name) {
return IMPOSSIBLE_MATCH;
}
if( name.getParent() instanceof BreakStatement bs && bs.getLabel() == name) {
return IMPOSSIBLE_MATCH;
}
if (this.pattern.simpleName == null) {
return nodeSet.addMatch(name, this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH);
}
String simpleName = name instanceof SimpleName sname ? sname.getIdentifier() :
name instanceof QualifiedName qname ? qname.getName().getIdentifier() :
null;
if( name instanceof SimpleName sn2 ) {
if( this.pattern.qualification == null)
return match(sn2, nodeSet);
// searching for a qualified name but we are only simple
org.eclipse.jdt.core.dom.ASTNode parent3 = name.getParent();
if( !(parent3 instanceof QualifiedName)) {
return match(sn2, nodeSet);
}
// Parent is a qualified name and we didn't match it...
// so we know the whole name was a failed match, but...
if( parent3 instanceof QualifiedName qn3 && qn3.getQualifier() == name) {
// Maybe the qualifier is the type we're looking for
if( match(sn2, nodeSet) == POSSIBLE_MATCH) {
return POSSIBLE_MATCH;
}
}

if( this.pattern.getMatchMode() == SearchPattern.R_EXACT_MATCH) {
return IMPOSSIBLE_MATCH;
}
if( match(sn2, nodeSet) == POSSIBLE_MATCH) {
return POSSIBLE_MATCH;
}
return IMPOSSIBLE_MATCH;
}
if( name instanceof QualifiedName qn2 ) {
return match(qn2, nodeSet);
}
return IMPOSSIBLE_MATCH;
}

public int match(SimpleName name, MatchingNodeSet nodeSet) {
String simpleName = name.getIdentifier();
return simpleName != null && matchesName(this.pattern.simpleName, simpleName.toCharArray()) ?
POSSIBLE_MATCH : IMPOSSIBLE_MATCH;
}
public int match(QualifiedName name, MatchingNodeSet nodeSet) {
String simpleName = name.getName().getIdentifier();
String qualifier = name.getQualifier().toString();
if( this.pattern.qualification == null ) {
// Return an impossible match here, because we are not seeking a qualifier.
// The SimpleName node should be the one to respond.
return IMPOSSIBLE_MATCH;
}
if( qualifier != null) {
String desiredQualifier = new String(this.pattern.qualification);
if( !qualifier.equals(desiredQualifier)) {
return IMPOSSIBLE_MATCH;
}
}
return simpleName != null && matchesName(this.pattern.simpleName, simpleName.toCharArray()) ?
POSSIBLE_MATCH : IMPOSSIBLE_MATCH;
}
Expand Down

0 comments on commit bc349f2

Please sign in to comment.