-
Notifications
You must be signed in to change notification settings - Fork 109
Replace ArrayStoreException with TypeNotPresentException in try/catch using Class.getAnnotation()
#613
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
Merged
timtebeek
merged 12 commits into
openrewrite:main
from
BhavanaPidapa:Recipe_ClassGetAnnotationMethod
Nov 25, 2024
Merged
Replace ArrayStoreException with TypeNotPresentException in try/catch using Class.getAnnotation()
#613
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ffc2e15
draft recipe and test case
81f9345
Added the recipe to the list
e31a547
Merge Conflicts
8471357
Custom Recipe and test cases for changing exception type
d2a31af
Updated the display name
ff55ffa
Added the copyright to the files
ce5a572
Apply suggestions from code review
timtebeek 7a27436
Apply suggestions from code review
timtebeek 9cfb45c
Remove unnecessary trailing newline
timtebeek e3574fc
Apply suggestions from code review
timtebeek 657ebd5
Fixed the code as mentioned in comments
18595ca
Use a Precondition, ListUtils, and prevent unnecessary changes
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/main/java/org/openrewrite/java/migrate/ArrayStoreExceptionToTypeNotPresentException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * 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.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 "ArrayStoreExceptionToTypeNotPresentException"; | ||
BhavanaPidapa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "This recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException to ensure compatibility with Java 11+."; | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| final MethodMatcher classGetAnnotationMethod = new MethodMatcher("java.lang.Class getAnnotation(java.lang.Class)"); | ||
| return new JavaVisitor<ExecutionContext>() { | ||
|
||
|
|
||
| @Override | ||
| public J visitTry(J.Try tryStmt, ExecutionContext ctx) { | ||
| boolean flag = false; | ||
| if (containsGetAnnotation(tryStmt)) { | ||
| List<J.Try.Catch> 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) | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tryStmt = tryStmt.withCatches(updatedCatches); | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| 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); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/test/java/org/openrewrite/java/migrate/TypeNotPresentExceptionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * 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; | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import org.junit.jupiter.api.Test; | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import org.openrewrite.test.RecipeSpec; | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| public class TypeNotPresentExceptionTest implements RewriteTest { | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new ArrayStoreExceptionToTypeNotPresentException()); | ||
| } | ||
|
|
||
| @Test | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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"); | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| 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 Exception"); | ||
| } | ||
| try { | ||
| Object.class.getAnnotation(Override.class); | ||
| } catch (TypeNotPresentException e) { | ||
| System.out.println("Caught ArrayStoreException"); | ||
| } | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| 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 Exception"); | ||
| } | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.