Skip to content

Commit c48a656

Browse files
BhavanaPidapabhavanapidapatimtebeekgithub-actions[bot]
authored
Replace ArrayStoreException with TypeNotPresentException in try/catch using Class.getAnnotation() (#613)
* draft recipe and test case * Added the recipe to the list * Custom Recipe and test cases for changing exception type * Updated the display name * Added the copyright to the files * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Remove unnecessary trailing newline * Apply suggestions from code review * Fixed the code as mentioned in comments * Use a Precondition, ListUtils, and prevent unnecessary changes --------- Co-authored-by: bhavanapidapa <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <[email protected]>
1 parent 619a171 commit c48a656

File tree

3 files changed

+189
-0
lines changed

3 files changed

+189
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.java.migrate;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Preconditions;
20+
import org.openrewrite.Recipe;
21+
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.internal.ListUtils;
23+
import org.openrewrite.java.ChangeType;
24+
import org.openrewrite.java.JavaIsoVisitor;
25+
import org.openrewrite.java.search.FindMethods;
26+
import org.openrewrite.java.search.UsesMethod;
27+
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.TypeUtils;
29+
30+
public class ArrayStoreExceptionToTypeNotPresentException extends Recipe {
31+
32+
private static final String ARRAY_STORE_EXCEPTION = "java.lang.ArrayStoreException";
33+
private static final String TYPE_NOT_PRESENT_EXCEPTION = "java.lang.TypeNotPresentException";
34+
35+
@Override
36+
public String getDisplayName() {
37+
return "Catch `TypeNotPresentException` thrown by `Class.getAnnotation()`";
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return "Replace catch blocks for `ArrayStoreException` around `Class.getAnnotation()` with `TypeNotPresentException` to ensure compatibility with Java 11+.";
43+
}
44+
45+
@Override
46+
public TreeVisitor<?, ExecutionContext> getVisitor() {
47+
String classGetAnnotationPattern = "java.lang.Class getAnnotation(java.lang.Class)";
48+
return Preconditions.check(new UsesMethod<>(classGetAnnotationPattern), new JavaIsoVisitor<ExecutionContext>() {
49+
@Override
50+
public J.Try visitTry(J.Try tryStatement, ExecutionContext ctx) {
51+
J.Try try_ = super.visitTry(tryStatement, ctx);
52+
if (FindMethods.find(try_, classGetAnnotationPattern).isEmpty()) {
53+
return try_;
54+
}
55+
return try_.withCatches(ListUtils.map(try_.getCatches(), catch_ -> {
56+
if (TypeUtils.isOfClassType(catch_.getParameter().getType(), ARRAY_STORE_EXCEPTION)) {
57+
return (J.Try.Catch) new ChangeType(ARRAY_STORE_EXCEPTION, TYPE_NOT_PRESENT_EXCEPTION, true)
58+
.getVisitor().visit(catch_, ctx);
59+
}
60+
return catch_;
61+
}));
62+
}
63+
});
64+
}
65+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ 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.ArrayStoreExceptionToTypeNotPresentException
77+
7678
---
7779
type: specs.openrewrite.org/v1beta/recipe
7880
name: org.openrewrite.java.migrate.UpgradeBuildToJava11
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.java.migrate;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.java;
24+
25+
class ArrayStoreExceptionToTypeNotPresentExceptionTest implements RewriteTest {
26+
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec.recipe(new ArrayStoreExceptionToTypeNotPresentException());
30+
}
31+
32+
@DocumentExample
33+
@Test
34+
void replaceCaughtException() {
35+
rewriteRun(
36+
//language=java
37+
java(
38+
"""
39+
import java.lang.annotation.*;
40+
import java.util.*;
41+
42+
public class Test {
43+
public void testMethod() {
44+
try {
45+
Object o = "test";
46+
o.getClass().getAnnotation(Override.class);
47+
} catch (ArrayStoreException e) {
48+
System.out.println("Caught Exception");
49+
}
50+
try {
51+
Object.class.getAnnotation(Override.class);
52+
} catch (ArrayStoreException e) {
53+
System.out.println("Caught ArrayStoreException");
54+
}
55+
}
56+
}
57+
""",
58+
"""
59+
import java.lang.annotation.*;
60+
import java.util.*;
61+
62+
public class Test {
63+
public void testMethod() {
64+
try {
65+
Object o = "test";
66+
o.getClass().getAnnotation(Override.class);
67+
} catch (TypeNotPresentException e) {
68+
System.out.println("Caught Exception");
69+
}
70+
try {
71+
Object.class.getAnnotation(Override.class);
72+
} catch (TypeNotPresentException e) {
73+
System.out.println("Caught ArrayStoreException");
74+
}
75+
}
76+
}
77+
"""
78+
)
79+
);
80+
}
81+
82+
@Test
83+
void retainOtherCaughtExceptions() {
84+
rewriteRun(
85+
//language=java
86+
java(
87+
"""
88+
public class Test {
89+
public void testMethod() {
90+
try {
91+
Object o = "test";
92+
o.getClass().getAnnotation(Override.class);
93+
} catch (NullPointerException e) {
94+
System.out.println("Caught Exception");
95+
}
96+
}
97+
}
98+
"""
99+
)
100+
);
101+
}
102+
103+
@Test
104+
void retainArrayStoreExceptionWithoutClassGetAnnotation() {
105+
rewriteRun(
106+
//language=java
107+
java(
108+
"""
109+
public class Test {
110+
public void testMethod() {
111+
try {
112+
Object o = "test";
113+
} catch (ArrayStoreException e) {
114+
System.out.println("Caught Exception");
115+
}
116+
}
117+
}
118+
"""
119+
)
120+
);
121+
}
122+
}

0 commit comments

Comments
 (0)