Skip to content

Commit

Permalink
enhancing Java 17 parser (will port to others later) to recover metho…
Browse files Browse the repository at this point in the history
…d type attribution when arg types are missing if there's only one valid signature matching the method name and arg count
  • Loading branch information
nmck257 committed Oct 18, 2024
1 parent 9da3321 commit 3e30d1e
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.DocCommentTable;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
Expand Down Expand Up @@ -933,8 +934,27 @@ public J visitMethodInvocation(MethodInvocationTree node, Space fmt) {
Symbol methodSymbol = (jcSelect instanceof JCFieldAccess) ? ((JCFieldAccess) jcSelect).sym :
((JCIdent) jcSelect).sym;

Type jcSelectType = jcSelect.type;
if (jcSelectType == null && jcSelect instanceof JCFieldAccess &&
hasOneMatchingMethod(((JCFieldAccess) jcSelect).selected.type, name.getSimpleName(), node.getArguments().size())) {
jcSelectType = methodSymbol.type;
}

return new J.MethodInvocation(randomId(), fmt, Markers.EMPTY, select, typeParams, name, args,
typeMapping.methodInvocationType(jcSelect.type, methodSymbol));
typeMapping.methodInvocationType(jcSelectType, methodSymbol));
}

private boolean hasOneMatchingMethod(Type methodSelectType, String name, int argCount) {
Symbol methodSymbol = null;
Type classType = methodSelectType;
while (!classType.equals(Type.noType)) {
for (var sym : classType.tsym.members().getSymbols(sym -> sym.name.toString().equals(name) && ((Symbol.MethodSymbol) sym).params().size() == argCount)) {
if (methodSymbol != null) return false;
methodSymbol = sym;
}
classType = ((Type.ClassType) classType).supertype_field;
}
return methodSymbol != null;
}

@Override
Expand Down
1 change: 1 addition & 0 deletions rewrite-java-tck/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {

dependencies {
implementation("org.assertj:assertj-core:latest.release")
implementation("org.junit-pioneer:junit-pioneer:2.0.0")
implementation(project(":rewrite-java"))
implementation(project(":rewrite-java-test"))
implementation(project(":rewrite-test"))
Expand Down
Loading

0 comments on commit 3e30d1e

Please sign in to comment.