Skip to content

Commit 0f250d2

Browse files
committed
TypecastParenPad should not make incorrect changes for Groovy
Fixes #561
1 parent ec961e2 commit 0f250d2

File tree

3 files changed

+161
-54
lines changed

3 files changed

+161
-54
lines changed

src/main/java/org/openrewrite/staticanalysis/TypecastParenPad.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openrewrite.java.style.TypecastParenPadStyle;
2626
import org.openrewrite.java.tree.J;
2727
import org.openrewrite.java.tree.JavaSourceFile;
28+
import org.openrewrite.staticanalysis.groovy.GroovyFileChecker;
2829

2930
import java.util.Optional;
3031

@@ -39,37 +40,38 @@ public String getDisplayName() {
3940
@Override
4041
public String getDescription() {
4142
return "Fixes whitespace padding between a typecast type identifier and the enclosing left and right parentheses. " +
42-
"For example, when configured to remove spacing, `( int ) 0L;` becomes `(int) 0L;`.";
43+
"For example, when configured to remove spacing, `( int ) 0L;` becomes `(int) 0L;`.";
4344
}
4445

4546
@Override
4647
public TreeVisitor<?, ExecutionContext> getVisitor() {
47-
return new TypecastParenPadVisitor();
48-
}
49-
50-
private static class TypecastParenPadVisitor extends JavaIsoVisitor<ExecutionContext> {
51-
SpacesStyle spacesStyle;
52-
TypecastParenPadStyle typecastParenPadStyle;
48+
return Preconditions.check(
49+
Preconditions.not(new GroovyFileChecker<>()),
50+
new JavaIsoVisitor<ExecutionContext>() {
51+
SpacesStyle spacesStyle;
52+
TypecastParenPadStyle typecastParenPadStyle;
5353

54-
@Override
55-
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
56-
if (tree instanceof JavaSourceFile) {
57-
SourceFile cu = (SourceFile) requireNonNull(tree);
58-
spacesStyle = Optional.ofNullable(cu.getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces());
59-
typecastParenPadStyle = Optional.ofNullable(cu.getStyle(TypecastParenPadStyle.class)).orElse(Checkstyle.typecastParenPadStyle());
54+
@Override
55+
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
56+
if (tree instanceof JavaSourceFile) {
57+
SourceFile cu = (SourceFile) requireNonNull(tree);
58+
spacesStyle = Optional.ofNullable(cu.getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces());
59+
typecastParenPadStyle = Optional.ofNullable(cu.getStyle(TypecastParenPadStyle.class)).orElse(Checkstyle.typecastParenPadStyle());
6060

61-
spacesStyle = spacesStyle.withWithin(spacesStyle.getWithin().withTypeCastParentheses(typecastParenPadStyle.getSpace()));
62-
}
63-
return super.visit(tree, ctx);
64-
}
61+
spacesStyle = spacesStyle.withWithin(spacesStyle.getWithin().withTypeCastParentheses(typecastParenPadStyle.getSpace()));
62+
}
63+
return super.visit(tree, ctx);
64+
}
6565

66-
@Override
67-
public J.TypeCast visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
68-
J.TypeCast tc = super.visitTypeCast(typeCast, ctx);
69-
tc = (J.TypeCast) new SpacesVisitor<>(spacesStyle, null, null, tc)
70-
.visitNonNull(tc, ctx, getCursor().getParentTreeCursor().fork());
71-
return tc;
72-
}
66+
@Override
67+
public J.TypeCast visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
68+
J.TypeCast tc = super.visitTypeCast(typeCast, ctx);
69+
tc = (J.TypeCast) new SpacesVisitor<>(spacesStyle, null, null, tc)
70+
.visitNonNull(tc, ctx, getCursor().getParentTreeCursor().fork());
71+
return tc;
72+
}
73+
}
74+
);
7375
}
7476

7577
}

