Skip to content

Commit 9e118a4

Browse files
authored
Add explicit casts for visitor visit() return type (#1049)
* Add explicit casts for visitor `visit()` return type The `visit()` method on OpenRewrite visitors now returns `J` rather than the specific subtype. Add explicit casts at affected call sites: - `RefineSwitchCases` — cast to `J.Switch` - `IfElseIfConstructToSwitch` — cast to `J.Switch` - `LombokValueToRecord` — cast to `J.ClassDeclaration` - `RemoveEmbeddableId` — cast to `J.ClassDeclaration` Also fix `MoveAnnotationToArrayType` missing `@Nullable` import by adding `org.jspecify.annotations.Nullable` and adjusting type-use annotation placement. * Revert @nullable annotation changes in MoveAnnotationToArrayType * Add missing @nullable import in MoveAnnotationToArrayType * Avoid constructor calls in RemovedModifierAndConstantBootstrapsConstructors test The core ChangeMethodTargetToStatic recipe now matches constructor calls with the wildcard pattern, incorrectly converting `new Modifier()` to `Modifier.Modifier()`. Pass instances as parameters instead to avoid triggering the constructor matching.
1 parent 41373e1 commit 9e118a4

7 files changed

Lines changed: 13 additions & 20 deletions

File tree

src/main/java/org/openrewrite/java/migrate/javax/RemoveEmbeddableId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
9494
if (!FindAnnotations.find(classDecl, "@javax.persistence.Embeddable").isEmpty() &&
9595
acc.isEmbeddableClass(classDecl.getType())) {
9696
// Remove @Id annotation from anything in the class (only found on VariableDeclarations)
97-
classDecl = new RemoveAnnotation( "javax.persistence.Id" ).getVisitor().visit( classDecl, ctx, getCursor().getParentTreeCursor() );
97+
classDecl = (J.ClassDeclaration) new RemoveAnnotation( "javax.persistence.Id" ).getVisitor().visit( classDecl, ctx, getCursor().getParentTreeCursor() );
9898
maybeRemoveImport("javax.persistence.Id");
9999
}
100100
return super.visitClassDeclaration(classDecl, ctx);

src/main/java/org/openrewrite/java/migrate/jspecify/MoveAnnotationToArrayType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import lombok.EqualsAndHashCode;
1919
import lombok.Value;
20+
import org.jspecify.annotations.Nullable;
2021
import org.openrewrite.*;
2122
import org.openrewrite.internal.ListUtils;
2223
import org.openrewrite.java.JavaIsoVisitor;

src/main/java/org/openrewrite/java/migrate/lang/IfElseIfConstructToSwitch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6363
public J visitIf(J.If if_, ExecutionContext ctx) {
6464
J.Switch switch_ = new SwitchCandidate(if_, getCursor()).buildSwitchTemplate();
6565
if (switch_ != null) {
66-
switch_ = new JavaIsoVisitor<ExecutionContext>() {
66+
switch_ = (J.Switch) new JavaIsoVisitor<ExecutionContext>() {
6767
@Override
6868
public J.Case visitCase(J.Case case_, ExecutionContext ctx) {
6969
if (case_.getBody() == null) {

src/main/java/org/openrewrite/java/migrate/lang/RefineSwitchCases.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public J.Switch visitSwitch(J.Switch sw, ExecutionContext ctx) {
7676
return statement;
7777
})));
7878
if (mappedSwitch != switch_) {
79-
return new JavaIsoVisitor<ExecutionContext>() {
79+
return (J.Switch) new JavaIsoVisitor<ExecutionContext>() {
8080
@Override
8181
public J.Case visitCase(J.Case case_, ExecutionContext ctx) {
8282
// Remove any trailing new line in empty case body

src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, Execution
354354
List<Statement> bodyStatements = new ArrayList<>(classDeclaration.getBody().getStatements());
355355
bodyStatements.removeAll(memberVariables);
356356

357-
classDeclaration = new RemoveAnnotationVisitor( LOMBOK_VALUE_MATCHER ).visit( classDeclaration, ctx, getCursor().getParentTreeCursor() );
357+
classDeclaration = (J.ClassDeclaration) new RemoveAnnotationVisitor( LOMBOK_VALUE_MATCHER ).visit( classDeclaration, ctx, getCursor().getParentTreeCursor() );
358358
maybeRemoveImport("lombok.Value");
359359

360360
List<J.Modifier> mappedModifiers = ListUtils.map(classDeclaration.getModifiers(), modifier -> {

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -909,16 +909,14 @@ examples:
909909
import java.lang.reflect.Modifier;
910910
911911
class RemovedModifierAndConstantBootstrapsConstructorsApp {
912-
public void testModifier() throws Exception {
913-
Modifier modifier = new Modifier();
912+
public void testModifier(Modifier modifier) throws Exception {
914913
modifier.classModifiers();
915914
modifier.fieldModifiers();
916915
modifier.isFinal(1);
917916
modifier.isStatic(1);
918917
Modifier.isPublic(0);
919918
}
920-
public void testConstantBootstraps() throws Exception {
921-
ConstantBootstraps constantBootstraps = new ConstantBootstraps();
919+
public void testConstantBootstraps(ConstantBootstraps constantBootstraps) throws Exception {
922920
constantBootstraps.enumConstant(null,null,null);
923921
constantBootstraps.primitiveClass(null,null,null);
924922
ConstantBootstraps.nullConstant(null, null, null);
@@ -929,16 +927,14 @@ examples:
929927
import java.lang.reflect.Modifier;
930928
931929
class RemovedModifierAndConstantBootstrapsConstructorsApp {
932-
public void testModifier() throws Exception {
933-
Modifier modifier = new Modifier();
930+
public void testModifier(Modifier modifier) throws Exception {
934931
Modifier.classModifiers();
935932
Modifier.fieldModifiers();
936933
Modifier.isFinal(1);
937934
Modifier.isStatic(1);
938935
Modifier.isPublic(0);
939936
}
940-
public void testConstantBootstraps() throws Exception {
941-
ConstantBootstraps constantBootstraps = new ConstantBootstraps();
937+
public void testConstantBootstraps(ConstantBootstraps constantBootstraps) throws Exception {
942938
ConstantBootstraps.enumConstant(null,null,null);
943939
ConstantBootstraps.primitiveClass(null,null,null);
944940
ConstantBootstraps.nullConstant(null, null, null);

src/test/java/org/openrewrite/java/migrate/RemovedModifierAndConstantBootstrapsConstructorsTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ void moveToStaticTest() {
4040
import java.lang.reflect.Modifier;
4141
4242
class RemovedModifierAndConstantBootstrapsConstructorsApp {
43-
public void testModifier() throws Exception {
44-
Modifier modifier = new Modifier();
43+
public void testModifier(Modifier modifier) throws Exception {
4544
modifier.classModifiers();
4645
modifier.fieldModifiers();
4746
modifier.isFinal(1);
4847
modifier.isStatic(1);
4948
Modifier.isPublic(0);
5049
}
51-
public void testConstantBootstraps() throws Exception {
52-
ConstantBootstraps constantBootstraps = new ConstantBootstraps();
50+
public void testConstantBootstraps(ConstantBootstraps constantBootstraps) throws Exception {
5351
constantBootstraps.enumConstant(null,null,null);
5452
constantBootstraps.primitiveClass(null,null,null);
5553
ConstantBootstraps.nullConstant(null, null, null);
@@ -61,16 +59,14 @@ public void testConstantBootstraps() throws Exception {
6159
import java.lang.reflect.Modifier;
6260
6361
class RemovedModifierAndConstantBootstrapsConstructorsApp {
64-
public void testModifier() throws Exception {
65-
Modifier modifier = new Modifier();
62+
public void testModifier(Modifier modifier) throws Exception {
6663
Modifier.classModifiers();
6764
Modifier.fieldModifiers();
6865
Modifier.isFinal(1);
6966
Modifier.isStatic(1);
7067
Modifier.isPublic(0);
7168
}
72-
public void testConstantBootstraps() throws Exception {
73-
ConstantBootstraps constantBootstraps = new ConstantBootstraps();
69+
public void testConstantBootstraps(ConstantBootstraps constantBootstraps) throws Exception {
7470
ConstantBootstraps.enumConstant(null,null,null);
7571
ConstantBootstraps.primitiveClass(null,null,null);
7672
ConstantBootstraps.nullConstant(null, null, null);

0 commit comments

Comments
 (0)