From ffc2e1505b75cf7baae358ded1f887437b28c709 Mon Sep 17 00:00:00 2001 From: bhavanapidapa Date: Tue, 19 Nov 2024 14:09:48 -0600 Subject: [PATCH 01/11] draft recipe and test case --- ...ExceptionInsteadOfArrayStoreException.java | 59 +++++++++++++++ .../migrate/TypeNotPresentExceptionTest.java | 73 +++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/migrate/HandleTypeNotPresentExceptionInsteadOfArrayStoreException.java create mode 100644 src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java diff --git a/src/main/java/org/openrewrite/java/migrate/HandleTypeNotPresentExceptionInsteadOfArrayStoreException.java b/src/main/java/org/openrewrite/java/migrate/HandleTypeNotPresentExceptionInsteadOfArrayStoreException.java new file mode 100644 index 0000000000..eb27db6ac5 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/HandleTypeNotPresentExceptionInsteadOfArrayStoreException.java @@ -0,0 +1,59 @@ +package org.openrewrite.java.migrate; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaVisitor; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; + +import java.util.Collections; +import java.util.List; + +public class HandleTypeNotPresentExceptionInsteadOfArrayStoreException extends Recipe { + @Override + public String getDisplayName() { + return "Handle TypeNotPresentException instead of ArrayStoreException"; + } + + @Override + public String getDescription() { + return "This recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException or both exceptions, to ensure compatibility with Java 11+."; + } + + @Override + public TreeVisitor getVisitor() { + return new JavaVisitor() { + @Override + public J visitTry(J.Try tryStmt, ExecutionContext ctx ) { + if(containsGetAnnotation(tryStmt.getBody())) + { + for (J.Try.Catch catchClause : tryStmt.getCatches()) { + if(catchClause.getParameter().getType() != null && "java.lang.ArrayStoreException".equals(catchClause.getParameter().getType().toString())) { + JavaType.Class typeNotPresentExceptionType = new JavaType.Class(null,0,"java.lang.TypeNotPresentException", null, null,null, null,null,null,null,null); + J.Try.Catch updatedCatch = catchClause.withParameter(catchClause.getParameter().withType(typeNotPresentExceptionType)); +// String multiCatchType = "java.lang.ArrayStoreException | java.lang.TypeNotPresentException"; +// updatedCatch = updatedCatch.withParameter(catchClause.getParameter().withType(JavaType.Class.build(multicatchType))); + tryStmt = tryStmt.withCatches((List) Collections.singleton(updatedCatch)); + } + } + } + return super.visitTry(tryStmt,ctx); + } + + private boolean containsGetAnnotation(J.Block body) { + if(body == null) return false; + for( J node : body.getStatements()) { + if (node instanceof J.MethodInvocation) { + J.MethodInvocation methodInvocation = (J.MethodInvocation) node; + if ("getAnnotation".equals(methodInvocation.getSimpleName())) { + return true; + } + } + } + return false; +} + }; + } +} + diff --git a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java new file mode 100644 index 0000000000..c10264c350 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java @@ -0,0 +1,73 @@ +package org.openrewrite.java.migrate; + +import org.junit.jupiter.api.Test; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.java.Assertions.javaVersion; + +public class TypeNotPresentExceptionTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new HandleTypeNotPresentExceptionInsteadOfArrayStoreException()) + .parser(JavaParser.fromJavaVersion() + //language=java + .dependsOn( + """ + import java.lang.annotation.*; + import java.util.*; + + public class Test { + public void testMethod() { + try { + Object o = "test"; + o.getClass().getAnnotation(Override.class); + } catch (ArrayStoreException e) { + System.out.println("Caught ArrayStoreException"); + } + } + } + """, + """ + import java.lang.annotation.*; + import java.util.*; + + public class Test { + public void testMethod() { + try { + Object o = "test"; + o.getClass().getAnnotation(Override.class); + } catch (TypeNotPresentException e) { + System.out.println("Caught TypeNotPresentException"); + } + } + } + """ + ) + ); + } + + + @Test + void replaceGetLocalizedOutputStream() { + rewriteRun( + //language=java + java( + """ + public class Test { + public void testMethod() { + try { + Object o = "test"; + o.getClass().getAnnotation(Override.class); + } catch (NullPointerException e) { + System.out.println("Caught NullPointerException"); + } + } + } + """ + ) + ); + } +} From 81f9345c6e43858558a400452ef4b43f17621aff Mon Sep 17 00:00:00 2001 From: bhavanapidapa Date: Tue, 19 Nov 2024 14:10:31 -0600 Subject: [PATCH 02/11] Added the recipe to the list --- src/main/resources/META-INF/rewrite/java-version-11.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index eb3e7cf90f..1c59df6255 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -74,6 +74,7 @@ recipeList: lightweightPeerFQCN: java.awt.peer.LightweightPeer - org.openrewrite.scala.migrate.UpgradeScala_2_12 - org.openrewrite.java.migrate.ReplaceLocalizedStreamMethods + - org.openrewrite.java.migrate.HandleTypeNotPresentExceptionInsteadOfArrayStoreException --- type: specs.openrewrite.org/v1beta/recipe From 8471357e256e7fbf1378c9d6ee24694b194ed1d4 Mon Sep 17 00:00:00 2001 From: bhavanapidapa Date: Fri, 22 Nov 2024 15:00:40 -0600 Subject: [PATCH 03/11] Custom Recipe and test cases for changing exception type --- ...oreExceptionToTypeNotPresentException.java | 61 +++++++++++++++++++ ...ExceptionInsteadOfArrayStoreException.java | 59 ------------------ .../META-INF/rewrite/java-version-11.yml | 2 +- .../migrate/TypeNotPresentExceptionTest.java | 51 ++++++++++------ 4 files changed, 93 insertions(+), 80 deletions(-) create mode 100644 src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java delete mode 100644 src/main/java/org/openrewrite/java/migrate/HandleTypeNotPresentExceptionInsteadOfArrayStoreException.java diff --git a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java new file mode 100644 index 0000000000..d0bff5eb2e --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java @@ -0,0 +1,61 @@ +package org.openrewrite.java.migrate; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.ChangeType; +import org.openrewrite.java.JavaVisitor; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +public class ArrayStoreExceptionToTypeNotPresentException extends Recipe { + @Override + public String getDisplayName() { + return "Handle TypeNotPresentException instead of ArrayStoreException"; + } + + @Override + public String getDescription() { + return "This recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException or both exceptions, to ensure compatibility with Java 11+."; + } + + @Override + public TreeVisitor getVisitor() { + final MethodMatcher classGetAnnotationMethod = new MethodMatcher("java.lang.Class getAnnotation(java.lang.Class)"); + return new JavaVisitor() { + + @Override + public J visitTry(J.Try tryStmt, ExecutionContext ctx) { + boolean flag = false; + if (containsGetAnnotation(tryStmt)) { + List updatedCatches = new ArrayList<>(); + for (J.Try.Catch catchClause : tryStmt.getCatches()) { + if (catchClause.getParameter().getType() != null && catchClause.getParameter().getType().isAssignableFrom(Pattern.compile("java.lang.ArrayStoreException"))) { + J.Try.Catch updatedCatch = (J.Try.Catch) new ChangeType("java.lang.ArrayStoreException", "java.lang.TypeNotPresentException", true).getVisitor().visit(catchClause, ctx); + updatedCatches.add(updatedCatch); + flag = true; + } else { + updatedCatches.add(catchClause); + } + } + if (flag) + tryStmt = tryStmt.withCatches(updatedCatches); + } + return super.visitTry(tryStmt, ctx); + } + + private boolean containsGetAnnotation(J.Try tryStmt) { + return tryStmt.getBody().getStatements().stream() + .filter(Expression.class::isInstance) + .map(Expression.class::cast) + .anyMatch(classGetAnnotationMethod::matches); + } + }; + } +} + diff --git a/src/main/java/org/openrewrite/java/migrate/HandleTypeNotPresentExceptionInsteadOfArrayStoreException.java b/src/main/java/org/openrewrite/java/migrate/HandleTypeNotPresentExceptionInsteadOfArrayStoreException.java deleted file mode 100644 index eb27db6ac5..0000000000 --- a/src/main/java/org/openrewrite/java/migrate/HandleTypeNotPresentExceptionInsteadOfArrayStoreException.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.openrewrite.java.migrate; - -import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; -import org.openrewrite.java.JavaVisitor; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.JavaType; - -import java.util.Collections; -import java.util.List; - -public class HandleTypeNotPresentExceptionInsteadOfArrayStoreException extends Recipe { - @Override - public String getDisplayName() { - return "Handle TypeNotPresentException instead of ArrayStoreException"; - } - - @Override - public String getDescription() { - return "This recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException or both exceptions, to ensure compatibility with Java 11+."; - } - - @Override - public TreeVisitor getVisitor() { - return new JavaVisitor() { - @Override - public J visitTry(J.Try tryStmt, ExecutionContext ctx ) { - if(containsGetAnnotation(tryStmt.getBody())) - { - for (J.Try.Catch catchClause : tryStmt.getCatches()) { - if(catchClause.getParameter().getType() != null && "java.lang.ArrayStoreException".equals(catchClause.getParameter().getType().toString())) { - JavaType.Class typeNotPresentExceptionType = new JavaType.Class(null,0,"java.lang.TypeNotPresentException", null, null,null, null,null,null,null,null); - J.Try.Catch updatedCatch = catchClause.withParameter(catchClause.getParameter().withType(typeNotPresentExceptionType)); -// String multiCatchType = "java.lang.ArrayStoreException | java.lang.TypeNotPresentException"; -// updatedCatch = updatedCatch.withParameter(catchClause.getParameter().withType(JavaType.Class.build(multicatchType))); - tryStmt = tryStmt.withCatches((List) Collections.singleton(updatedCatch)); - } - } - } - return super.visitTry(tryStmt,ctx); - } - - private boolean containsGetAnnotation(J.Block body) { - if(body == null) return false; - for( J node : body.getStatements()) { - if (node instanceof J.MethodInvocation) { - J.MethodInvocation methodInvocation = (J.MethodInvocation) node; - if ("getAnnotation".equals(methodInvocation.getSimpleName())) { - return true; - } - } - } - return false; -} - }; - } -} - diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index effcb261a7..2ce31ac4ad 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -73,7 +73,7 @@ recipeList: - org.openrewrite.scala.migrate.UpgradeScala_2_12 - org.openrewrite.java.migrate.ReplaceComSunAWTUtilitiesMethods - org.openrewrite.java.migrate.ReplaceLocalizedStreamMethods - - org.openrewrite.java.migrate.HandleTypeNotPresentExceptionInsteadOfArrayStoreException + - org.openrewrite.java.migrate.ArrayStoreExceptionToTypeNotPresentException --- type: specs.openrewrite.org/v1beta/recipe diff --git a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java index c10264c350..1ac9d39d9c 100644 --- a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java +++ b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java @@ -1,29 +1,36 @@ package org.openrewrite.java.migrate; import org.junit.jupiter.api.Test; -import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; -import static org.openrewrite.java.Assertions.javaVersion; public class TypeNotPresentExceptionTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new HandleTypeNotPresentExceptionInsteadOfArrayStoreException()) - .parser(JavaParser.fromJavaVersion() - //language=java - .dependsOn( + spec.recipe(new ArrayStoreExceptionToTypeNotPresentException()); + } + + @Test + void ArrayStoreExceptionTest() { + rewriteRun( + //language=java + java( """ import java.lang.annotation.*; import java.util.*; - + public class Test { public void testMethod() { try { Object o = "test"; o.getClass().getAnnotation(Override.class); + } catch (ArrayStoreException e) { + System.out.println("Caught Exception"); + } + try { + Object.class.getAnnotation(Override.class); } catch (ArrayStoreException e) { System.out.println("Caught ArrayStoreException"); } @@ -33,14 +40,19 @@ public void testMethod() { """ import java.lang.annotation.*; import java.util.*; - + public class Test { public void testMethod() { try { Object o = "test"; o.getClass().getAnnotation(Override.class); } catch (TypeNotPresentException e) { - System.out.println("Caught TypeNotPresentException"); + System.out.println("Caught Exception"); + } + try { + Object.class.getAnnotation(Override.class); + } catch (TypeNotPresentException e) { + System.out.println("Caught ArrayStoreException"); } } } @@ -49,24 +61,23 @@ public void testMethod() { ); } - @Test - void replaceGetLocalizedOutputStream() { + void rOtherExceptionsTest() { rewriteRun( //language=java java( """ - public class Test { - public void testMethod() { - try { - Object o = "test"; - o.getClass().getAnnotation(Override.class); - } catch (NullPointerException e) { - System.out.println("Caught NullPointerException"); + public class Test { + public void testMethod() { + try { + Object o = "test"; + o.getClass().getAnnotation(Override.class); + } catch (NullPointerException e) { + System.out.println("Caught Exception"); + } } } - } - """ + """ ) ); } From d2a31afc62fa806bf6f0fe3ef335f8c194dc61be Mon Sep 17 00:00:00 2001 From: bhavanapidapa Date: Fri, 22 Nov 2024 15:11:14 -0600 Subject: [PATCH 04/11] Updated the display name --- .../migrate/ArrayStoreExceptionToTypeNotPresentException.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java index d0bff5eb2e..69e6b413e9 100644 --- a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java +++ b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java @@ -16,12 +16,12 @@ public class ArrayStoreExceptionToTypeNotPresentException extends Recipe { @Override public String getDisplayName() { - return "Handle TypeNotPresentException instead of ArrayStoreException"; + return "ArrayStoreExceptionToTypeNotPresentException"; } @Override public String getDescription() { - return "This recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException or both exceptions, to ensure compatibility with Java 11+."; + return "This recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException to ensure compatibility with Java 11+."; } @Override From ff55ffacf4ce1b8235926aa72e63fc5bb45dce45 Mon Sep 17 00:00:00 2001 From: bhavanapidapa Date: Fri, 22 Nov 2024 15:32:48 -0600 Subject: [PATCH 05/11] Added the copyright to the files --- ...ayStoreExceptionToTypeNotPresentException.java | 15 +++++++++++++++ .../java/migrate/TypeNotPresentExceptionTest.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java index 69e6b413e9..292a53728f 100644 --- a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java +++ b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; diff --git a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java index 1ac9d39d9c..5dd0034faa 100644 --- a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java +++ b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; From ce5a57274863e9bd0c61590314610ce444e8f8a6 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 24 Nov 2024 22:02:50 +0100 Subject: [PATCH 06/11] Apply suggestions from code review --- .../migrate/ArrayStoreExceptionToTypeNotPresentException.java | 3 ++- .../openrewrite/java/migrate/TypeNotPresentExceptionTest.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java index 292a53728f..7e25534375 100644 --- a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java +++ b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java @@ -58,8 +58,9 @@ public J visitTry(J.Try tryStmt, ExecutionContext ctx) { updatedCatches.add(catchClause); } } - if (flag) + if (flag) { tryStmt = tryStmt.withCatches(updatedCatches); + } } return super.visitTry(tryStmt, ctx); } diff --git a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java index 5dd0034faa..4f097ef17b 100644 --- a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java +++ b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java @@ -21,7 +21,7 @@ import static org.openrewrite.java.Assertions.java; -public class TypeNotPresentExceptionTest implements RewriteTest { +class TypeNotPresentExceptionTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.recipe(new ArrayStoreExceptionToTypeNotPresentException()); From 7a27436fd51be27ab6f10ee24a93f333689c2fb9 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 24 Nov 2024 22:56:25 +0100 Subject: [PATCH 07/11] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../openrewrite/java/migrate/TypeNotPresentExceptionTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java index 4f097ef17b..72731667ba 100644 --- a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java +++ b/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java @@ -16,6 +16,7 @@ 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; @@ -27,6 +28,7 @@ public void defaults(RecipeSpec spec) { spec.recipe(new ArrayStoreExceptionToTypeNotPresentException()); } + @DocumentExample @Test void ArrayStoreExceptionTest() { rewriteRun( From 9cfb45c1979cf969ccc98e282da84b01afd4fffb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 24 Nov 2024 22:56:40 +0100 Subject: [PATCH 08/11] Remove unnecessary trailing newline --- .../migrate/ArrayStoreExceptionToTypeNotPresentException.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java index 7e25534375..016d610de1 100644 --- a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java +++ b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java @@ -74,4 +74,3 @@ private boolean containsGetAnnotation(J.Try tryStmt) { }; } } - From e3574fc2e386cb29ce0cda9320ef132231d307cb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 24 Nov 2024 22:57:01 +0100 Subject: [PATCH 09/11] Apply suggestions from code review --- .../migrate/ArrayStoreExceptionToTypeNotPresentException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java index 016d610de1..8c6eacc284 100644 --- a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java +++ b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java @@ -36,7 +36,7 @@ public String getDisplayName() { @Override public String getDescription() { - return "This recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException to ensure compatibility with Java 11+."; + return "Replace catch blocks for `ArrayStoreException` around `getAnnotation()` with `TypeNotPresentException` to ensure compatibility with Java 11+."; } @Override From 657ebd5cf6f0562990ed5ce0c39b5eb3b10a124e Mon Sep 17 00:00:00 2001 From: bhavanapidapa Date: Mon, 25 Nov 2024 13:37:52 -0600 Subject: [PATCH 10/11] Fixed the code as mentioned in comments --- .../ArrayStoreExceptionToTypeNotPresentException.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java index 8c6eacc284..3f2cf4ad5a 100644 --- a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java +++ b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java @@ -31,7 +31,7 @@ public class ArrayStoreExceptionToTypeNotPresentException extends Recipe { @Override public String getDisplayName() { - return "ArrayStoreExceptionToTypeNotPresentException"; + return "ArrayStoreException to TypeNotPresentException around getAnnotation() method "; } @Override @@ -46,8 +46,10 @@ public TreeVisitor getVisitor() { @Override public J visitTry(J.Try tryStmt, ExecutionContext ctx) { + if (!containsGetAnnotation(tryStmt)) { + return super.visitTry(tryStmt, ctx); + } boolean flag = false; - if (containsGetAnnotation(tryStmt)) { List updatedCatches = new ArrayList<>(); for (J.Try.Catch catchClause : tryStmt.getCatches()) { if (catchClause.getParameter().getType() != null && catchClause.getParameter().getType().isAssignableFrom(Pattern.compile("java.lang.ArrayStoreException"))) { @@ -61,7 +63,6 @@ public J visitTry(J.Try tryStmt, ExecutionContext ctx) { if (flag) { tryStmt = tryStmt.withCatches(updatedCatches); } - } return super.visitTry(tryStmt, ctx); } From 18595caf8709e1a58b6a2886ce2988ed81b2d9fb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 25 Nov 2024 22:10:03 +0100 Subject: [PATCH 11/11] Use a Precondition, ListUtils, and prevent unnecessary changes --- ...oreExceptionToTypeNotPresentException.java | 62 ++++++++----------- ...ceptionToTypeNotPresentExceptionTest.java} | 27 +++++++- 2 files changed, 49 insertions(+), 40 deletions(-) rename src/test/java/org/openrewrite/java/migrate/{TypeNotPresentExceptionTest.java => ArrayStoreExceptionToTypeNotPresentExceptionTest.java} (82%) diff --git a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java index 3f2cf4ad5a..a9048b3479 100644 --- a/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java +++ b/src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java @@ -16,62 +16,50 @@ package org.openrewrite.java.migrate; import org.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.ListUtils; import org.openrewrite.java.ChangeType; -import org.openrewrite.java.JavaVisitor; -import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.search.FindMethods; +import org.openrewrite.java.search.UsesMethod; import org.openrewrite.java.tree.J; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; +import org.openrewrite.java.tree.TypeUtils; public class ArrayStoreExceptionToTypeNotPresentException extends Recipe { + + private static final String ARRAY_STORE_EXCEPTION = "java.lang.ArrayStoreException"; + private static final String TYPE_NOT_PRESENT_EXCEPTION = "java.lang.TypeNotPresentException"; + @Override public String getDisplayName() { - return "ArrayStoreException to TypeNotPresentException around getAnnotation() method "; + return "Catch `TypeNotPresentException` thrown by `Class.getAnnotation()`"; } @Override public String getDescription() { - return "Replace catch blocks for `ArrayStoreException` around `getAnnotation()` with `TypeNotPresentException` to ensure compatibility with Java 11+."; + return "Replace catch blocks for `ArrayStoreException` around `Class.getAnnotation()` with `TypeNotPresentException` to ensure compatibility with Java 11+."; } @Override public TreeVisitor getVisitor() { - final MethodMatcher classGetAnnotationMethod = new MethodMatcher("java.lang.Class getAnnotation(java.lang.Class)"); - return new JavaVisitor() { - + String classGetAnnotationPattern = "java.lang.Class getAnnotation(java.lang.Class)"; + return Preconditions.check(new UsesMethod<>(classGetAnnotationPattern), new JavaIsoVisitor() { @Override - public J visitTry(J.Try tryStmt, ExecutionContext ctx) { - if (!containsGetAnnotation(tryStmt)) { - return super.visitTry(tryStmt, ctx); + public J.Try visitTry(J.Try tryStatement, ExecutionContext ctx) { + J.Try try_ = super.visitTry(tryStatement, ctx); + if (FindMethods.find(try_, classGetAnnotationPattern).isEmpty()) { + return try_; } - boolean flag = false; - List updatedCatches = new ArrayList<>(); - for (J.Try.Catch catchClause : tryStmt.getCatches()) { - if (catchClause.getParameter().getType() != null && catchClause.getParameter().getType().isAssignableFrom(Pattern.compile("java.lang.ArrayStoreException"))) { - J.Try.Catch updatedCatch = (J.Try.Catch) new ChangeType("java.lang.ArrayStoreException", "java.lang.TypeNotPresentException", true).getVisitor().visit(catchClause, ctx); - updatedCatches.add(updatedCatch); - flag = true; - } else { - updatedCatches.add(catchClause); - } + return try_.withCatches(ListUtils.map(try_.getCatches(), catch_ -> { + if (TypeUtils.isOfClassType(catch_.getParameter().getType(), ARRAY_STORE_EXCEPTION)) { + return (J.Try.Catch) new ChangeType(ARRAY_STORE_EXCEPTION, TYPE_NOT_PRESENT_EXCEPTION, true) + .getVisitor().visit(catch_, ctx); } - if (flag) { - tryStmt = tryStmt.withCatches(updatedCatches); - } - return super.visitTry(tryStmt, ctx); - } - - private boolean containsGetAnnotation(J.Try tryStmt) { - return tryStmt.getBody().getStatements().stream() - .filter(Expression.class::isInstance) - .map(Expression.class::cast) - .anyMatch(classGetAnnotationMethod::matches); + return catch_; + })); } - }; + }); } } diff --git a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java b/src/test/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentExceptionTest.java similarity index 82% rename from src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java rename to src/test/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentExceptionTest.java index 72731667ba..4843e89816 100644 --- a/src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentExceptionTest.java @@ -22,7 +22,8 @@ import static org.openrewrite.java.Assertions.java; -class TypeNotPresentExceptionTest implements RewriteTest { +class ArrayStoreExceptionToTypeNotPresentExceptionTest implements RewriteTest { + @Override public void defaults(RecipeSpec spec) { spec.recipe(new ArrayStoreExceptionToTypeNotPresentException()); @@ -30,7 +31,7 @@ public void defaults(RecipeSpec spec) { @DocumentExample @Test - void ArrayStoreExceptionTest() { + void replaceCaughtException() { rewriteRun( //language=java java( @@ -79,7 +80,7 @@ public void testMethod() { } @Test - void rOtherExceptionsTest() { + void retainOtherCaughtExceptions() { rewriteRun( //language=java java( @@ -98,4 +99,24 @@ public void testMethod() { ) ); } + + @Test + void retainArrayStoreExceptionWithoutClassGetAnnotation() { + rewriteRun( + //language=java + java( + """ + public class Test { + public void testMethod() { + try { + Object o = "test"; + } catch (ArrayStoreException e) { + System.out.println("Caught Exception"); + } + } + } + """ + ) + ); + } }