Skip to content

Commit df39941

Browse files
author
bhavanapidapa
committed
Dedicate Recipe as mentioned in comments.
1 parent 8af6188 commit df39941

File tree

4 files changed

+66
-203
lines changed

4 files changed

+66
-203
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 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+
return (J.Try.Catch) new ChangeType(ILLEGAL_ARGUMENT_EXCEPTION, ALREADY_CONNECTED_EXCEPTION, true)
58+
.getVisitor().visit(catch_, ctx);
59+
}
60+
return catch_;
61+
}));
62+
}
63+
});
64+
}
65+
}

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

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

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

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

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

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

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

3232
@DocumentExample

0 commit comments

Comments
 (0)