Skip to content

[java] Initial stub for annotation type resolution#4240

Closed
jsotuyod wants to merge 1 commit into
pmd:pmd/7.0.xfrom
jsotuyod:annotation-resolution
Closed

[java] Initial stub for annotation type resolution#4240
jsotuyod wants to merge 1 commit into
pmd:pmd/7.0.xfrom
jsotuyod:annotation-resolution

Conversation

@jsotuyod

Copy link
Copy Markdown
Member

Describe the PR

  • During type resolution annotations weren't included. The AST allows to look into them for the source proper, but there was no way for types obtained from the classpath.

Related issues

  • Fixes #

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

 - During type resolution annotations weren't included. The AST allows to look
into them for the source proper, but there was no way for types obtained from
the classpath.
@jsotuyod jsotuyod force-pushed the annotation-resolution branch from bb378a8 to 236291f Compare November 26, 2022 22:31
@jsotuyod

jsotuyod commented Nov 26, 2022

Copy link
Copy Markdown
Member Author

@oowekyala I would love your input before I drive deeper into this, as I have my share of doubts.

I wanted to try and use PMD 7 to implement some rules around @Nullable / @NonNull, but in doing so I found out the new type resolution system completely omits annotations, so it's impossible to know how a given class, field or param is annotated without the AST.

First off, annotations are somewhat odd in the current system… We have 2 main categories of objects:

  • Symbols: which represent the declaration of a symbol within a given scope.
  • Types: which are actual types of things.

So, a method declaration is a symbol, and it's return type and formal parameters are types.

Annotations are definitely not symbols in this context. They are not defined (when that's the case, it's properly handled and a JClassSymbol is used to represent them), but merely applied to different symbols, similarly to modifiers. But these "modifiers" do have a type. Moreover symbols are the one that actually holds the references to everything (the ones parsed from ASM / AST), and even expose the methods one would expect from the equivalent java.reflect objects (ie: getDeclaredFields() or getDeclaredMethods()), so it seems natural that they are to also expose an equivalent getDeclaredAnnotations().

Is it ok to have symbols hold references to the actual annotations this way?
Is there a better way to approach surfacing existing annotations under the current type resolution system?

@oowekyala oowekyala mentioned this pull request Nov 27, 2022
4 tasks
@oowekyala

oowekyala commented Nov 27, 2022

Copy link
Copy Markdown
Member

Hi Juan, yes I think symbols are the place annotations should be reflected. I had started work on that a while ago and stashed it. See #4241 and #3752. This has the symbol part of annotations, in particular https://github.com/pmd/pmd/pull/4241/files#diff-47b203023af58e3d1abf78590e8159d79fdae0bc4c1f8527c4643961a3208789R16 which is your JAnnotatableElementSymbol.

The other part to this is adding a List<SymAnnot> getAnnotations() to JTypeMirror. But this is another dimension of tricky I think... Annotations need to flow correctly through type inference, but they can't interfere with type equality. Also primitive types can be annotated, so we will have more instances of JPrimitiveType than what the codebase expects for now.
Edit: see this branch for a start of the implementation

@jsotuyod

Copy link
Copy Markdown
Member Author

@oowekyala thanks, I'll look into that branch and see where it goes.

@jsotuyod jsotuyod closed this Nov 29, 2022
@oowekyala oowekyala mentioned this pull request Nov 29, 2022
4 tasks
@jsotuyod jsotuyod deleted the annotation-resolution branch January 16, 2023 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants