diff --git a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11Parser.java b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11Parser.java index 55afa2ec85..0fe766a526 100644 --- a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11Parser.java +++ b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11Parser.java @@ -109,6 +109,7 @@ private ReloadableJava11Parser( // https://docs.oracle.com/en/java/javacard/3.1/guide/setting-java-compiler-options.html Options.instance(context).put("-g", "-g"); Options.instance(context).put("-proc", "none"); + Options.instance(context).put("-parameters", "true"); // Ensure type attribution continues despite errors in individual files or nodes. // If an error occurs in a single file or node, type attribution should still proceed diff --git a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11ParserVisitor.java b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11ParserVisitor.java index 20c8b26c6e..48f290e55d 100644 --- a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11ParserVisitor.java +++ b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11ParserVisitor.java @@ -467,7 +467,7 @@ public J visitClass(ClassTree node, Space fmt) { List membersMultiVariablesSeparated = new ArrayList<>(node.getMembers().size()); for (Tree m : node.getMembers()) { - // skip lomobok generated trees + // skip lombok generated trees if (isLombokGenerated(m)) { continue; } @@ -1938,7 +1938,6 @@ private static boolean isLombokGenerated(Tree t) { Tree tree = (t instanceof JCAnnotation) ? ((JCAnnotation) t).getAnnotationType() : t; Symbol sym = extractSymbol(tree); - if (sym == null) { // not a symbol we can check return false; diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java index 591f94e62f..73f23946e1 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java @@ -109,6 +109,7 @@ private ReloadableJava17Parser( // https://docs.oracle.com/en/java/javacard/3.1/guide/setting-java-compiler-options.html Options.instance(context).put("-g", "-g"); Options.instance(context).put("-proc", "none"); + Options.instance(context).put("-parameters", "true"); // Ensure type attribution continues despite errors in individual files or nodes. // If an error occurs in a single file or node, type attribution should still proceed diff --git a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21Parser.java b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21Parser.java index 1905bdcff2..fb4e271213 100644 --- a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21Parser.java +++ b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21Parser.java @@ -109,6 +109,7 @@ private ReloadableJava21Parser( // https://docs.oracle.com/en/java/javacard/3.1/guide/setting-java-compiler-options.html Options.instance(context).put("-g", "-g"); Options.instance(context).put("-proc", "none"); + Options.instance(context).put("-parameters", "true"); // Ensure type attribution continues despite errors in individual files or nodes. // If an error occurs in a single file or node, type attribution should still proceed diff --git a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8Parser.java b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8Parser.java index ce0425a772..15f05bb8ed 100644 --- a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8Parser.java +++ b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8Parser.java @@ -102,6 +102,7 @@ class ReloadableJava8Parser implements JavaParser { // https://docs.oracle.com/en/java/javacard/3.1/guide/setting-java-compiler-options.html Options.instance(context).put("-g", "-g"); Options.instance(context).put("-proc", "none"); + Options.instance(context).put("-parameters", "true"); // Ensure type attribution continues despite errors in individual files or nodes. // If an error occurs in a single file or node, type attribution should still proceed diff --git a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8ParserVisitor.java b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8ParserVisitor.java index 4030dc3847..8dce53cfb3 100644 --- a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8ParserVisitor.java +++ b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8ParserVisitor.java @@ -520,7 +520,6 @@ public J visitCompilationUnit(CompilationUnitTree node, Space fmt) { packageDecl = new J.Package(randomId(), packagePrefix, Markers.EMPTY, convert(cu.getPackageName()), packageAnnotations); } - return new J.CompilationUnit( randomId(), fmt, @@ -912,10 +911,11 @@ public J visitMethodInvocation(MethodInvocationTree node, Space fmt) { singletonList(padRight(new J.Empty(randomId(), sourceBefore(")"), Markers.EMPTY), EMPTY)) : convertAll(node.getArguments(), commaDelim, t -> sourceBefore(")")), Markers.EMPTY); - Symbol genericSymbol = (jcSelect instanceof JCFieldAccess) ? ((JCFieldAccess) jcSelect).sym : ((JCIdent) jcSelect).sym; + Symbol methodSymbol = (jcSelect instanceof JCFieldAccess) ? ((JCFieldAccess) jcSelect).sym : + ((JCIdent) jcSelect).sym; return new J.MethodInvocation(randomId(), fmt, Markers.EMPTY, select, typeParams, name, args, - typeMapping.methodInvocationType(jcSelect.type, genericSymbol)); + typeMapping.methodInvocationType(jcSelect.type, methodSymbol)); } @Override @@ -1701,9 +1701,6 @@ private boolean isEnum(Tree t) { } private @Nullable JRightPadded convert(@Nullable Tree t, Function suffix) { - if (t == null) { - return null; - } return convert(t, suffix, j -> Markers.EMPTY); } @@ -1942,9 +1939,9 @@ private static boolean isLombokGenerated(Tree t) { } return isLombokAnnotationType(sym.getQualifiedName().toString()) || - sym.getDeclarationAttributes().stream() - .map(c -> c.type.toString()) - .anyMatch(ReloadableJava8ParserVisitor::isLombokAnnotationType); + sym.getDeclarationAttributes().stream() + .map(a -> a.type.toString()) + .anyMatch(ReloadableJava8ParserVisitor::isLombokAnnotationType); } private static boolean isLombokAnnotationType(String name) { diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/MethodInvocationTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/MethodInvocationTest.java index 6dd52ae024..2690db6d72 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/MethodInvocationTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/MethodInvocationTest.java @@ -17,7 +17,10 @@ import org.junit.jupiter.api.Test; import org.openrewrite.ExecutionContext; +import org.openrewrite.Issue; import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaParser; +import org.openrewrite.java.MinimumJava11; import org.openrewrite.test.RewriteTest; import java.util.Optional; @@ -132,4 +135,28 @@ void m() { ) ); } + + @Issue("https://github.com/openrewrite/rewrite/issues/5400") // Fails on Java 8, but passes on Java 11 and later + @MinimumJava11 + @Test + void methodParameterNamesPresent() { + rewriteRun( + spec -> spec.parser(JavaParser.fromJavaVersion().classpath("assertj-core")), + java( + """ + import static org.assertj.core.api.Assertions.assertThat; + class Regular { + void method(String exp, String act) { + assertThat(act).isEqualTo(exp); + } + } + """, + spec -> spec.beforeRecipe(cu -> { + J.MethodDeclaration md = (J.MethodDeclaration) cu.getClasses().get(0).getBody().getStatements().get(0); + J.MethodInvocation mi = (J.MethodInvocation) md.getBody().getStatements().get(0); + assertThat(mi.getMethodType().getParameterNames()).containsExactly("expected"); + }) + ) + ); + } }