GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Move annotations inside the node they apply to by oowekyala · Pull Request #1875 · pmd/pmd

Conversation

@oowekyala

@oowekyala oowekyala commented Jun 19, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Annotations are now part of the node they apply to, which fixes a lot of inconsistencies, where sometimes the annotations are inside the node, and sometimes just somewhere in the parent, with no real structure.

Implementation is another jjtree tweak, which allows avoiding looking ahead past the annotations. The annotations are just marked as "pending", and then adopted by the next node to be opened (be it a class or method declaration, whatever).

This makes some nodes completely redundant, which can later be turned into interfaces or removed: TypeDeclaration, ClassOrInterfaceBodyDeclaration, AnnotationTypeBodyDeclaration, and BlockStatement (which for now contains the annotations of a local class for example)

Some examples:

CodeOld ASTCurrent 7.0 ASTNew AST after this PR

Top-level type declaration

@A class C {}
 +TypeDeclaration
   +MarkerAnnotation "A"
   +ClassOrInterfaceDeclaration
      +ClassOrInterfaceBody
Same
 +TypeDeclaration
-  +MarkerAnnotation "A"
   +ClassOrInterfaceDeclaration
+     +MarkerAnnotation "A"
      +ClassOrInterfaceBody

Cast expression

(@A T.@B S) expr

N/A (Parse error)

+CastExpression
 +MarkerAnnotation "A"
 +ClassOrInterfaceType "S"
   +MarkerAnnotation "B"
   +AmbiguousName "T"
 +(Expression `expr`)
 +CastExpression
- +MarkerAnnotation "A"
  +ClassOrInterfaceType "S"
    +MarkerAnnotation "B"
-   +AmbiguousName "T"
+   +ClassOrInterfaceType "T"
+    +MarkerAnnotation "A"
  +(Expression `expr`)

Notice how T is not ambiguous anymore.

Cast expression with intersection

(@A T & S) expr
+CastExpression
 +MarkerAnnotation "A"
 +ClassOrInterfaceType "T"
 +ClassOrInterfaceType "S"
 +(Expression `expr`)
 +CastExpression
  +MarkerAnnotation "A"
+ +IntersectionType
   +ClassOrInterfaceType "T"
   +ClassOrInterfaceType "S"
  +(Expression `expr`)
 +CastExpression
- +MarkerAnnotation "A"
  +IntersectionType
   +ClassOrInterfaceType "T"
+   +MarkerAnnotation "A"
   +ClassOrInterfaceType "S"
  +(Expression `expr`)

Notice @A binds to T, not T & S

Constructor call

new @A T()
+AllocationExpression
 +MarkerAnnotation "A"
 +Type
  +ReferenceType
   +ClassOrInterfaceType "T"
 +Arguments
+ConstructorCall
 +MarkerAnnotation "A"
 +ClassOrInterfaceType "T"
 +ArgumentsList
+ConstructorCall
 +ClassOrInterfaceType "T"
  +MarkerAnnotation "A"
 +ArgumentsList

Array allocation

new @A int[0]
+AllocationExpression
 +MarkerAnnotation "A"
 +Type
  +PrimitiveType "int"
 +ArrayDimsAndInits
  +Expression
   +PrimaryExpression
    +Literal "0"
+ArrayAllocation
 +MarkerAnnotation "A"
 +PrimitiveType "int"
 +ArrayAllocationDims
  +ArrayDimExpr
   +NumericLiteral "0"
 +ArrayAllocation
- +MarkerAnnotation "A"
  +PrimitiveType "int"
+  +MarkerAnnotation "A"
  +ArrayAllocationDims
   +ArrayDimExpr
    +NumericLiteral "0"

Array type

@A int @B[]

N/A (parse error)

+MarkerAnnotation "A"
+ArrayType
 +PrimitiveType "int"
 +ArrayTypeDims
  +ArrayTypeDim
   +MarkerAnnotation "B"
-+MarkerAnnotation "A"
 +ArrayType
  +PrimitiveType "int"
+  +MarkerAnnotation "A"
  +ArrayTypeDims
   +ArrayTypeDim
    +MarkerAnnotation "B"

Notice @A binds to int, not int[]

Type parameters

