Skip to content

[java] Java8 parsing corner case with annotated array types  #997

Closed

Description

@oowekyala

Problems

With Java 8 type annotations, the dimensions of an array may each have different annotations, which our parser actually doesn't support... There are several instances of this problem:

Type node

      // Doesn't parse
      int @Foo[]@Bar[] i;

JLS

Our grammar:

https://github.com/pmd/pmd/blob/master/pmd-java/etc/grammar/Java.jjt#L1885-L1887

Array creation expression

   // Doesn't parse
    new int @Foo[2] @Bar[];

JLS

Our grammar:

https://github.com/pmd/pmd/blob/master/pmd-java/etc/grammar/Java.jjt#L2261-L2269

VariableDeclaratorId

class Foo {
  int i @Bar[]; // field declaration

  void bar(int i@Bar[]) { // formal parameter 
    int i @Bar[]; // local variable declaration
  }
}

JLS

Our grammar:

https://github.com/pmd/pmd/blob/master/pmd-java/etc/grammar/Java.jjt#L1771-1787

Method declarations

The JLS allows array dimensions (including annotations) to be added after the formal parameter list, eg:

  int method()@Foo[]@Bar[] {
        return new int[2][];
  }

By the way, even without annotations, the parser currently discards the additionnal array dimensions, though it should be used in type resolution:

https://github.com/pmd/pmd/blob/master/pmd-java/etc/grammar/Java.jjt#L1822

This is related to the broader #910 changeset

Solution

  • Add a node for the array dimensions, which resembles the JLS for the Dims production. This wouldn't be API breaking, provided we make the dimensions of eg ReferenceType delegate to that Dims node.
  • Dimensionable should probably be deprecated entirely, and we could use a new interface to replace it that exposes the annotations and the dimensions for type resolution, based on the Dims node
  • To make the method return type resolution work correctly, we could make ReturnType a Dimensionable (and a TypeNode), and add the dimensions of the Dims node found in the method declarator to the dimensions found in the type node.

I imagine that could be 3 PRs

Activity

  1. added
    a:bug
    on Mar 23, 2018
  2. self-assigned this
    on Mar 23, 2018
  3. added this to the 6.3.0 milestone on Mar 23, 2018
  4. modified the milestones: 6.3.0, 6.4.0 on Apr 28, 2018
  5. modified the milestones: 6.4.0, 6.5.0 on May 19, 2018
  6. added
    in:ast
    on May 27, 2018
  7. modified the milestones: 6.5.0, 6.6.0 on Jun 26, 2018
  8. modified the milestones: 6.6.0, 6.7.0 on Jul 29, 2018
  9. modified the milestones: 6.7.0, 6.8.0 on Sep 1, 2018
  10. removed this from the 6.8.0 milestone on Sep 29, 2018
  11. added this to the 7.0.0 milestone on Nov 6, 2018
  12. oowekyala commented on Apr 25, 2020

    @oowekyala
    MemberAuthor

    Fixed on the java-grammar branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a:bugin:ast

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions