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

Preserve annotation values parsed from dependencies in the classpath #4745

Open
zexblue01 opened this issue Dec 4, 2024 · 1 comment · May be fixed by #4746
Open

Preserve annotation values parsed from dependencies in the classpath #4745

zexblue01 opened this issue Dec 4, 2024 · 1 comment · May be fixed by #4746
Labels
enhancement New feature or request

Comments

@zexblue01
Copy link

zexblue01 commented Dec 4, 2024

What problem are you trying to solve?

I’m working on a linter for detecting certain methods in our code base. The linting is based on method annotations. The annotation in question has a value attached which I need for making the linting decision. The method in question is only in the classpath, not part of the source files.

Dependency lib in the classpath:

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface EssentialTracker {
  String value();
}

public class TestEssentialTracker {
 @EssentialTracker("aaa")
 public String getEssentialTracker() {
  return "test-essential-tracker";
 }
}

Source file input I'm linting:

String result = new TestEssentialTracker().getEssentialTracker();

In this case, I want to be able to access the value "aaa" in the recipe.

I did some experiments. It seems in the recipe I can access the annotation type but not the “value” because it was converted into JavaType.FullyQualified here without knowledge of the value: https://github.com/openrewrite/rewrite/blob/main/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17TypeMapping.java#L696

Describe the solution you'd like

Preserve the annotation value pairs in the parsed LST.

Have you considered any alternatives or workarounds?

I don't think there is a workaround with OpenRewrite. I worked around in my solution by loading all classes and use reflection to inspect the actual annotation values, which is a bit cumbersome.

Additional context

n/a

Are you interested in contributing this feature to OpenRewrite?

Yes, here is the patch I worked on. I don't have good understanding of OpenRewrite implementation holistically though, would love your feedback.

@knutwannheden
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

Successfully merging a pull request may close this issue.

2 participants