diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/service/GroovyAutoFormatService.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/service/GroovyAutoFormatService.java index 5818e4f0f49..a645fbeac2f 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/service/GroovyAutoFormatService.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/service/GroovyAutoFormatService.java @@ -16,14 +16,23 @@ package org.openrewrite.groovy.service; import org.jspecify.annotations.Nullable; +import org.openrewrite.SourceFile; import org.openrewrite.Tree; import org.openrewrite.groovy.format.AutoFormatVisitor; +import org.openrewrite.groovy.format.SpacesVisitor; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.service.AutoFormatService; +import org.openrewrite.java.style.IntelliJ; +import org.openrewrite.java.style.SpacesStyle; public class GroovyAutoFormatService extends AutoFormatService { @Override public

JavaVisitor

autoFormatVisitor(@Nullable Tree stopAfter) { return new AutoFormatVisitor<>(stopAfter); } + + @Override + public

JavaVisitor

spacesVisitor(SourceFile sourceFile, SpacesStyle spacesStyle, @Nullable Tree stopAfter) { + return new SpacesVisitor<>(spacesStyle, null, null, IntelliJ.wrappingAndBraces(), stopAfter); + } } diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/format/TypecastParenPadGroovyTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/format/TypecastParenPadGroovyTest.java new file mode 100644 index 00000000000..ca3cdbd0eee --- /dev/null +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/format/TypecastParenPadGroovyTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2026 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.groovy.format; + +import org.junit.jupiter.api.Test; +import org.openrewrite.java.format.TypecastParenPad; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.groovy.Assertions.groovy; + +class TypecastParenPadGroovyTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new TypecastParenPad()); + } + + @Test + void removesPadding() { + rewriteRun( + groovy( + """ + def a = ( int ) 0L + """, + """ + def a = (int) 0L + """ + ) + ); + } + + @Test + void doesNotCorruptAsStyleCast() { + rewriteRun( + groovy( + """ + def a = 0L as Integer + """ + ) + ); + } +} diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java b/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java index ffe2a093b01..bb74ae27b3a 100755 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/TypecastParenPad.java @@ -19,6 +19,7 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.service.AutoFormatService; import org.openrewrite.java.style.Checkstyle; import org.openrewrite.java.style.IntelliJ; import org.openrewrite.java.style.SpacesStyle; @@ -61,7 +62,9 @@ private static class TypecastParenPadVisitor extends JavaIsoVisitor(spacesStyle, tc) + JavaSourceFile cu = getCursor().firstEnclosingOrThrow(JavaSourceFile.class); + return (J.TypeCast) cu.service(AutoFormatService.class) + .spacesVisitor(cu, spacesStyle, tc) .visitNonNull(tc, ctx, getCursor().getParentTreeCursor().fork()); } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/service/AutoFormatService.java b/rewrite-java/src/main/java/org/openrewrite/java/service/AutoFormatService.java index 97883f0885f..4dd7984e253 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/service/AutoFormatService.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/service/AutoFormatService.java @@ -23,6 +23,8 @@ import org.openrewrite.java.format.AutoFormatVisitor; import org.openrewrite.java.format.BlankLinesVisitor; import org.openrewrite.java.format.NormalizeFormatVisitor; +import org.openrewrite.java.format.SpacesVisitor; +import org.openrewrite.java.style.SpacesStyle; @Incubating(since = "8.2.0") public class AutoFormatService { @@ -50,4 +52,14 @@ public

JavaVisitor

normalizeFormatVisitor(@Nullable Tree stopAfter) { public

JavaVisitor

blankLinesVisitor(SourceFile sourceFile, @Nullable Tree stopAfter) { return new BlankLinesVisitor<>(sourceFile, stopAfter); } + + /** + * Returns the language-appropriate {@link SpacesVisitor}. The {@code spacesStyle} + * carries Java-specific tuning (e.g. typecast parenthesis padding) that only applies + * to languages with C-style casts; languages that reuse {@code J.TypeCast} for other + * syntax resolve their own style from {@code sourceFile} instead. + */ + public

JavaVisitor

spacesVisitor(SourceFile sourceFile, SpacesStyle spacesStyle, @Nullable Tree stopAfter) { + return new SpacesVisitor<>(spacesStyle, stopAfter); + } } diff --git a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/service/KotlinAutoFormatService.java b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/service/KotlinAutoFormatService.java index 85502ed2928..72c91f62c43 100644 --- a/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/service/KotlinAutoFormatService.java +++ b/rewrite-kotlin/src/main/java/org/openrewrite/kotlin/service/KotlinAutoFormatService.java @@ -20,14 +20,17 @@ import org.openrewrite.Tree; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.service.AutoFormatService; +import org.openrewrite.java.style.SpacesStyle; import org.openrewrite.kotlin.format.AutoFormatVisitor; import org.openrewrite.kotlin.format.BlankLinesVisitor; import org.openrewrite.kotlin.format.NormalizeFormatVisitor; +import org.openrewrite.kotlin.format.SpacesVisitor; import org.openrewrite.kotlin.style.BlankLinesStyle; import org.openrewrite.kotlin.style.IntelliJ; import org.openrewrite.style.Style; import static org.openrewrite.kotlin.style.IntelliJ.blankLines; +import static org.openrewrite.kotlin.style.IntelliJ.spaces; public class KotlinAutoFormatService extends AutoFormatService { @@ -45,4 +48,9 @@ public

JavaVisitor

normalizeFormatVisitor(@Nullable Tree stopAfter) { public

JavaVisitor

blankLinesVisitor(SourceFile sourceFile, @Nullable Tree stopAfter) { return new BlankLinesVisitor<>(Style.from(BlankLinesStyle.class, sourceFile, IntelliJ::blankLines), stopAfter); } + + @Override + public

JavaVisitor

spacesVisitor(SourceFile sourceFile, SpacesStyle spacesStyle, @Nullable Tree stopAfter) { + return new SpacesVisitor<>(Style.from(org.openrewrite.kotlin.style.SpacesStyle.class, sourceFile, () -> spaces()), stopAfter); + } } diff --git a/rewrite-kotlin/src/test/java/org/openrewrite/kotlin/format/TypecastParenPadKotlinTest.java b/rewrite-kotlin/src/test/java/org/openrewrite/kotlin/format/TypecastParenPadKotlinTest.java new file mode 100644 index 00000000000..3473b3ecdac --- /dev/null +++ b/rewrite-kotlin/src/test/java/org/openrewrite/kotlin/format/TypecastParenPadKotlinTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2026 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.kotlin.format; + +import org.junit.jupiter.api.Test; +import org.openrewrite.java.format.TypecastParenPad; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.kotlin.Assertions.kotlin; + +class TypecastParenPadKotlinTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new TypecastParenPad()); + } + + @Test + void asCast() { + rewriteRun( + kotlin( + """ + fun method(list: List) { + val b = list.get(0) as List<*> + val c = list.get(0) as String + } + """ + ) + ); + } + + @Test + void nullSafeCast() { + rewriteRun( + kotlin( + """ + fun method(a: Any) { + val b = a as? List<*> + } + """ + ) + ); + } +}