Skip to content

Commit

Permalink
Fix testDeclarationOfAccessedFields 1 through 4
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 12, 2024
1 parent 6dd1174 commit 96d7323
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ protected int matchField(IVariableBinding field, boolean matchName) {
if (field == null) return INACCURATE_MATCH;
if (!field.isField()) return IMPOSSIBLE_MATCH;

if (matchName && !matchesName(this.pattern.name, field.getName().toCharArray())) return IMPOSSIBLE_MATCH;
if (matchName && !matchesName(this.pattern.name, field.getName().toCharArray()))
return IMPOSSIBLE_MATCH;

FieldPattern fieldPattern = (FieldPattern)this.pattern;
ITypeBinding receiverBinding = field.getDeclaringClass();
Expand All @@ -233,10 +234,21 @@ protected int matchField(IVariableBinding field, boolean matchName) {

// Note there is no dynamic lookup for field access
int declaringLevel = resolveLevelForType(fieldPattern.declaringSimpleName, fieldPattern.declaringQualification, receiverBinding);
if (declaringLevel == IMPOSSIBLE_MATCH) return IMPOSSIBLE_MATCH;
if (declaringLevel == IMPOSSIBLE_MATCH)
return IMPOSSIBLE_MATCH;

// look at field type only if declaring type is not specified
if (fieldPattern.declaringSimpleName == null) return declaringLevel;
if (fieldPattern.declaringSimpleName == null) {
if( this.isDeclarationOfAccessedFieldsPattern && this.pattern instanceof DeclarationOfAccessedFieldsPattern doafp) {
IJavaElement je = field.getJavaElement();
if( je != null ) {
doafp.knownFields.add(je);
}
} else {
return declaringLevel;
}
return IMPOSSIBLE_MATCH;
}

// get real field binding
// TODO what is a ParameterizedFieldBinding?
Expand All @@ -246,7 +258,16 @@ protected int matchField(IVariableBinding field, boolean matchName) {
// }

int typeLevel = resolveLevelForType(field.getType());
return declaringLevel > typeLevel ? typeLevel : declaringLevel; // return the weaker match
int ret = declaringLevel > typeLevel ? typeLevel : declaringLevel; // return the weaker match
if( this.isDeclarationOfAccessedFieldsPattern && this.pattern instanceof DeclarationOfAccessedFieldsPattern doafp) {
IJavaElement je = field.getJavaElement();
if( je != null ) {
doafp.knownFields.add(je);
}
} else {
return ret;
}
return IMPOSSIBLE_MATCH;
}
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.core.search.matching.PatternLocator#matchLevelAndReportImportRef(org.eclipse.jdt.internal.compiler.ast.ImportReference, org.eclipse.jdt.internal.compiler.lookup.Binding, org.eclipse.jdt.internal.core.search.matching.MatchLocator)
Expand All @@ -273,22 +294,53 @@ protected void matchReportReference(ASTNode reference, IJavaElement element, Bin
}
@Override
public int match(Name name, MatchingNodeSet nodeSet, MatchLocator locator) {
if (this.pattern.findDeclarations || this.isDeclarationOfAccessedFieldsPattern) {
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( this.isDeclarationOfAccessedFieldsPattern && 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();
IBinding b = name.resolveBinding();
IJavaElement je = b == null ? null : b.getJavaElement();
if( je != null && doafp.knownFields.includes(je)) {
doafp.knownFields.remove(je);
ISourceReference sr = je instanceof ISourceReference ? (ISourceReference)je : null;
IResource r = null;
ISourceRange srg = null;
String elName = je.getElementName();
try {
srg = sr.getSourceRange();
IJavaElement ancestor = je.getAncestor(IJavaElement.COMPILATION_UNIT);
r = ancestor == null ? null : ancestor.getCorrespondingResource();
} catch(JavaModelException jme) {
// ignore
}
if( srg != null ) {
int accuracy = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
FieldDeclarationMatch fdMatch = new FieldDeclarationMatch(
je,
accuracy,
srg.getOffset() + srg.getLength() - elName.length() - 1,
elName.length(),
locator.getParticipant(), r);
try {
locator.report(fdMatch);
} catch(CoreException ce) {
// ignore
}
}
}
return IMPOSSIBLE_MATCH;
}
}

return nodeSet.addMatch(name, this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH);
}
return IMPOSSIBLE_MATCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,7 @@ public void acceptAST(org.eclipse.jdt.core.ICompilationUnit source, org.eclipse.
if( possibleMatches[i] == pm ) {
domUnits[i] = ast;
nonNullDomIndexes.add(i);
MatchLocator.this.currentPossibleMatch = pm;
ast.accept(new PatternLocatorVisitor(MatchLocator.this.patternLocator, possibleMatches[i].nodeSet, MatchLocator.this));
return;
}
Expand Down

0 comments on commit 96d7323

Please sign in to comment.