Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe IllegalArgumentExceptionToAlreadyConnectedException #615

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.ChangeType;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.search.FindMethods;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.Map;

public class MethodExceptionReplacerRecipe extends Recipe {
private final Map<String, Map<String, String>> methodToExceptionMapping;

public MethodExceptionReplacerRecipe(Map<String, Map<String, String>> methodToExceptionMapping) {
this.methodToExceptionMapping = methodToExceptionMapping;
}
BhavanaPidapa marked this conversation as resolved.
Show resolved Hide resolved

@Override
public String getDisplayName() {
return "Generic Recipe to Exception Replacement based on method signatures";
}

@Override
public String getDescription() {
return "This recipe replaces specified exceptions with other exceptions based on method signatures.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.Try visitTry(J.Try tryStatement, ExecutionContext ctx) {
J.Try try_ = super.visitTry(tryStatement, ctx);

for (Map.Entry<String, Map<String, String>> entry : methodToExceptionMapping.entrySet()) {
String methodPattern = entry.getKey();
Map<String, String> exceptionMapping = entry.getValue();

if (FindMethods.find(try_, methodPattern).isEmpty()) {
continue;
}

for (Map.Entry<String, String> exceptionEntry : exceptionMapping.entrySet()) {
String oldException = exceptionEntry.getKey();
String newException = exceptionEntry.getValue();
try_ = try_.withCatches(ListUtils.map(try_.getCatches(), catch_ -> {
if (TypeUtils.isOfClassType(catch_.getParameter().getType(), oldException)) {
return (J.Try.Catch) new ChangeType(oldException, newException, true)
.getVisitor().visit(catch_, ctx);
}
return catch_;
}));
}
}

return try_;
}
};
}
}
19 changes: 18 additions & 1 deletion src/main/resources/META-INF/rewrite/java-version-11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ recipeList:
- org.openrewrite.java.migrate.ReplaceComSunAWTUtilitiesMethods
- org.openrewrite.java.migrate.ReplaceLocalizedStreamMethods
- org.openrewrite.java.migrate.ArrayStoreExceptionToTypeNotPresentException
- org.openrewrite.java.migrate.MethodExceptionReplacerRecipe

---
type: specs.openrewrite.org/v1beta/recipe
Expand Down Expand Up @@ -290,4 +291,20 @@ recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: java.nio.file.Path get(..)
newMethodName: of

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.MethodExceptionReplacerRecipe
displayName: Replace `javax.security.auth.Policy` with `java.security.Policy`
description: The `javax.security.auth.Policy` class is not available from Java SE 11 onwards.
tags:
- java11
recipeList:
- org.openrewrite.java.migrate.MethodExceptionReplacerRecipe:
methodToExceptionMapping:
"java.nio.channels.DatagramChannel send(java.nio.ByteBuffer, java.net.SocketAddress)":
"java.lang.IllegalArgumentException": "java.nio.channels.AlreadyConnectedException"
- org.openrewrite.java.migrate.MethodExceptionReplacerRecipe:
methodToExceptionMapping:
"java.lang.Class getAnnotation(java.lang.Class)":
"java.lang.ArrayStoreException": "java.lang.TypeNotPresentException"
---
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ArrayStoreExceptionToTypeNotPresentExceptionTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ArrayStoreExceptionToTypeNotPresentException());
spec.recipeFromResources("org.openrewrite.java.migrate.MethodExceptionReplacerRecipe");
}

@DocumentExample
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class IllegalArgumentExceptionToAlreadyConnectedExceptionTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipeFromResources("org.openrewrite.java.migrate.MethodExceptionReplacerRecipe");
}

@DocumentExample
@Test
void replaceCaughtException() {
rewriteRun(
//language=java
java(
"""
import java.nio.ByteBuffer;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;

public class Test {
public void sendData() {
try {
DatagramChannel channel = DatagramChannel.open();
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
} catch (IllegalArgumentException e) {
System.out.println("Caught Exception");
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("DatagramChannel already connected to a different address");
}
}
}
""",
"""
import java.nio.ByteBuffer;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;

public class Test {
public void sendData() {
try {
DatagramChannel channel = DatagramChannel.open();
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
} catch (AlreadyConnectedException e) {
System.out.println("Caught Exception");
} catch (AlreadyConnectedException e) {
throw new AlreadyConnectedException("DatagramChannel already connected to a different address");
}
}
}
"""
)
);
}

@Test
void retainOtherCaughtExceptions() {
rewriteRun(
//language=java
java(
"""
import java.io.IOException;import java.nio.ByteBuffer;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;

public class Test {
public void sendData() {
try {
DatagramChannel channel = DatagramChannel.open();
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
} catch (IOException e) {
System.out.println("Caught Exception");
}
}
}
"""
)
);
}

@Test
void retainIllegalArgumentExceptionWithoutChannelSendAnnotation() {
rewriteRun(
//language=java
java(
"""
import java.nio.ByteBuffer;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;

public class Test {
public void sendData() {
try {
DatagramChannel channel = DatagramChannel.open();
} catch (IllegalArgumentException e) {
System.out.println("Caught Exception");
}
}
}
"""
)
);
}
}
Loading