Skip to content

Commit

Permalink
Handle another corner case for QualifiedName
Browse files Browse the repository at this point in the history
Even if it's technically a field access, if all the segments are simple
names without any class names, method invocations, or keywords,
then it should be treated as a qualified name for the purpose of
converting to the JDT AST.

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Jun 11, 2024
1 parent 08e372b commit 59bb19e
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,30 @@ private Expression convertExpressionImpl(JCExpression javac) {
commonSettings(res, javac);
return res;
}
useQualifiedName: if (fieldAccess.getExpression() instanceof JCFieldAccess parentFieldAccess) {
JCFieldAccess cursor = parentFieldAccess;
if (Objects.equals(Names.instance(this.context)._class, cursor.getIdentifier())
|| Objects.equals(Names.instance(this.context)._this, cursor.getIdentifier())
|| Objects.equals(Names.instance(this.context)._super, cursor.getIdentifier())) {
break useQualifiedName;
}
while (cursor.getExpression() instanceof JCFieldAccess newParent) {
cursor = newParent;
if (Objects.equals(Names.instance(this.context)._class, cursor.getIdentifier())
|| Objects.equals(Names.instance(this.context)._this, cursor.getIdentifier())
|| Objects.equals(Names.instance(this.context)._super, cursor.getIdentifier())) {
break useQualifiedName;
}
}

if (cursor.getExpression() instanceof JCIdent oldestIdentifier
&& !Objects.equals(Names.instance(this.context)._class, oldestIdentifier.getName())
&& !Objects.equals(Names.instance(this.context)._this, oldestIdentifier.getName())
&& !Objects.equals(Names.instance(this.context)._super, oldestIdentifier.getName())) {
// all segments are simple names
return convertQualifiedName(fieldAccess);
}
}
FieldAccess res = this.ast.newFieldAccess();
commonSettings(res, javac);
res.setExpression(convertExpression(fieldAccess.getExpression()));
Expand Down Expand Up @@ -1558,6 +1582,29 @@ private Expression convertExpressionImpl(JCExpression javac) {
return null;
}

/**
* precondition: you've checked all the segments are identifier that can be used in a qualified name
*/
private Name convertQualifiedName(JCFieldAccess fieldAccess) {
JCExpression parent = fieldAccess.getExpression();
Name parentName;
if (parent instanceof JCFieldAccess parentFieldAccess) {
parentName = convertQualifiedName(parentFieldAccess);
} else if (parent instanceof JCIdent parentIdent) {
parentName = convertName(parentIdent.getName());
} else {
throw new IllegalArgumentException("Unrecognized javac AST node type: " + parent.getClass().getCanonicalName());
}
commonSettings(parentName, parent);
SimpleName segmentName = (SimpleName)convertName(fieldAccess.getIdentifier());
int endPos = fieldAccess.getEndPosition(this.javacCompilationUnit.endPositions);
int startPos = endPos - fieldAccess.getIdentifier().length();
segmentName.setSourceRange(startPos, fieldAccess.getIdentifier().length());
QualifiedName res = this.ast.newQualifiedName(parentName, segmentName);
commonSettings(res, fieldAccess);
return res;
}

private Expression convertExpression(JCExpression javac) {
Expression ret = convertExpressionImpl(javac);
if( ret != null )
Expand Down

0 comments on commit 59bb19e

Please sign in to comment.