|
| 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 | +} |
0 commit comments