Skip to content

[java] Support for annotations in types#4245

Merged
oowekyala merged 62 commits into
pmd:pmd/7.0.xfrom
oowekyala:clem.type-annots-in-infer
Jan 8, 2023
Merged

[java] Support for annotations in types#4245
oowekyala merged 62 commits into
pmd:pmd/7.0.xfrom
oowekyala:clem.type-annots-in-infer

Conversation

@oowekyala

@oowekyala oowekyala commented Nov 29, 2022

Copy link
Copy Markdown
Member

Describe the PR

  • Add type annotation to type mirrors
    • I modified the JTypeMirror interface and implementation to also carry around a set of type annotations.
    • This means no changes to the core inference are needed to propagate annotated types (in the simple cases I've tested yet).
  • I changed the type of the method that returns annotations to return a PSet<SymAnnot> instead of List<SymAnnot>. We need a set because there is no defined iteration order for annotations that come from the class file, and we also have to merge annotations during inference. PSet is practical because we don't have to make copies to make the list immutable.
  • Support parsing type annotation from ASM:
    • implemented for fields and methods/ctors, for type parameters and their bounds
    • not implemented for superclass/super interface signatures - can be done easily in another PR I think
  • Same thing for AST types, the tests are shared between different implementations.

Related issues

  • Fixes [java] Expose annotations in symbol API #3752
  • Things to do later
    • support parsing annotations in supertype signatures (eg class Foo extends List<@NonNull Integer>)
    • verify that type inference merges type annotations properly when an ivar and instantiation have different type annotations

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)

Missing pieces:
- other declarations
- some type annot paths
All TypePaths are now handled
This is because iteration order is
not defined, also in some places we
have to merge annotations from a
declaration with another set of annots
and there may be duplicates.
This was an implementation bug, type param annotations
are now correctly reflected only on the symbol. They
need to be merged with the type annots of the usage when
doing inference though - todo for later
@jsotuyod

Copy link
Copy Markdown
Member

@oowekyala is this now ready for review?

@oowekyala

Copy link
Copy Markdown
Member Author

I still have a couple of local commits, will publish them tonight. I'll mark this as draft until then

@oowekyala oowekyala marked this pull request as draft December 12, 2022 15:20
@oowekyala oowekyala force-pushed the clem.type-annots-in-infer branch from 3d98a9e to f850049 Compare December 16, 2022 11:50
@oowekyala oowekyala marked this pull request as ready for review December 16, 2022 19:20
@oowekyala oowekyala self-assigned this Jan 8, 2023
@oowekyala oowekyala merged commit d60145e into pmd:pmd/7.0.x Jan 8, 2023
@adangel adangel added the in:type-resolution Affects the type resolution code label Jan 13, 2023
@adangel adangel linked an issue Jan 13, 2023 that may be closed by this pull request
@jsotuyod

jsotuyod commented Jan 16, 2023

Copy link
Copy Markdown
Member

This PR states that there is future work:

Things to do later

  • support parsing annotations in supertype signatures (eg class Foo extends List<@NonNull Integer>)
  • verify that type inference merges type annotations properly when an ivar and instantiation have different type annotations

I'd add to this list that return types for constructors are currently not including annotations.

Given the code (present in this PR):

public abstract class ClassWithTypeAnnotationsOnMethods {
     static class CtorOwner {

        CtorOwner(@A @B int i) { }

        @A CtorOwner() { }

        CtorOwner(String i) throws @A Exception {}
    }
}

The following test would fail:

JClassType symParent = impl.getDeclaration(ClassWithTypeAnnotationsOnMethods.class);
JClassType sym = symParent.getDeclaredClass("CtorOwner");
assertHasTypeAnnots(sym.getConstructors().get(1).getReturnType(), ANNOT_A); // FAILS!

Type params on constructor args and exceptions are properly retrieved (although we are missing those tests).

@adangel adangel mentioned this pull request Jan 23, 2023
55 tasks
@oowekyala oowekyala deleted the clem.type-annots-in-infer branch January 25, 2023 11:16
@oowekyala oowekyala restored the clem.type-annots-in-infer branch February 1, 2023 23:14
@oowekyala oowekyala deleted the clem.type-annots-in-infer branch February 1, 2023 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in:type-resolution Affects the type resolution code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] Expose annotations in symbol API

3 participants