diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java index 13664d53840..9d4b897a0dc 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java @@ -26,6 +26,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import java.util.Optional; @@ -189,7 +190,8 @@ void onlyIfUsingCompileScope(String onlyIfUsing) { @ValueSource(strings = {"com.google.common.math.*", "com.google.common.math.IntMath"}) void onlyIfUsingMultipleScopes(String onlyIfUsing) { rewriteRun( - spec -> spec.recipe(addDependency("com.google.guava:guava:29.0-jre", onlyIfUsing)), + spec -> spec.recipe(addDependency("com.google.guava:guava:29.0-jre", onlyIfUsing)) + .typeValidationOptions(TypeValidation.none()), mavenProject("project", srcMainJava( java(usingGuavaIntMath) @@ -230,7 +232,8 @@ void onlyIfUsingMultipleScopes(String onlyIfUsing) { void usedInMultipleSourceSetsUsingExplicitSourceSet(String onlyIfUsing) { AddDependency addDep = new AddDependency("com.google.guava", "guava", "29.0-jre", null, null, onlyIfUsing, null, null, null, Boolean.TRUE); rewriteRun( - spec -> spec.recipe(addDep), + spec -> spec.recipe(addDep) + .typeValidationOptions(TypeValidation.none()), mavenProject("project", srcMainJava( java(usingGuavaIntMath) @@ -287,7 +290,8 @@ void usedInMultipleSourceSetsUsingExplicitSourceSet(String onlyIfUsing) { void usedInTransitiveSourceSet() { AddDependency addDep = new AddDependency("com.google.guava", "guava", "29.0-jre", null, null, "com.google.common.math.IntMath", null, null, null, Boolean.TRUE); rewriteRun( - spec -> spec.recipe(addDep), + spec -> spec.recipe(addDep) + .typeValidationOptions(TypeValidation.none()), mavenProject("project", srcSmokeTestJava( java(usingGuavaIntMath) @@ -341,7 +345,8 @@ void usedInTransitiveSourceSet() { void addDependencyIfNotUsedInATransitive() { AddDependency addDep = new AddDependency("com.google.guava", "guava", "29.0-jre", null, null, "com.google.common.math.IntMath", null, null, null, Boolean.TRUE); rewriteRun( - spec -> spec.recipe(addDep), + spec -> spec.recipe(addDep) + .typeValidationOptions(TypeValidation.none()), mavenProject("project", srcSmokeTestJava( java(usingGuavaIntMath) diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TaskTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TaskTest.java index 939daef1d49..d52ed0f61cd 100755 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TaskTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TaskTest.java @@ -28,7 +28,7 @@ class TaskTest implements RewriteTest { @Test void declareTaskOldStyle() { rewriteRun( - spec -> spec.typeValidationOptions(TypeValidation.none()), + spec -> spec.afterTypeValidationOptions(TypeValidation.none()), buildGradle( """ task(testWithCloud, type: Test) { @@ -44,7 +44,7 @@ void declareTaskOldStyle() { @Test void testDsl() { rewriteRun( - spec -> spec.typeValidationOptions(TypeValidation.none()), + spec -> spec.afterTypeValidationOptions(TypeValidation.none()), buildGradle( """ plugins { diff --git a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java index c0b59decf81..be568dbbbf2 100644 --- a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java +++ b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java @@ -686,7 +686,11 @@ private JavaType.Method methodReferenceType(DCTree.DCReference ref, @Nullable Ja for (JavaType testParamType : method.getParameterTypes()) { Type paramType = attr.attribType(param, symbol); if (testParamType instanceof JavaType.GenericTypeVariable) { - for (JavaType bound : ((JavaType.GenericTypeVariable) testParamType).getBounds()) { + List bounds = ((JavaType.GenericTypeVariable) testParamType).getBounds(); + if (bounds.isEmpty() && paramType.tsym != null && "java.lang.Object".equals(paramType.tsym.getQualifiedName().toString())) { + return method; + } + for (JavaType bound : bounds) { if (paramTypeMatches(bound, paramType)) { return method; } diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java index 4db1407edb8..8a6fa49bd6d 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java @@ -690,7 +690,11 @@ private JavaType.Method methodReferenceType(DCTree.DCReference ref, @Nullable Ja for (JavaType testParamType : method.getParameterTypes()) { Type paramType = attr.attribType(param, symbol); if (testParamType instanceof JavaType.GenericTypeVariable) { - for (JavaType bound : ((JavaType.GenericTypeVariable) testParamType).getBounds()) { + List bounds = ((JavaType.GenericTypeVariable) testParamType).getBounds(); + if (bounds.isEmpty() && paramType.tsym != null && "java.lang.Object".equals(paramType.tsym.getQualifiedName().toString())) { + return method; + } + for (JavaType bound : bounds) { if (paramTypeMatches(bound, paramType)) { return method; } diff --git a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java index d05a6d1ce07..464a1aa9511 100644 --- a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java +++ b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java @@ -690,7 +690,11 @@ private JavaType.Method methodReferenceType(DCTree.DCReference ref, @Nullable Ja for (JavaType testParamType : method.getParameterTypes()) { Type paramType = attr.attribType(param, symbol); if (testParamType instanceof JavaType.GenericTypeVariable) { - for (JavaType bound : ((JavaType.GenericTypeVariable) testParamType).getBounds()) { + List bounds = ((JavaType.GenericTypeVariable) testParamType).getBounds(); + if (bounds.isEmpty() && paramType.tsym != null && "java.lang.Object".equals(paramType.tsym.getQualifiedName().toString())) { + return method; + } + for (JavaType bound : bounds) { if (paramTypeMatches(bound, paramType)) { return method; } diff --git a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java index 6c393d58a9d..0c641775262 100644 --- a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java +++ b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java @@ -648,7 +648,11 @@ private JavaType.Method methodReferenceType(DCTree.DCReference ref, @Nullable Ja for (JavaType testParamType : method.getParameterTypes()) { Type paramType = attr.attribType(param, symbol); if (testParamType instanceof JavaType.GenericTypeVariable) { - for (JavaType bound : ((JavaType.GenericTypeVariable) testParamType).getBounds()) { + List bounds = ((JavaType.GenericTypeVariable) testParamType).getBounds(); + if (bounds.isEmpty() && paramType.tsym != null && "java.lang.Object".equals(paramType.tsym.getQualifiedName().toString())) { + return method; + } + for (JavaType bound : bounds) { if (paramTypeMatches(bound, paramType)) { return method; } diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/JavaParserTypeMappingTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/JavaParserTypeMappingTest.java index d5712c1a9dc..82c8ae86d4c 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/JavaParserTypeMappingTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/JavaParserTypeMappingTest.java @@ -25,6 +25,7 @@ import org.openrewrite.java.tree.JavaType.Parameterized; import org.openrewrite.java.tree.TypeUtils; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import java.util.concurrent.atomic.AtomicReference; @@ -140,8 +141,10 @@ class TypeC extends java.util.ArrayList { @Issue("https://github.com/openrewrite/rewrite/issues/1762") @Test + @MinimumJava11 void methodInvocationWithUnknownTypeSymbol() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().constructorInvocations(false).build()), java( """ import java.util.ArrayList; @@ -174,6 +177,7 @@ List method(List values) { @Test void methodInvocationOnUnknownType() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()), java( """ import java.util.ArrayList; @@ -237,7 +241,7 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations import java.util.List; import java.util.stream.Collectors; - @SuppressWarningsWarnings("ALL") + @SuppressWarnings("ALL") class MakeEasyToFind { void method(List multiMaps) { List ints; @@ -302,7 +306,7 @@ public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext executionContext) import java.util.List; import java.util.stream.Collectors; - @SuppressWarningsWarnings("ALL") + @SuppressWarnings("ALL") class MakeEasyToFind { void method(List multiMaps) { Object obj = multiMaps.stream() diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/MinimumJava11.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/MinimumJava11.java index 9869b248b3d..89ed1976357 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/MinimumJava11.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/MinimumJava11.java @@ -26,6 +26,6 @@ @Retention(RUNTIME) @Target({TYPE, METHOD}) -@Tag("java17") +@Tag("java11") public @interface MinimumJava11 { } diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/BinaryTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/BinaryTest.java index b38e0c2c969..1d062eee570 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/BinaryTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/BinaryTest.java @@ -63,6 +63,8 @@ void endOfLineBreaks() { rewriteRun( java( """ + import java.util.Objects; + class Test { void test() { boolean b = Objects.equals(1, 2) // diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/ForEachLoopTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/ForEachLoopTest.java index c07788e2a86..39430b714da 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/ForEachLoopTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/ForEachLoopTest.java @@ -45,9 +45,11 @@ void statementTerminatorForSingleLineForLoops() { java( """ class Test { - void test() { + void test(int[] n) { for(Integer i : n) test(); } + void test() { + } } """ ) diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/ForLoopTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/ForLoopTest.java index 54a4d68a7d3..d6a06277e3d 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/ForLoopTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/ForLoopTest.java @@ -110,7 +110,7 @@ void formatLoopNoInit() { java( """ class Test { - void test() { + void test(int i) { for ( ; i < 10 ; i++ ) {} } } diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/InstanceOfTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/InstanceOfTest.java index 27c6bff2dc8..ca986bd4289 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/InstanceOfTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/InstanceOfTest.java @@ -31,7 +31,7 @@ void instanceOf() { java( """ class Test { - void test() { + void test(Object o) { boolean b = o instanceof String; } } diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java index a233cc6c989..60251b5dd20 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java @@ -17,7 +17,9 @@ import org.junit.jupiter.api.Test; import org.openrewrite.Issue; +import org.openrewrite.java.MinimumJava11; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import static org.openrewrite.java.Assertions.java; @@ -389,6 +391,7 @@ class Test { @Test void exception() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()), java( """ public class A { @@ -721,6 +724,7 @@ List method() throws Exception { @Test void multipleReferenceParameters() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()), java( """ class Test { @@ -1064,6 +1068,7 @@ interface Test { @Test void methodNotFound() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().methodInvocations(false).build()), java( """ interface Test { @@ -1082,6 +1087,7 @@ interface Test { @Test void typeNotFound() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()), java( """ interface Test { @@ -1650,6 +1656,7 @@ void arrayTypeLiterals() { @Test @Issue("https://github.com/openrewrite/rewrite/issues/3530") + @MinimumJava11 void arrayTypeLiterals2() { rewriteRun( java("" + diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/LambdaTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/LambdaTest.java index 7903ec9bb4d..8299f71d333 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/LambdaTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/LambdaTest.java @@ -95,7 +95,7 @@ void multipleParameters() { rewriteRun( java( """ - import java.util.function.BiFunction; + import java.util.function.BiConsumer; class Test { void test() { BiConsumer a = (s1, s2) -> { }; diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/MemberReferenceTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/MemberReferenceTest.java index 0465415a250..8f5f319ae21 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/MemberReferenceTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/MemberReferenceTest.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Test; import org.openrewrite.Issue; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import static org.openrewrite.java.Assertions.java; @@ -28,6 +29,7 @@ class MemberReferenceTest implements RewriteTest { @Test void unknownDeclaringType() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).methodInvocations(false).build()), java( """ package com.company.pkg; @@ -130,11 +132,13 @@ static void func(String s) {} @Test void constructorMethodReference() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().methodInvocations(false).build()), java( """ import java.util.HashSet; -import java.util.Set; -import java.util.stream.Stream; + import java.util.Set; + import java.util.stream.Stream; + class Test { Stream n = Stream.of(1, 2); Set n2 = n.collect(HashSet < Integer > :: new, HashSet :: add); diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/NewClassTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/NewClassTest.java index a32be176483..9149abb362f 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/NewClassTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/NewClassTest.java @@ -84,6 +84,9 @@ void anonymousClass() { rewriteRun( java( """ + import java.util.ArrayList; + import java.util.List; + class Test { List l = new ArrayList() { /** Javadoc */ diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/TypeParameterAndWildcardTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/TypeParameterAndWildcardTest.java index dcc58444067..10fd235ecb5 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/TypeParameterAndWildcardTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/TypeParameterAndWildcardTest.java @@ -17,6 +17,7 @@ import org.junit.jupiter.api.Test; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import static org.openrewrite.java.Assertions.java; @@ -25,6 +26,7 @@ class TypeParameterAndWildcardTest implements RewriteTest { @Test void annotatedTypeParametersOnWildcardBounds() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()), java( """ import java.util.List; @@ -40,6 +42,7 @@ class A { @Test void annotatedTypeParametersOnReturnTypeExpression() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()), java( """ import java.util.List; diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/VariableDeclarationsTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/VariableDeclarationsTest.java index f4d26d77e02..d855d77df6f 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/VariableDeclarationsTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/VariableDeclarationsTest.java @@ -17,6 +17,7 @@ import org.junit.jupiter.api.Test; import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.MinimumJava11; import org.openrewrite.java.MinimumJava17; import org.openrewrite.test.RewriteTest; @@ -59,6 +60,7 @@ class Test { } @Test + @MinimumJava11 void finalVar() { rewriteRun( java( diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/AddImportTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/AddImportTest.java index 9636a2879b8..d80733df383 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/AddImportTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/AddImportTest.java @@ -840,15 +840,15 @@ void dontAddImportForStaticImportsIndirectlyReferenced() { spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() { @Override public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { - maybeAddImport("com.fasterxml.jackson.databind.ObjectMapper"); + maybeAddImport("java.io.File"); return super.visitCompilationUnit(cu, ctx); } - })).parser(JavaParser.fromJavaVersion().classpath("jackson-databind")), + })), java( """ - import com.fasterxml.jackson.databind.ObjectMapper; + import java.io.File; class Helper { - static ObjectMapper OBJECT_MAPPER; + static File FILE; } """ ), @@ -856,7 +856,7 @@ class Helper { """ class Test { void test() { - Helper.OBJECT_MAPPER.writer(); + Helper.FILE.exists(); } } """ diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateInstanceOfTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateInstanceOfTest.java index 508c986a46d..e477887d27b 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateInstanceOfTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateInstanceOfTest.java @@ -68,7 +68,7 @@ public J visitLiteral(J.Literal literal, ExecutionContext executionContext) { } })) // custom missing type validation - .typeValidationOptions(TypeValidation.none()) + .afterTypeValidationOptions(TypeValidation.none()) .afterRecipe(run -> run.getChangeset().getAllResults().forEach(r -> assertTypeAttribution((J) r.getAfter()))); } diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/MethodMatcherTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/MethodMatcherTest.java index 15979a2a0cd..d9113a61421 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/MethodMatcherTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/MethodMatcherTest.java @@ -23,6 +23,7 @@ import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import java.util.regex.Pattern; @@ -430,6 +431,7 @@ void test() { @Test void matcherForUnknownType() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.none()), java( """ class Test { diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/UnwrapParenthesesTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/UnwrapParenthesesTest.java index 4316e9267c6..df0b4d03714 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/UnwrapParenthesesTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/UnwrapParenthesesTest.java @@ -188,7 +188,9 @@ void keepParenthesesOnUnaryWithWrappedBinary() { rewriteRun( java( """ + import java.util.HashSet; public class A { + static HashSet set = new HashSet<>(); static boolean notEmpty = !(set == null || set.isEmpty()); } """ diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/format/MethodParamPadTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/format/MethodParamPadTest.java index 5b89cdde5e2..97186c8bcb9 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/format/MethodParamPadTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/format/MethodParamPadTest.java @@ -331,9 +331,11 @@ void recordWithCompactConstructor() { rewriteRun( version(java( """ + import java.util.Objects; + public record HttpClientTrafficLogData( - Request request, - Response response + String request, + String response ) { public HttpClientTrafficLogData diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/format/NoWhitespaceBeforeTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/format/NoWhitespaceBeforeTest.java index fcd5888a200..b8f099a6941 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/format/NoWhitespaceBeforeTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/format/NoWhitespaceBeforeTest.java @@ -30,6 +30,7 @@ import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import org.openrewrite.test.SourceSpec; +import org.openrewrite.test.TypeValidation; import java.util.Collections; import java.util.List; @@ -735,7 +736,8 @@ public static void main(String[] args) { @Test void doNotStripAnnotationArguments() { rewriteRun( - spec -> spec.parser(JavaParser.fromJavaVersion().styles(noWhitespaceBeforeStyle())), + spec -> spec.parser(JavaParser.fromJavaVersion().styles(noWhitespaceBeforeStyle())) + .typeValidationOptions(TypeValidation.none()), java( """ import org.graalvm.compiler.core.common.SuppressFBWarnings; diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/format/TabsAndIndentsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/format/TabsAndIndentsTest.java index acbd72321f5..8269b77ce67 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/format/TabsAndIndentsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/format/TabsAndIndentsTest.java @@ -945,7 +945,7 @@ class Test { @Test void moreAnnotations() { rewriteRun( - spec -> spec.typeValidationOptions(TypeValidation.none()), + spec -> spec.afterTypeValidationOptions(TypeValidation.none()), java( """ import lombok.EqualsAndHashCode; @@ -1678,6 +1678,7 @@ Test method(Function f) { @Test void failure1() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.none()), java( """ public class Test { @@ -2302,6 +2303,7 @@ void shiftRight() {} @Test void recordComponents() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.none()), java( """ public record RenameRequest( @@ -2320,7 +2322,7 @@ void enumConstants() { java( """ public enum WorkflowStatus { - @Nullable + @SuppressWarnings("ALL") VALUE1 } """ diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/MigrateJavaTemplateToRewrite8Test.java b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/MigrateJavaTemplateToRewrite8Test.java index 7d290a84c17..a7bff9a5b60 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/MigrateJavaTemplateToRewrite8Test.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/MigrateJavaTemplateToRewrite8Test.java @@ -148,6 +148,7 @@ void templaveVariable() { import lombok.Value; import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; + import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import java.util.List; @@ -194,6 +195,7 @@ public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext context) { import lombok.Value; import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; + import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import java.util.List; diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/MigrateMarkersSearchResultTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/MigrateMarkersSearchResultTest.java index e68efe8c24e..8df746d3668 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/MigrateMarkersSearchResultTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/MigrateMarkersSearchResultTest.java @@ -20,6 +20,7 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import static org.openrewrite.java.Assertions.java; @@ -38,6 +39,7 @@ public void defaults(RecipeSpec spec) { @Test void migrate() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.none()), java( """ package org.openrewrite.kubernetes.resource; diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/UpdateStaticAnalysisPackageTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/UpdateStaticAnalysisPackageTest.java index 82f76dc4922..81103058f63 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/UpdateStaticAnalysisPackageTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/UpdateStaticAnalysisPackageTest.java @@ -36,7 +36,7 @@ public void defaults(RecipeSpec spec) { .build() .activateRecipes("org.openrewrite.java.upgrade.UpdateStaticAnalysisPackage") ) - .typeValidationOptions(TypeValidation.none()); + .afterTypeValidationOptions(TypeValidation.none()); } @SuppressWarnings("all") @@ -44,6 +44,7 @@ public void defaults(RecipeSpec spec) { @Test void changeCleanUpToStaticanalysisForSpecificClassOnly() { rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.none()), java( """ package org.openrewrite.java.migrate; diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/style/AutodetectTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/style/AutodetectTest.java index 28b18b6bb6d..61cf8cef637 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/style/AutodetectTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/style/AutodetectTest.java @@ -427,20 +427,20 @@ public void method() { void rewriteImportLayout() { var cus = jp().parse( """ - import com.fasterxml.jackson.annotation.JsonCreator; - - import org.openrewrite.internal.StringUtils; - import org.openrewrite.internal.ListUtils; - import org.openrewrite.internal.lang.Nullable; - - import java.util.*; - import java.util.stream.Collectors; - - import static java.util.Collections.*; - import static java.util.function.Function.identity; - - public class Test { - } + import com.fasterxml.jackson.annotation.JsonCreator; + + import org.openrewrite.internal.StringUtils; + import org.openrewrite.internal.ListUtils; + import org.openrewrite.internal.lang.Nullable; + + import java.util.*; + import java.util.stream.Collectors; + + import static java.util.Collections.*; + import static java.util.function.Function.identity; + + public class Test { + } """ ); @@ -530,16 +530,16 @@ void staticImports() { void detectStarImport() { var cus = jp().parse( """ - import java.util.*; - - public class Test { - List l; - Set s; - Map m; - Collection c; - LinkedHashMap lhm; - HashSet integer; - } + import java.util.*; + + public class Test { + List l; + Set s; + Map m; + Collection c; + LinkedHashMap lhm; + HashSet integer; + } """ ); @@ -555,28 +555,28 @@ public class Test { void detectImportCounts() { var cus = jp().parse( """ - import java.util.ArrayList; - import java.util.Collections; - import java.util.HashSet; - import java.util.List; - import java.util.Set; - - import javax.persistence.Entity; - import javax.persistence.FetchType; - import javax.persistence.JoinColumn; - import javax.persistence.JoinTable; - import javax.persistence.ManyToMany; - import javax.persistence.Table; - import javax.xml.bind.annotation.XmlElement; - - public class Test { - List l; - Set s; - Map m; - Collection c; - LinkedHashMap lhm; - HashSet integer; - } + import java.util.ArrayList; + import java.util.Collections; + import java.util.HashSet; + import java.util.List; + import java.util.Set; + + import javax.persistence.Entity; + import javax.persistence.FetchType; + import javax.persistence.JoinColumn; + import javax.persistence.JoinTable; + import javax.persistence.ManyToMany; + import javax.persistence.Table; + import javax.xml.bind.annotation.XmlElement; + + public class Test { + List l; + Set s; + Map m; + Collection c; + LinkedHashMap lhm; + HashSet integer; + } """ ); @@ -593,11 +593,11 @@ public class Test { void detectMethodArgs() { var cus = jp().parse( """ - class Test { - void i() { - a("a" ,"b" ,"c" ,"d"); - } + class Test { + void i() { + a("a" ,"b" ,"c" ,"d"); } + } """ ); @@ -614,11 +614,11 @@ void i() { void detectMethodArgAfterComma() { var cus = jp().parse( """ - class Test { - void i() { - a("a", "b"); - } + class Test { + void i() { + a("a", "b"); } + } """ ); @@ -636,11 +636,11 @@ void i() { void detectColonInForEachLoop() { var cus = jp().parse( """ - class Test { - void i() { - for (int i : new int[]{}) {} - } + class Test { + void i() { + for (int i : new int[]{}) {} } + } """ ); @@ -656,11 +656,11 @@ void i() { void detectAfterTypeCast() { var cus = jp().parse( """ - class T { - { - String s = (String) getString(); - } + class T { + { + String s = (String) getString(); } + } """ ); @@ -698,11 +698,11 @@ void m() { void detectMethodArgsNoArgs() { var cus = jp().parse( """ - class Test { - void i() { - a(); - } + class Test { + void i() { + a(); } + } """ ); @@ -719,11 +719,11 @@ void i() { void detectMethodArgsNoSpaceForComma() { var cus = jp().parse( """ - class Test { - void i() { - a("a","b","c"); - } + class Test { + void i() { + a("a","b","c"); } + } """ ); @@ -740,11 +740,11 @@ void i() { void detectMethodArgsSpaceForComma() { var cus = jp().parse( """ - class Test { - void i() { - a("a" , "b" , "c"); - } + class Test { + void i() { + a("a" , "b" , "c"); } + } """ ); @@ -761,11 +761,11 @@ void i() { void detectAfterCommaInNewArray() { var cus = jp().parse( """ - class T { - static { - int[] i = new int[]{1, 2, 3, 4}; - } + class T { + static { + int[] i = new int[]{1, 2, 3, 4}; } + } """ ); @@ -783,14 +783,14 @@ class T { void detectAfterCommaShouldIgnoreFirstElement() { var cus = jp().parse( """ - class T { - static { - int[] i0 = new int[]{1, 2}; - int[] i1 = new int[]{2, 3}; - int[] i2 = new int[]{3, 4}; - int[] i3 = new int[]{4,5}; - } + class T { + static { + int[] i0 = new int[]{1, 2}; + int[] i1 = new int[]{2, 3}; + int[] i2 = new int[]{3, 4}; + int[] i3 = new int[]{4,5}; } + } """ ); @@ -808,18 +808,18 @@ class T { void detectAfterCommaBasedOnLambdas() { var cus = jp().parse( """ - import java.util.function.BiConsumer; - - class T { - static { - int[] i0 = new int[]{1,2}; - int[] i1 = new int[]{2,3}; - - BiConsumer c0 = (a, b) -> {}; - BiConsumer c1 = (a, b) -> {}; - BiConsumer c2 = (a, b) -> {}; - } + import java.util.function.BiConsumer; + + class T { + static { + int[] i0 = new int[]{1,2}; + int[] i1 = new int[]{2,3}; + + BiConsumer c0 = (a, b) -> {}; + BiConsumer c1 = (a, b) -> {}; + BiConsumer c2 = (a, b) -> {}; } + } """ ); @@ -837,13 +837,13 @@ class T { void detectNoSpacesWithinMethodCall() { var cus = jp().parse( """ - class Test { - void a(String a, String b, String c) { - } - void i() { - a("a","b","c"); - } + class Test { + void a(String a, String b, String c) { + } + void i() { + a("a","b","c"); } + } """ ); @@ -860,11 +860,11 @@ void i() { void detectSpacesWithinMethodCall() { var cus = jp().parse( """ - class Test { - void i() { - a( "a","b","c" ); - } + class Test { + void i() { + a( "a","b","c" ); } + } """ ); @@ -881,14 +881,14 @@ void i() { void detectElseWithNoNewLine() { var cus = jp().parse( """ - class Test { - void method(int n) { - if (n == 0) { - } else if (n == 1) { - } else { - } + class Test { + void method(int n) { + if (n == 0) { + } else if (n == 1) { + } else { } } + } """ ); @@ -905,16 +905,16 @@ void method(int n) { void detectElseOnNewLine() { var cus = jp().parse( """ - class Test { - void method(int n) { - if (n == 0) { - } - else if (n == 1) { - } - else { - } + class Test { + void method(int n) { + if (n == 0) { + } + else if (n == 1) { + } + else { } } + } """ ); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/Assertions.java b/rewrite-java/src/main/java/org/openrewrite/java/Assertions.java index d26bec85398..b4613d39548 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/Assertions.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/Assertions.java @@ -52,19 +52,14 @@ static void customizeExecutionContext(ExecutionContext ctx) { } } - // validateTypes and assertValidTypes can be merged into a single function once JavaRecipeTest is removed - static SourceFile validateTypes(SourceFile after, RecipeSpec testMethodSpec, RecipeSpec testClassSpec) { - if (after instanceof JavaSourceFile) { - TypeValidation typeValidation = testMethodSpec.getTypeValidation() != null ? testMethodSpec.getTypeValidation() : testClassSpec.getTypeValidation(); - if (typeValidation == null) { - typeValidation = new TypeValidation(); - } - assertValidTypes(typeValidation, (JavaSourceFile) after); + static SourceFile validateTypes(SourceFile source, TypeValidation typeValidation) { + if (source instanceof JavaSourceFile) { + assertValidTypes(typeValidation, (JavaSourceFile) source); } - return after; + return source; } - public static void assertValidTypes(TypeValidation typeValidation, J sf) { + private static void assertValidTypes(TypeValidation typeValidation, J sf) { if (typeValidation.identifiers() || typeValidation.methodInvocations() || typeValidation.methodDeclarations() || typeValidation.classDeclarations() || typeValidation.constructorInvocations()) { List missingTypeResults = FindMissingTypes.findMissingTypes(sf); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/search/FindMissingTypes.java b/rewrite-java/src/main/java/org/openrewrite/java/search/FindMissingTypes.java index 0d74f97a9a5..8446a636de6 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/search/FindMissingTypes.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/search/FindMissingTypes.java @@ -18,10 +18,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import org.openrewrite.*; -import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.JavaVisitor; -import org.openrewrite.java.JavadocVisitor; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.java.tree.JavaType; @@ -148,10 +145,19 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu public J.MemberReference visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) { J.MemberReference mr = super.visitMemberReference(memberRef, ctx); JavaType.Method type = mr.getMethodType(); - if (!isWellFormedType(type, seenTypes)) { - mr = SearchResult.found(mr, "MemberReference type is missing or malformed"); - } else if (!type.getName().equals(mr.getReference().getSimpleName()) && !type.isConstructor()) { - mr = SearchResult.found(mr, "type information has a different method name '" + type.getName() + "'"); + if (type != null) { + if (!isWellFormedType(type, seenTypes)) { + mr = SearchResult.found(mr, "MemberReference type is missing or malformed"); + } else if (!type.getName().equals(mr.getReference().getSimpleName()) && !type.isConstructor()) { + mr = SearchResult.found(mr, "type information has a different method name '" + type.getName() + "'"); + } + } else { + JavaType.Variable variableType = mr.getVariableType(); + if (!isWellFormedType(variableType, seenTypes)) { + mr = SearchResult.found(mr, "MemberReference type is missing or malformed"); + } else if (!variableType.getName().equals(mr.getReference().getSimpleName())) { + mr = SearchResult.found(mr, "type information has a different variable name '" + variableType.getName() + "'"); + } } return mr; } diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java b/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java index fd9853fdb03..555fa533e36 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java @@ -41,7 +41,7 @@ public static SourceSpecs pomXml(@Language("xml") @Nullable String before) { public static SourceSpecs pomXml(@Language("xml") @Nullable String before, Consumer> spec) { SourceSpec maven = new SourceSpec<>(Xml.Document.class, "maven", MavenParser.builder(), before, - SourceSpec.EachResult.noop, Assertions::customizeExecutionContext); + SourceSpec.ValidateSource.noop, Assertions::customizeExecutionContext); maven.path("pom.xml"); spec.accept(maven); return maven; @@ -55,7 +55,7 @@ public static SourceSpecs pomXml(@Language("xml") @Nullable String before, @Lang public static SourceSpecs pomXml(@Language("xml") @Nullable String before, @Language("xml") @Nullable String after, Consumer> spec) { SourceSpec maven = new SourceSpec<>(Xml.Document.class, "maven", MavenParser.builder(), before, - SourceSpec.EachResult.noop, Assertions::customizeExecutionContext).after(s -> after); + SourceSpec.ValidateSource.noop, Assertions::customizeExecutionContext).after(s -> after); maven.path("pom.xml"); spec.accept(maven); return maven; diff --git a/rewrite-test/src/main/java/org/openrewrite/test/RecipeSpec.java b/rewrite-test/src/main/java/org/openrewrite/test/RecipeSpec.java index bf9c6e6141c..90c43fbb0ea 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/RecipeSpec.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/RecipeSpec.java @@ -80,6 +80,9 @@ public static RecipeSpec defaults() { @Nullable TypeValidation typeValidation; + @Nullable + TypeValidation afterTypeValidation; + boolean serializationValidation = true; @Nullable @@ -262,6 +265,11 @@ public RecipeSpec typeValidationOptions(TypeValidation typeValidation) { return this; } + public RecipeSpec afterTypeValidationOptions(TypeValidation typeValidation) { + this.afterTypeValidation = typeValidation; + return this; + } + @Nullable ExecutionContext getExecutionContext() { return executionContext; diff --git a/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java b/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java index 7d5250d0ff7..1bea7fb1ebf 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java @@ -284,6 +284,9 @@ default void rewriteRun(Consumer spec, SourceSpec... sourceSpecs) } sourceFile = sourceFile.withMarkers(markers); + // Validate before source + nextSpec.validateSource.accept(sourceFile, TypeValidation.before(testMethodSpec, testClassSpec)); + // Validate that printing a parsed AST yields the same source text int j = 0; for (Parser.Input input : inputs.values()) { @@ -464,7 +467,7 @@ default void rewriteRun(Consumer spec, SourceSpec... sourceSpecs) expectedAfter : trimIndentPreserveCRLF(expectedAfter); assertContentEquals(result.getAfter(), expected, actualAfter, "Unexpected result in"); - sourceSpec.eachResult.accept(result.getAfter(), testMethodSpec, testClassSpec); + sourceSpec.validateSource.accept(result.getAfter(), TypeValidation.after(testMethodSpec, testClassSpec)); } else { boolean isRemote = result.getAfter() instanceof Remote; if (!isRemote && Objects.equals(result.getBefore().getSourcePath(), result.getAfter().getSourcePath()) && diff --git a/rewrite-test/src/main/java/org/openrewrite/test/SourceSpec.java b/rewrite-test/src/main/java/org/openrewrite/test/SourceSpec.java index daf1f350cab..45a5a6567b6 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/SourceSpec.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/SourceSpec.java @@ -55,15 +55,15 @@ public class SourceSpec implements SourceSpecs { UnaryOperator after; /** - * Apply a function to each SourceFile after recipe execution. + * Apply a function to each SourceFile (before and after) recipe execution. * Useful for validating the AST or its metadata. */ - final EachResult eachResult; + final ValidateSource validateSource; - public interface EachResult { - EachResult noop = (sourceFile, testMethodSpec, testClassSpec) -> sourceFile; + public interface ValidateSource { + ValidateSource noop = (sourceFile, typeValidation) -> sourceFile; - SourceFile accept(SourceFile sourceFile, RecipeSpec testMethodSpec, RecipeSpec testClassSpec); + SourceFile accept(SourceFile sourceFile, TypeValidation typeValidation); } final ThrowingConsumer customizeExecutionContext; @@ -75,7 +75,7 @@ public SourceSpec(Class sourceFileType, @Nullable String dsl, this.parser = parser; this.before = before; this.after = after; - this.eachResult = EachResult.noop; + this.validateSource = ValidateSource.noop; this.customizeExecutionContext = (ctx) -> { }; } diff --git a/rewrite-test/src/main/java/org/openrewrite/test/TypeValidation.java b/rewrite-test/src/main/java/org/openrewrite/test/TypeValidation.java index 5581a3d2c98..6d2ed291f59 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/TypeValidation.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/TypeValidation.java @@ -46,7 +46,24 @@ public class TypeValidation { @Builder.Default private boolean constructorInvocations = true; + public static TypeValidation all() { + return new TypeValidation(); + } + public static TypeValidation none() { return new TypeValidation(false,false,false,false,false,false); } + + static TypeValidation before(RecipeSpec testMethodSpec, RecipeSpec testClassSpec) { + TypeValidation typeValidation = testMethodSpec.getTypeValidation() != null ? + testMethodSpec.getTypeValidation() : testClassSpec.getTypeValidation(); + return typeValidation != null ? typeValidation : new TypeValidation(); + } + + static TypeValidation after(RecipeSpec testMethodSpec, RecipeSpec testClassSpec) { + TypeValidation typeValidation = testMethodSpec.getAfterTypeValidation() != null ? + testMethodSpec.getAfterTypeValidation() : testClassSpec.getAfterTypeValidation(); + // after type validation defaults to before type validation; possibly this will become stricter in the future + return typeValidation != null ? typeValidation : before(testMethodSpec, testClassSpec); + } }