diff --git a/src/main/java/org/openrewrite/java/migrate/lang/ExtractExplicitConstructorInvocationArguments.java b/src/main/java/org/openrewrite/java/migrate/lang/ExtractExplicitConstructorInvocationArguments.java index 6a4eb7bcff..bb7ddbd5e0 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/ExtractExplicitConstructorInvocationArguments.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/ExtractExplicitConstructorInvocationArguments.java @@ -20,6 +20,7 @@ import org.openrewrite.ExecutionContext; import org.openrewrite.Preconditions; import org.openrewrite.Recipe; +import org.openrewrite.SourceFile; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.StringUtils; import org.openrewrite.java.JavaIsoVisitor; @@ -60,6 +61,13 @@ public class ExtractExplicitConstructorInvocationArguments extends Recipe { @Override public TreeVisitor getVisitor() { return Preconditions.check(new UsesJavaVersion<>(25), new JavaIsoVisitor() { + @Override + public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { + // JEP 513 is a Java language feature, and the rewrite is expressed as a `JavaTemplate`, which + // parses its stub as Java. Other JVM languages sharing the Java LST are therefore out of scope. + return sourceFile instanceof J.CompilationUnit; + } + @Override public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx); diff --git a/src/test/java/org/openrewrite/java/migrate/lang/ExtractExplicitConstructorInvocationArgumentsTest.java b/src/test/java/org/openrewrite/java/migrate/lang/ExtractExplicitConstructorInvocationArgumentsTest.java index 00d51c26cd..68bf3c604e 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/ExtractExplicitConstructorInvocationArgumentsTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/ExtractExplicitConstructorInvocationArgumentsTest.java @@ -29,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.condition.JRE.JAVA_25; +import static org.openrewrite.groovy.Assertions.groovy; import static org.openrewrite.java.Assertions.java; import static org.openrewrite.java.Assertions.javaVersion; @@ -691,4 +692,30 @@ class Child { ) ); } + + @Test + void doNotRunOnGroovySources() { + rewriteRun( + //language=groovy + groovy( + // `@CompileStatic` gives the `super(..)` call a method type; without one the recipe returns early + // and this source would not reach the guard under test + """ + import groovy.transform.CompileStatic + + class Parent { + Parent(String name) { + } + } + + @CompileStatic + class Child extends Parent { + Child(String name) { + super(name.trim()) + } + } + """ + ) + ); + } }