Skip to content

Commit f43c8ce

Browse files
BhavanaPidapabhavanapidapatimtebeekgithub-actions[bot]
authored
Recipe IllegalArgumentExceptionToAlreadyConnectedException (#615)
* Generic Recipe to exception replacement based on method signature * Delete src/main/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedException.java * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Updating the recipe name * Dedicate Recipe as mentioned in comments. * Updated few changes in yml file * Restore ArrayStoreExceptionToTypeNotPresentExceptionTest * Split the tests to catch and rethrow the exception * Add now required import --------- 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>
1 parent 0bd6e06 commit f43c8ce

File tree

3 files changed

+233
-1
lines changed

3 files changed

+233
-1
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 IllegalArgumentExceptionToAlreadyConnectedException extends Recipe {
31+
32+
private static final String ILLEGAL_ARGUMENT_EXCEPTION = "java.lang.IllegalArgumentException";
33+
private static final String ALREADY_CONNECTED_EXCEPTION = "java.nio.channels.AlreadyConnectedException";
34+
35+
@Override
36+
public String getDisplayName() {
37+
return "Replace `IllegalArgumentException` with `AlreadyConnectedException` in `DatagramChannel.send()` method";
38+
}
39+
40+
@Override
41+
public String getDescription() {
42+
return "Replace `IllegalArgumentException` with `AlreadyConnectedException` for DatagramChannel.send() to ensure compatibility with Java 11+.";
43+
}
44+
45+
@Override
46+
public TreeVisitor<?, ExecutionContext> getVisitor() {
47+
String datagramChannelSendMethodPattern = "java.nio.channels.DatagramChannel send(java.nio.ByteBuffer, java.net.SocketAddress)";
48+
return Preconditions.check(new UsesMethod<>(datagramChannelSendMethodPattern), 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_, datagramChannelSendMethodPattern).isEmpty()) {
53+
return try_;
54+
}
55+
return try_.withCatches(ListUtils.map(try_.getCatches(), catch_ -> {
56+
if (TypeUtils.isOfClassType(catch_.getParameter().getType(), ILLEGAL_ARGUMENT_EXCEPTION)) {
57+
maybeAddImport(ALREADY_CONNECTED_EXCEPTION);
58+
return (J.Try.Catch) new ChangeType(ILLEGAL_ARGUMENT_EXCEPTION, ALREADY_CONNECTED_EXCEPTION, true)
59+
.getVisitor().visit(catch_, ctx);
60+
}
61+
return catch_;
62+
}));
63+
}
64+
});
65+
}
66+
}

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

Lines changed: 1 addition & 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.IllegalArgumentExceptionToAlreadyConnectedException
7778

7879
---
7980
type: specs.openrewrite.org/v1beta/recipe
@@ -290,4 +291,3 @@ recipeList:
290291
- org.openrewrite.java.ChangeMethodName:
291292
methodPattern: java.nio.file.Path get(..)
292293
newMethodName: of
293-
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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.recipe(new IllegalArgumentExceptionToAlreadyConnectedException());
30+
}
31+
32+
@DocumentExample
33+
@Test
34+
void catchException() {
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+
class Test {
44+
void sendDataCatch() {
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+
}
51+
}
52+
}
53+
""",
54+
"""
55+
import java.nio.ByteBuffer;
56+
import java.nio.channels.AlreadyConnectedException;
57+
import java.net.SocketAddress;
58+
import java.nio.channels.DatagramChannel;
59+
60+
class Test {
61+
void sendDataCatch() {
62+
try {
63+
DatagramChannel channel = DatagramChannel.open();
64+
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
65+
} catch (AlreadyConnectedException e) {
66+
System.out.println("Caught Exception");
67+
}
68+
}
69+
}
70+
"""
71+
)
72+
);
73+
}
74+
75+
@Test
76+
void rethrowException() {
77+
rewriteRun(
78+
//language=java
79+
java(
80+
"""
81+
import java.nio.ByteBuffer;
82+
import java.net.SocketAddress;
83+
import java.nio.channels.DatagramChannel;
84+
85+
class Test {
86+
void sendDataRethrow() {
87+
try {
88+
DatagramChannel channel = DatagramChannel.open();
89+
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
90+
} catch (IllegalArgumentException e) {
91+
throw new IllegalArgumentException("DatagramChannel already connected to a different address");
92+
}
93+
}
94+
}
95+
""",
96+
"""
97+
import java.nio.ByteBuffer;
98+
import java.nio.channels.AlreadyConnectedException;
99+
import java.net.SocketAddress;
100+
import java.nio.channels.DatagramChannel;
101+
102+
class Test {
103+
void sendDataRethrow() {
104+
try {
105+
DatagramChannel channel = DatagramChannel.open();
106+
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
107+
} catch (AlreadyConnectedException e) {
108+
throw new AlreadyConnectedException("DatagramChannel already connected to a different address");
109+
}
110+
}
111+
}
112+
"""
113+
)
114+
);
115+
}
116+
117+
@Test
118+
void retainOtherCaughtExceptions() {
119+
rewriteRun(
120+
//language=java
121+
java(
122+
"""
123+
import java.io.IOException;
124+
import java.nio.ByteBuffer;
125+
import java.net.SocketAddress;
126+
import java.nio.channels.DatagramChannel;
127+
128+
class Test {
129+
void sendData() {
130+
try {
131+
DatagramChannel channel = DatagramChannel.open();
132+
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
133+
} catch (IOException e) {
134+
System.out.println("Caught Exception");
135+
}
136+
}
137+
}
138+
"""
139+
)
140+
);
141+
}
142+
143+
@Test
144+
void retainIllegalArgumentExceptionWithoutChannelSend() {
145+
rewriteRun(
146+
//language=java
147+
java(
148+
"""
149+
import java.nio.ByteBuffer;
150+
import java.net.SocketAddress;
151+
import java.nio.channels.DatagramChannel;
152+
153+
public class Test {
154+
public void sendData() {
155+
try {
156+
DatagramChannel channel = DatagramChannel.open();
157+
} catch (IllegalArgumentException e) {
158+
System.out.println("Caught Exception");
159+
}
160+
}
161+
}
162+
"""
163+
)
164+
);
165+
}
166+
}

0 commit comments

Comments
 (0)