Skip to content

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

@oowekyala

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

Metadata

Metadata

Assignees

Labels

a:bugPMD crashes or fails to analyse a file.in:astAbout the AST structure or API, the parsing step

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions