From 0ad2c51b72a56dd021687fa1be821f466a45ad11 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 7 Dec 2024 22:34:44 +0100 Subject: [PATCH] Add now required import --- ...tExceptionToAlreadyConnectedException.java | 1 + ...eptionToAlreadyConnectedExceptionTest.java | 25 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedException.java b/src/main/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedException.java index 2a6b45b7c..a430e3723 100644 --- a/src/main/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedException.java +++ b/src/main/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedException.java @@ -54,6 +54,7 @@ public J.Try visitTry(J.Try tryStatement, ExecutionContext ctx) { } return try_.withCatches(ListUtils.map(try_.getCatches(), catch_ -> { if (TypeUtils.isOfClassType(catch_.getParameter().getType(), ILLEGAL_ARGUMENT_EXCEPTION)) { + maybeAddImport(ALREADY_CONNECTED_EXCEPTION); return (J.Try.Catch) new ChangeType(ILLEGAL_ARGUMENT_EXCEPTION, ALREADY_CONNECTED_EXCEPTION, true) .getVisitor().visit(catch_, ctx); } diff --git a/src/test/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedExceptionTest.java b/src/test/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedExceptionTest.java index 68bbfc525..1fd0d0214 100644 --- a/src/test/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedExceptionTest.java +++ b/src/test/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedExceptionTest.java @@ -40,8 +40,8 @@ void catchException() { import java.net.SocketAddress; import java.nio.channels.DatagramChannel; - public class Test { - public void sendDataCatch() { + class Test { + void sendDataCatch() { try { DatagramChannel channel = DatagramChannel.open(); channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080)); @@ -53,11 +53,12 @@ public void sendDataCatch() { """, """ import java.nio.ByteBuffer; + import java.nio.channels.AlreadyConnectedException; import java.net.SocketAddress; import java.nio.channels.DatagramChannel; - public class Test { - public void sendDataCatch() { + class Test { + void sendDataCatch() { try { DatagramChannel channel = DatagramChannel.open(); channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080)); @@ -81,8 +82,8 @@ void rethrowException() { import java.net.SocketAddress; import java.nio.channels.DatagramChannel; - public class Test { - public void sendDataRethrow() { + class Test { + void sendDataRethrow() { try { DatagramChannel channel = DatagramChannel.open(); channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080)); @@ -94,11 +95,12 @@ public void sendDataRethrow() { """, """ import java.nio.ByteBuffer; + import java.nio.channels.AlreadyConnectedException; import java.net.SocketAddress; import java.nio.channels.DatagramChannel; - public class Test { - public void sendDataRethrow() { + class Test { + void sendDataRethrow() { try { DatagramChannel channel = DatagramChannel.open(); channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080)); @@ -118,12 +120,13 @@ void retainOtherCaughtExceptions() { //language=java java( """ - import java.io.IOException;import java.nio.ByteBuffer; + import java.io.IOException; + import java.nio.ByteBuffer; import java.net.SocketAddress; import java.nio.channels.DatagramChannel; - public class Test { - public void sendData() { + class Test { + void sendData() { try { DatagramChannel channel = DatagramChannel.open(); channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));