<@A T, @B S extends @C Object>
+TypeParameters
 +MarkerAnnotation "A"
 +TypeParameter "T"
 +MarkerAnnotation "B"
 +TypeParameter "S"
  +MarkerAnnotation "C"
  +TypeBound
   +ReferenceType
    +ClassOrInterfaceType "Object"
 +TypeParameters
  +MarkerAnnotation "A"
  +TypeParameter "T"
  +MarkerAnnotation "B"
  +TypeParameter "S"
   +MarkerAnnotation "C"
   +TypeBound
-   +ReferenceType
     +ClassOrInterfaceType "Object"
 +TypeParameters
- +MarkerAnnotation "A"
  +TypeParameter "T"
+  +MarkerAnnotation "A"
- +MarkerAnnotation "B"
  +TypeParameter "S"
+  +MarkerAnnotation "B"
-  +MarkerAnnotation "C"
-   +TypeBound
     +ClassOrInterfaceType "Object"
+     +MarkerAnnotation "C"
  
  • TypeParameters now only can have TypeParameter as a child
  • Annotations that apply to the param are in the param
  • Annotations that apply to the bound are in the type
  • This removes the need for TypeBound, because annotations are cleanly placed.

Enum constants

enum {
 @A E1, @B E2   
}
+EnumBody
 +MarkerAnnotation "A"
 +EnumConstant "E1"
 +MarkerAnnotation "B"
 +EnumConstant "E2"
 
 +EnumBody
  +MarkerAnnotation "A"
  +EnumConstant "E1"
+  +VariableDeclaratorId "E1"
  +MarkerAnnotation "B"
  +EnumConstant "E2"
+  +VariableDeclaratorId "E1"
 
 +EnumBody
- +MarkerAnnotation "A"
  +EnumConstant "E1"
+  +MarkerAnnotation "A"
   +VariableDeclaratorId "E1"
- +MarkerAnnotation "B"
  +EnumConstant "E2"
+  +MarkerAnnotation "B"
   +VariableDeclaratorId "E1"
 
  • Annotations are not just randomly in the enum body anymore

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
oowekyala added 16 commits June 19, 2019 00:37
@oowekyala
Extract annotation changes
a709ce5
@oowekyala
Fix tests
a9bcb00
@oowekyala
Update grammar hints
5b91d2f
@oowekyala
Remove test to enable later
d84eb67
@oowekyala
Fix comment
69f7227
@oowekyala
Fix wildcard type annotations
93ceb24
@oowekyala
Add test for enum constants
eec53bc
@oowekyala
Test other annotation positions
0351b59
@oowekyala
remove wrapLeft
8262e38
@oowekyala
Remove annotation list, parse annotations flatly
388c952
@oowekyala
Generalise annotatable
154b376
@oowekyala
Array type annotations
4030f0f
@oowekyala
Add local class annotation tests
176eb23
@oowekyala
Fix tests
cbc336a
@oowekyala
Add changelog notice
2a40d49
@oowekyala
Fix instanceof with annotated type
926c008
@oowekyala oowekyala added the in:ast About the AST structure or API, the parsing step label Jun 19, 2019
@oowekyala oowekyala added this to the 7.0.0 milestone Jun 19, 2019
oowekyala added 4 commits June 20, 2019 16:27
@oowekyala
Make type param implement annotatable
4248c44
@oowekyala
Merge branch 'java-grammar' into grammar-annotation-floating
9356d12
@oowekyala
Fix text bounds on declarations
b47aa62
@oowekyala
Fix some cases of type annotations failure
a64c467
@oowekyala

oowekyala commented Jun 26, 2019

Copy link
Copy Markdown
Member Author

I added a lengthy test case for type annotations that includes all contexts where type annotations may appear for exhaustiveness. It's based off the spec of type annotations.

There are a few cases where type annotations are legal that this PR fixes

  • On method references: @Nonnull String::length
  • On instanceof expressions: o instanceof @Readonly String
  • On all bounds of an intersection type: @A U1 & @B U2. I originally misread the spec and thought only the first type could be annotated.