src/main/resources/META-INF/rewrite/examples.yml

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -360,44 +360,44 @@ examples:
360360
- description: ''
361361
sources:
362362
- before: |
363+
import java.io.FileReader;
364+
import java.io.IOException;
365+
363366
class A {
364-
void foo() throws IllegalAccessException {
367+
void foo() throws IOException {
365368
try {
366-
throw new IllegalAccessException();
367-
} catch (Exception e) {
368-
throw e; // `e` is removed below
369+
new FileReader("").read();
370+
} catch (IOException e) {
371+
throw e;
369372
}
370373
}
371374
}
372375
after: |
376+
import java.io.FileReader;
377+
import java.io.IOException;
378+
373379
class A {
374-
void foo() throws IllegalAccessException {
375-
throw new IllegalAccessException();
380+
void foo() throws IOException {
381+
new FileReader("").read();
376382
}
377383
}
378384
language: java
379385
- description: ''
380386
sources:
381387
- before: |
382-
import java.io.FileReader;
383-
import java.io.IOException;
384-
385388
class A {
386-
void foo() throws IOException {
389+
void foo() throws IllegalAccessException {
387390
try {
388-
new FileReader("").read();
389-
} catch (IOException e) {
390-
throw e;
391+
throw new IllegalAccessException();
392+
} catch (Exception e) {
393+
throw e; // `e` is removed below
391394
}
392395
}
393396
}
394397
after: |
395-
import java.io.FileReader;
396-
import java.io.IOException;
397-
398398
class A {
399-
void foo() throws IOException {
400-
new FileReader("").read();
399+
void foo() throws IllegalAccessException {
400+
throw new IllegalAccessException();
401401
}
402402
}
403403
language: java
@@ -2834,18 +2834,6 @@ examples:
28342834
type: specs.openrewrite.org/v1beta/example
28352835
recipeName: org.openrewrite.staticanalysis.SimplifyBooleanExpression
28362836
examples:
2837-
- description: ''
2838-
sources:
2839-
- before: |
2840-
fun getSymbol() : String? {
2841-
return null
2842-
}
2843-
language: kotlin
2844-
- before: |
2845-
val isPositive = getSymbol().equals("+") == true
2846-
after: |
2847-
val isPositive = getSymbol().equals("+")
2848-
language: kotlin
28492837
- description: ''
28502838
sources:
28512839
- before: |
@@ -2865,6 +2853,18 @@ examples:
28652853
}
28662854
}
28672855
language: java
2856+
- description: ''
2857+
sources:
2858+
- before: |
2859+
fun getSymbol() : String? {
2860+
return null
2861+
}
2862+
language: kotlin
2863+
- before: |
2864+
val isPositive = getSymbol().equals("+") == true
2865+
after: |
2866+
val isPositive = getSymbol().equals("+")
2867+
language: kotlin
28682868
---
28692869
type: specs.openrewrite.org/v1beta/example
28702870
recipeName: org.openrewrite.staticanalysis.SimplifyBooleanReturn
@@ -3187,6 +3187,31 @@ examples:
31873187
language: java
31883188
---
31893189
type: specs.openrewrite.org/v1beta/example
3190+
recipeName: org.openrewrite.staticanalysis.TypecastParenPad
3191+
examples:
3192+
- description: ''
3193+
sources:
3194+
- before: |
3195+
class A {
3196+
void m() {
3197+
int i = ( int ) 0L;
3198+
int j = (int) 0L;
3199+
int k = (int)0L;
3200+
int l = ( int )0L;
3201+
}
3202+
}
3203+
after: |
3204+
class A {
3205+
void m() {
3206+
int i = (int) 0L;
3207+
int j = (int) 0L;
3208+
int k = (int) 0L;
3209+
int l = (int) 0L;
3210+
}
3211+
}
3212+
language: java
3213+
---
3214+
type: specs.openrewrite.org/v1beta/example
31903215
recipeName: org.openrewrite.staticanalysis.URLEqualsHashCodeRecipes
31913216
examples:
31923217
- description: ''
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.staticanalysis;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.Issue;
21+
import org.openrewrite.test.RecipeSpec;
22+
import org.openrewrite.test.RewriteTest;
23+
24+
import static org.openrewrite.groovy.Assertions.groovy;
25+
import static org.openrewrite.java.Assertions.java;
26+
27+
class TypecastParenPadTest implements RewriteTest {
28+
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec.recipe(new TypecastParenPad());
32+
}
33+
34+
@DocumentExample
35+
@Test
36+
void typecastParenPad() {
37+
rewriteRun(
38+
//language=java
39+
java(
40+
"""
41+
class A {
42+
void m() {
43+
int i = ( int ) 0L;
44+
int j = (int) 0L;
45+
int k = (int)0L;
46+
int l = ( int )0L;
47+
}
48+
}
49+
""",
50+
"""
51+
class A {
52+
void m() {
53+
int i = (int) 0L;
54+
int j = (int) 0L;
55+
int k = (int) 0L;
56+
int l = (int) 0L;
57+
}
58+
}
59+
"""
60+
)
61+
);
62+
}
63+
64+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/561")
65+
@Test
66+
void noChangeForGroovy() {
67+
rewriteRun(
68+
//language=groovy
69+
groovy(
70+
"""
71+
class A {
72+
void m(Object o) {
73+
int l = ( o as String).length()
74+
}
75+
}
76+
"""
77+
)
78+
);
79+
}
80+
}

0 commit comments

Comments
 (0)