Skip to content

Commit 8471357

Browse files
author
bhavanapidapa
committed
Custom Recipe and test cases for changing exception type
1 parent e31a547 commit 8471357

File tree

4 files changed

+93
-80
lines changed

4 files changed

+93
-80
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.openrewrite.java.migrate;
2+
3+
import org.openrewrite.ExecutionContext;
4+
import org.openrewrite.Recipe;
5+
import org.openrewrite.TreeVisitor;
6+
import org.openrewrite.java.ChangeType;
7+
import org.openrewrite.java.JavaVisitor;
8+
import org.openrewrite.java.MethodMatcher;
9+
import org.openrewrite.java.tree.Expression;
10+
import org.openrewrite.java.tree.J;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.regex.Pattern;
15+
16+
public class ArrayStoreExceptionToTypeNotPresentException extends Recipe {
17+
@Override
18+
public String getDisplayName() {
19+
return "Handle TypeNotPresentException instead of ArrayStoreException";
20+
}
21+
22+
@Override
23+
public String getDescription() {
24+
return "This recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException or both exceptions, to ensure compatibility with Java 11+.";
25+
}
26+
27+
@Override
28+
public TreeVisitor<?, ExecutionContext> getVisitor() {
29+
final MethodMatcher classGetAnnotationMethod = new MethodMatcher("java.lang.Class getAnnotation(java.lang.Class)");
30+
return new JavaVisitor<ExecutionContext>() {
31+
32+
@Override
33+
public J visitTry(J.Try tryStmt, ExecutionContext ctx) {
34+
boolean flag = false;
35+
if (containsGetAnnotation(tryStmt)) {
36+
List<J.Try.Catch> updatedCatches = new ArrayList<>();
37+
for (J.Try.Catch catchClause : tryStmt.getCatches()) {
38+
if (catchClause.getParameter().getType() != null && catchClause.getParameter().getType().isAssignableFrom(Pattern.compile("java.lang.ArrayStoreException"))) {
39+
J.Try.Catch updatedCatch = (J.Try.Catch) new ChangeType("java.lang.ArrayStoreException", "java.lang.TypeNotPresentException", true).getVisitor().visit(catchClause, ctx);
40+
updatedCatches.add(updatedCatch);
41+
flag = true;
42+
} else {
43+
updatedCatches.add(catchClause);
44+
}
45+
}
46+
if (flag)
47+
tryStmt = tryStmt.withCatches(updatedCatches);
48+
}
49+
return super.visitTry(tryStmt, ctx);
50+
}
51+
52+
private boolean containsGetAnnotation(J.Try tryStmt) {
53+
return tryStmt.getBody().getStatements().stream()
54+
.filter(Expression.class::isInstance)
55+
.map(Expression.class::cast)
56+
.anyMatch(classGetAnnotationMethod::matches);
57+
}
58+
};
59+
}
60+
}
61+

src/main/java/org/openrewrite/java/migrate/HandleTypeNotPresentExceptionInsteadOfArrayStoreException.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/main/resources/META-INF/rewrite/java-version-11.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ recipeList:
7373
- org.openrewrite.scala.migrate.UpgradeScala_2_12
7474
- org.openrewrite.java.migrate.ReplaceComSunAWTUtilitiesMethods
7575
- org.openrewrite.java.migrate.ReplaceLocalizedStreamMethods
76-
- org.openrewrite.java.migrate.HandleTypeNotPresentExceptionInsteadOfArrayStoreException
76+
- org.openrewrite.java.migrate.ArrayStoreExceptionToTypeNotPresentException
7777

7878
---
7979
type: specs.openrewrite.org/v1beta/recipe

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

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
package org.openrewrite.java.migrate;
22

33
import org.junit.jupiter.api.Test;
4-
import org.openrewrite.java.JavaParser;
54
import org.openrewrite.test.RecipeSpec;
65
import org.openrewrite.test.RewriteTest;
76

87
import static org.openrewrite.java.Assertions.java;
9-
import static org.openrewrite.java.Assertions.javaVersion;
108

119
public class TypeNotPresentExceptionTest implements RewriteTest {
1210
@Override
1311
public void defaults(RecipeSpec spec) {
14-
spec.recipe(new HandleTypeNotPresentExceptionInsteadOfArrayStoreException())
15-
.parser(JavaParser.fromJavaVersion()
16-
//language=java
17-
.dependsOn(
12+
spec.recipe(new ArrayStoreExceptionToTypeNotPresentException());
13+
}
14+
15+
@Test
16+
void ArrayStoreExceptionTest() {
17+
rewriteRun(
18+
//language=java
19+
java(
1820
"""
1921
import java.lang.annotation.*;
2022
import java.util.*;
21-
23+
2224
public class Test {
2325
public void testMethod() {
2426
try {
2527
Object o = "test";
2628
o.getClass().getAnnotation(Override.class);
29+
} catch (ArrayStoreException e) {
30+
System.out.println("Caught Exception");
31+
}
32+
try {
33+
Object.class.getAnnotation(Override.class);
2734
} catch (ArrayStoreException e) {
2835
System.out.println("Caught ArrayStoreException");
2936
}
@@ -33,14 +40,19 @@ public void testMethod() {
3340
"""
3441
import java.lang.annotation.*;
3542
import java.util.*;
36-
43+
3744
public class Test {
3845
public void testMethod() {
3946
try {
4047
Object o = "test";
4148
o.getClass().getAnnotation(Override.class);
4249
} catch (TypeNotPresentException e) {
43-
System.out.println("Caught TypeNotPresentException");
50+
System.out.println("Caught Exception");
51+
}
52+
try {
53+
Object.class.getAnnotation(Override.class);
54+
} catch (TypeNotPresentException e) {
55+
System.out.println("Caught ArrayStoreException");
4456
}
4557
}
4658
}
@@ -49,24 +61,23 @@ public void testMethod() {
4961
);
5062
}
5163

52-
5364
@Test
54-
void replaceGetLocalizedOutputStream() {
65+
void rOtherExceptionsTest() {
5566
rewriteRun(
5667
//language=java
5768
java(
5869
"""
59-
public class Test {
60-
public void testMethod() {
61-
try {
62-
Object o = "test";
63-
o.getClass().getAnnotation(Override.class);
64-
} catch (NullPointerException e) {
65-
System.out.println("Caught NullPointerException");
70+
public class Test {
71+
public void testMethod() {
72+
try {
73+
Object o = "test";
74+
o.getClass().getAnnotation(Override.class);
75+
} catch (NullPointerException e) {
76+
System.out.println("Caught Exception");
77+
}
6678
}
6779
}
68-
}
69-
"""
80+
"""
7081
)
7182
);
7283
}

0 commit comments

Comments
 (0)