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

Replace ArrayStoreException with TypeNotPresentException in try/catch using Class.getAnnotation() #613

Merged

Conversation

BhavanaPidapa
Copy link
Contributor

What's changed?

This PR contains recipe - org.openrewrite.java.migrate.ArrayStoreExceptionToTypeNotPresentException
Rule:
image

What's your motivation?

This custom recipe replaces catch blocks for ArrayStoreException around getAnnotation() with TypeNotPresentException to ensure compatibility with Java 11+.

Anyone you would like to review specifically?

@timtebeek @cjobinabo

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@BhavanaPidapa BhavanaPidapa changed the title Recipe class get annotation method Recipe Replace ArrayStoreException with TypeNotPresentException Nov 22, 2024
@timtebeek timtebeek added the recipe Recipe requested label Nov 24, 2024
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher classGetAnnotationMethod = new MethodMatcher("java.lang.Class getAnnotation(java.lang.Class)");
return new JavaVisitor<ExecutionContext>() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a Precondition here to only call the visitor when classGetAnnotationMethod is used. That way we can reject the vast majority of classes without visiting any elements, for a nice performance boost.

timtebeek and others added 3 commits November 24, 2024 22:56
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek changed the title Recipe Replace ArrayStoreException with TypeNotPresentException Replace ArrayStoreException with TypeNotPresentException in try/catch using getAnnotation() Nov 24, 2024
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for kicking this off @BhavanaPidapa ; I've applied a few small changes in 18595ca to simplify the implementation and use established patterns. Approved from my side & ready to merge.

@timtebeek timtebeek changed the title Replace ArrayStoreException with TypeNotPresentException in try/catch using getAnnotation() Replace ArrayStoreException with TypeNotPresentException in try/catch using Class.getAnnotation() Nov 25, 2024
@timtebeek timtebeek merged commit c48a656 into openrewrite:main Nov 25, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe requested
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants