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

Add annotation element values to type attribution model #4746

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

zexblue01
Copy link

@zexblue01 zexblue01 commented Dec 4, 2024

What's changed?

This PR adds ability to preserve annotation values parsed by JavaParsers (I updated all existing Java parser versions: 8, 11, 17, 21).

It adds a new subclass Annotation of FullyQualified in JavaType. It delegates most functionality to annotation's JavaType.FullyQualified object but stores list of value attribute pairs as part of the structure in ElementValue objects (with the two implementations SingleElementValue and ArrayElementValue).

What's your motivation?

In the recipe I'm working on I need to be able to access annotation values on the method invoked in the source file (method could be defined in a dependency in the classpath instead of being as part of the source file input).

Anything in particular you'd like reviewers to focus on?

Not sure if it is there already a model somewhere else for annotation I could use instead of defining a new subclass of FullyQualified.
Also the interface didn't change after this change. It is still returning FullyQualified objects when accessing annotations. I'm concerned that changing this will complicate this change but let me know if you feel differently.

Anyone you would like to review specifically?

@jkschneider
@timtebeek

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.

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

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.

Great to see already; some very minor feedback as a first pass through; I like how you've taken care to minimize your changes and immediately roll them out across the various parsers.

@timtebeek timtebeek self-requested a review December 4, 2024 22:16
Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

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

Looking very good already! I have quite a few use cases in mind for this, actually. I think we have some issues I can link to this PR (I will try to dig them out).

I added some review comments and also took the liberty to push a commit to the PR to store the "actual" annotation values, rather than a stringified representation of them, as I suspect that could make them a lot more useful. This work definitely isn't complete yet. Also, feel free to revert if you disagree.

@knutwannheden knutwannheden changed the title preserve annotation values when parsing compiled classes Add annotation element values to type attribution model Dec 10, 2024
@knutwannheden knutwannheden force-pushed the eason-preserve_annotation_values branch from 89436f3 to 865540c Compare December 10, 2024 14:17

@Override
public FullyQualified withFullyQualifiedName(String fullyQualifiedName) {
return this;
Copy link
Contributor

@Laurens-W Laurens-W Dec 10, 2024

Choose a reason for hiding this comment

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

Should this not delegate to type?
Perhaps this should delegate to a this.withType and call type.withFullyQualifiedName?

knutwannheden added a commit to openrewrite/rewrite-csharp that referenced this pull request Dec 13, 2024
timtebeek referenced this pull request in langchain4j/langchain4j-openrewrite-recipes Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request parser-java
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

Preserve annotation values parsed from dependencies in the classpath
4 participants