Skip to content

Commit 7bd8c82

Browse files
author
bhavanapidapa
committed
Generic Recipe to exception replacement based on method signature
1 parent 59d56a6 commit 7bd8c82

File tree

5 files changed

+225
-2
lines changed

5 files changed

+225
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package org.openrewrite.java.migrate;public class IllegalArgumentExceptionToAlreadyConnectedException {
2+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.*;
19+
import org.openrewrite.internal.ListUtils;
20+
import org.openrewrite.java.ChangeType;
21+
import org.openrewrite.java.JavaIsoVisitor;
22+
import org.openrewrite.java.search.FindMethods;
23+
import org.openrewrite.java.tree.J;
24+
import org.openrewrite.java.tree.TypeUtils;
25+
26+
import java.util.Map;
27+
28+
public class MethodExceptionReplacerRecipe extends Recipe {
29+
private final Map<String, Map<String, String>> methodToExceptionMapping;
30+
31+
public MethodExceptionReplacerRecipe(Map<String, Map<String, String>> methodToExceptionMapping) {
32+
this.methodToExceptionMapping = methodToExceptionMapping;
33+
}
34+
35+
@Override
36+
public String getDisplayName() {
37+
return "Generic Recipe to Exception Replacement based on method signatures";
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return "This recipe replaces specified exceptions with other exceptions based on method signatures.";
43+
}
44+
45+
@Override
46+
public TreeVisitor<?, ExecutionContext> getVisitor() {
47+
return new JavaIsoVisitor<ExecutionContext>() {
48+
49+
@Override
50+
public J.Try visitTry(J.Try tryStatement, ExecutionContext ctx) {
51+
J.Try try_ = super.visitTry(tryStatement, ctx);
52+
53+
for (Map.Entry<String, Map<String, String>> entry : methodToExceptionMapping.entrySet()) {
54+
String methodPattern = entry.getKey();
55+
Map<String, String> exceptionMapping = entry.getValue();
56+
57+
if (FindMethods.find(try_, methodPattern).isEmpty()) {
58+
continue;
59+
}
60+
61+
for (Map.Entry<String, String> exceptionEntry : exceptionMapping.entrySet()) {
62+
String oldException = exceptionEntry.getKey();
63+
String newException = exceptionEntry.getValue();
64+
try_ = try_.withCatches(ListUtils.map(try_.getCatches(), catch_ -> {
65+
if (TypeUtils.isOfClassType(catch_.getParameter().getType(), oldException)) {
66+
return (J.Try.Catch) new ChangeType(oldException, newException, true)
67+
.getVisitor().visit(catch_, ctx);
68+
}
69+
return catch_;
70+
}));
71+
}
72+
}
73+
74+
return try_;
75+
}
76+
};
77+
}
78+
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ recipeList:
7474
- org.openrewrite.java.migrate.ReplaceComSunAWTUtilitiesMethods
7575
- org.openrewrite.java.migrate.ReplaceLocalizedStreamMethods
7676
- org.openrewrite.java.migrate.ArrayStoreExceptionToTypeNotPresentException
77+
- org.openrewrite.java.migrate.MethodExceptionReplacerRecipe
7778

7879
---
7980
type: specs.openrewrite.org/v1beta/recipe
@@ -290,4 +291,20 @@ recipeList:
290291
- org.openrewrite.java.ChangeMethodName:
291292
methodPattern: java.nio.file.Path get(..)
292293
newMethodName: of
293-
294+
---
295+
type: specs.openrewrite.org/v1beta/recipe
296+
name: org.openrewrite.java.migrate.MethodExceptionReplacerRecipe
297+
displayName: Replace `javax.security.auth.Policy` with `java.security.Policy`
298+
description: The `javax.security.auth.Policy` class is not available from Java SE 11 onwards.
299+
tags:
300+
- java11
301+
recipeList:
302+
- org.openrewrite.java.migrate.MethodExceptionReplacerRecipe:
303+
methodToExceptionMapping:
304+
"java.nio.channels.DatagramChannel send(java.nio.ByteBuffer, java.net.SocketAddress)":
305+
"java.lang.IllegalArgumentException": "java.nio.channels.AlreadyConnectedException"
306+
- org.openrewrite.java.migrate.MethodExceptionReplacerRecipe:
307+
methodToExceptionMapping:
308+
"java.lang.Class getAnnotation(java.lang.Class)":
309+
"java.lang.ArrayStoreException": "java.lang.TypeNotPresentException"
310+
---

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ArrayStoreExceptionToTypeNotPresentExceptionTest implements RewriteTest {
2626

2727
@Override
2828
public void defaults(RecipeSpec spec) {
29-
spec.recipe(new ArrayStoreExceptionToTypeNotPresentException());
29+
spec.recipeFromResources("org.openrewrite.java.migrate.MethodExceptionReplacerRecipe");
3030
}
3131

3232
@DocumentExample
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 IllegalArgumentExceptionToAlreadyConnectedExceptionTest implements RewriteTest {
26+
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec.recipeFromResources("org.openrewrite.java.migrate.MethodExceptionReplacerRecipe");
30+
}
31+
32+
@DocumentExample
33+
@Test
34+
void replaceCaughtException() {
35+
rewriteRun(
36+
//language=java
37+
java(
38+
"""
39+
import java.nio.ByteBuffer;
40+
import java.net.SocketAddress;
41+
import java.nio.channels.DatagramChannel;
42+
43+
public class Test {
44+
public void sendData() {
45+
try {
46+
DatagramChannel channel = DatagramChannel.open();
47+
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
48+
} catch (IllegalArgumentException e) {
49+
System.out.println("Caught Exception");
50+
} catch (IllegalArgumentException e) {
51+
throw new IllegalArgumentException("DatagramChannel already connected to a different address");
52+
}
53+
}
54+
}
55+
""",
56+
"""
57+
import java.nio.ByteBuffer;
58+
import java.net.SocketAddress;
59+
import java.nio.channels.DatagramChannel;
60+
61+
public class Test {
62+
public void sendData() {
63+
try {
64+
DatagramChannel channel = DatagramChannel.open();
65+
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
66+
} catch (AlreadyConnectedException e) {
67+
System.out.println("Caught Exception");
68+
} catch (AlreadyConnectedException e) {
69+
throw new AlreadyConnectedException("DatagramChannel already connected to a different address");
70+
}
71+
}
72+
}
73+
"""
74+
)
75+
);
76+
}
77+
78+
@Test
79+
void retainOtherCaughtExceptions() {
80+
rewriteRun(
81+
//language=java
82+
java(
83+
"""
84+
import java.io.IOException;import java.nio.ByteBuffer;
85+
import java.net.SocketAddress;
86+
import java.nio.channels.DatagramChannel;
87+
88+
public class Test {
89+
public void sendData() {
90+
try {
91+
DatagramChannel channel = DatagramChannel.open();
92+
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
93+
} catch (IOException e) {
94+
System.out.println("Caught Exception");
95+
}
96+
}
97+
}
98+
"""
99+
)
100+
);
101+
}
102+
103+
@Test
104+
void retainIllegalArgumentExceptionWithoutChannelSendAnnotation() {
105+
rewriteRun(
106+
//language=java
107+
java(
108+
"""
109+
import java.nio.ByteBuffer;
110+
import java.net.SocketAddress;
111+
import java.nio.channels.DatagramChannel;
112+
113+
public class Test {
114+
public void sendData() {
115+
try {
116+
DatagramChannel channel = DatagramChannel.open();
117+
} catch (IllegalArgumentException e) {
118+
System.out.println("Caught Exception");
119+
}
120+
}
121+
}
122+
"""
123+
)
124+
);
125+
}
126+
}

0 commit comments

Comments
 (0)