There are two cases which we currently don't handle:

  • On the types of a throws clause: void foo() throws @A Exception, java.io.@B IOException
    • This is easy to fix and there's already a TODO on the wiki, about replacing NameList with ThrowsList, which would be a list of ClassOrInterfaceType
  • On the varargs ellipsis ...: void varargs(String @A ... last);
    • In that case, the ... is treated just like array dimensions and annotations apply exactly as if the param was declared as String @A [] last
    • The sensible way to parse that would probably be to use ArrayTypeDims in some way... I'm not sure yet.
    • Eg maybe we should parse String... just as if it was String[], so use an ArrayType.
      • This causes problems because ArrayType is not fully recursive, so that the component type of an ArrayType is not always represented by a node.
        • Eg in String[], the component type is represented by the ClassOrInterfaceType
        • But in String[][], there is no node representing the component type String[] , since [][] are flat.
        • So maybe we should represent array types like the following:
CodeOld ASTCurrent 7.0 ASTProposed representation
String[][]
 +Type
  +ReferenceType[@ArrayDepth=2]
   +ClassOrInterfaceType "String"
 +ArrayType
    +ClassOrInterfaceType "String"
    +ArrayTypeDims
       +ArrayTypeDim
       +ArrayTypeDim
 +ArrayType
    +ArrayType
      +ClassOrInterfaceType "String"
 String @A [] @B []

N/A (Parse error)

 +ArrayType
    +ClassOrInterfaceType
    +ArrayTypeDims
       +ArrayTypeDim
          +Annotation "A"
       +ArrayTypeDim
          +Annotation "B"
 +ArrayType
    +ArrayType
      +ClassOrInterfaceType
      +Annotation "A"
    +Annotation "B"

With varargs:

 String @A [] @B ...

N/A (Parse error)

N/A (Parse error)

 +ArrayType[@Varargs = true()]
    +ArrayType
      +ClassOrInterfaceType
      +Annotation "A"
    +Annotation "B"

Note that this representation is both more economic in number of nodes (especially in the most common case where there are no annotations on the array dimensions), and also preserves every intermediary array type, which is nice.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

oowekyala added 2 commits July 12, 2019 02:53
@oowekyala
Fix type annot on method ref in cast ctx
d664b17
@oowekyala
Fix choice amibiguity with annotated local class
0d6e2f3
Fixes some failing tests
@adangel adangel self-assigned this Jul 15, 2019
adangel
adangel reviewed Jul 15, 2019
View reviewed changes
Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JSingleChildNode.java
*
* @author Clément Fournier
*/
interface JSingleChildNode<T extends JavaNode> extends JavaNode {

@adangel adangel Jul 15, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is it important, that the interface starts with the prefix "J"? We don't usually do this...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@oowekyala oowekyala Jul 15, 2019

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I only did this, because this interface is equivalent to one that is introduced in #1622, and I want to avoid the name clash. When that PR is merged we can remove this one.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
adangel
adangel reviewed Jul 15, 2019
View reviewed changes
Comment thread pmd-java/src/test/resources/net/sourceforge/pmd/ast/FullTypeAnnotations.java
* https://checkerframework.org/jsr308/java-annotation-design.html#type-names
* is particularly interesting
*/
public class ParserCornerCases18 {

@adangel adangel Jul 15, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We should probably rename this class to avoid confusion 😄

Do we have annotated method declarations covered? Like

@Override
public void run() { }

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@adangel
adangel merged commit 0d6e2f3 into pmd:java-grammar Jul 15, 2019
@adangel

adangel commented Jul 15, 2019

Copy link
Copy Markdown
Member

@oowekyala
Sorry for the delay. I've merged it now into java-grammar. I've updated https://github.com/pmd/pmd/wiki/PMD-7.0.0-Java and https://github.com/pmd/pmd/wiki/Java_clean_changes#annotation-nesting

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala
oowekyala deleted the grammar-annotation-floating branch July 15, 2019 22:38
@oowekyala oowekyala mentioned this pull request Feb 19, 2020
[java] Flatten body declarations #2300
Merged
@adangel adangel mentioned this pull request Jan 23, 2023
PMD 7 Tracking Issue #3898
Closed
55 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@adangel adangel

Labels

in:ast About the AST structure or API, the parsing step

Projects

None yet

Milestone

7.0.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@oowekyala @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.