Skip to content

Commit ffc2e15

Browse files
author
bhavanapidapa
committed
draft recipe and test case
1 parent 85e984c commit ffc2e15

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.JavaVisitor;
7+
import org.openrewrite.java.tree.J;
8+
import org.openrewrite.java.tree.JavaType;
9+
10+
import java.util.Collections;
11+
import java.util.List;
12+
13+
public class HandleTypeNotPresentExceptionInsteadOfArrayStoreException extends Recipe {
14+
@Override
15+
public String getDisplayName() {
16+
return "Handle TypeNotPresentException instead of ArrayStoreException";
17+
}
18+
19+
@Override
20+
public String getDescription() {
21+
return "This recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException or both exceptions, to ensure compatibility with Java 11+.";
22+
}
23+
24+
@Override
25+
public TreeVisitor<?, ExecutionContext> getVisitor() {
26+
return new JavaVisitor<ExecutionContext>() {
27+
@Override
28+
public J visitTry(J.Try tryStmt, ExecutionContext ctx ) {
29+
if(containsGetAnnotation(tryStmt.getBody()))
30+
{
31+
for (J.Try.Catch catchClause : tryStmt.getCatches()) {
32+
if(catchClause.getParameter().getType() != null && "java.lang.ArrayStoreException".equals(catchClause.getParameter().getType().toString())) {
33+
JavaType.Class typeNotPresentExceptionType = new JavaType.Class(null,0,"java.lang.TypeNotPresentException", null, null,null, null,null,null,null,null);
34+
J.Try.Catch updatedCatch = catchClause.withParameter(catchClause.getParameter().withType(typeNotPresentExceptionType));
35+
// String multiCatchType = "java.lang.ArrayStoreException | java.lang.TypeNotPresentException";
36+
// updatedCatch = updatedCatch.withParameter(catchClause.getParameter().withType(JavaType.Class.build(multicatchType)));
37+
tryStmt = tryStmt.withCatches((List<J.Try.Catch>) Collections.singleton(updatedCatch));
38+
}
39+
}
40+
}
41+
return super.visitTry(tryStmt,ctx);
42+
}
43+
44+
private boolean containsGetAnnotation(J.Block body) {
45+
if(body == null) return false;
46+
for( J node : body.getStatements()) {
47+
if (node instanceof J.MethodInvocation) {
48+
J.MethodInvocation methodInvocation = (J.MethodInvocation) node;
49+
if ("getAnnotation".equals(methodInvocation.getSimpleName())) {
50+
return true;
51+
}
52+
}
53+
}
54+
return false;
55+
}
56+
};
57+
}
58+
}
59+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.openrewrite.java.migrate;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openrewrite.java.JavaParser;
5+
import org.openrewrite.test.RecipeSpec;
6+
import org.openrewrite.test.RewriteTest;
7+
8+
import static org.openrewrite.java.Assertions.java;
9+
import static org.openrewrite.java.Assertions.javaVersion;
10+
11+
public class TypeNotPresentExceptionTest implements RewriteTest {
12+
@Override
13+
public void defaults(RecipeSpec spec) {
14+
spec.recipe(new HandleTypeNotPresentExceptionInsteadOfArrayStoreException())
15+
.parser(JavaParser.fromJavaVersion()
16+
//language=java
17+
.dependsOn(
18+
"""
19+
import java.lang.annotation.*;
20+
import java.util.*;
21+
22+
public class Test {
23+
public void testMethod() {
24+
try {
25+
Object o = "test";
26+
o.getClass().getAnnotation(Override.class);
27+
} catch (ArrayStoreException e) {
28+
System.out.println("Caught ArrayStoreException");
29+
}
30+
}
31+
}
32+
""",
33+
"""
34+
import java.lang.annotation.*;
35+
import java.util.*;
36+
37+
public class Test {
38+
public void testMethod() {
39+
try {
40+
Object o = "test";
41+
o.getClass().getAnnotation(Override.class);
42+
} catch (TypeNotPresentException e) {
43+
System.out.println("Caught TypeNotPresentException");
44+
}
45+
}
46+
}
47+
"""
48+
)
49+
);
50+
}
51+
52+
53+
@Test
54+
void replaceGetLocalizedOutputStream() {
55+
rewriteRun(
56+
//language=java
57+
java(
58+
"""
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");
66+
}
67+
}
68+
}
69+
"""
70+
)
71+
);
72+
}
73+
}

0 commit comments

Comments
 (0)