Skip to content

Java Grammar: improper method def line and column position for methods with ARRAY_DECLARATOR #3145

@rnveach

Description

@rnveach

Taken from PR #2940

Example 1 with brackets:

double[] computeDerivatives()
    |--METHOD_DEF -> METHOD_DEF [85:10] // expected column 4, not 10
    |   |--MODIFIERS -> MODIFIERS [85:10]
    |   |--TYPE -> TYPE [85:10]
    |   |   `--ARRAY_DECLARATOR -> [ [85:10]
    |   |       |--LITERAL_DOUBLE -> double [85:4]
    |   |       `--RBRACK -> ] [85:11]

Example 2 with no brackets:

double computeDerivatives()
    |--METHOD_DEF -> METHOD_DEF [90:4] // correct line and column
    |   |--MODIFIERS -> MODIFIERS [90:4]
    |   |--TYPE -> TYPE [90:4]
    |   |   `--LITERAL_DOUBLE -> double [90:4]

Example 3 with brackets on seperate lines:

double
[]
computeDerivatives()
    |--METHOD_DEF -> METHOD_DEF [88:4] // expected line 87, not 88
    |   |--MODIFIERS -> MODIFIERS [88:4]
    |   |--TYPE -> TYPE [88:4]
    |   |   `--ARRAY_DECLARATOR -> [ [88:4]
    |   |       |--LITERAL_DOUBLE -> double [87:4]
    |   |       `--RBRACK -> ] [88:5]

I expected METHOD_DEF to signify the exact line and column position of the start of the method definition. In this case, example 1's column position 10 is wrong and it should be 4, which we see for when there is no brackets in example 2. Even if we go by the line number and find the start of the line, the 3rd example shows it can be wrong too.

Full Example:

$ cat TestClass.java
public class TestClass {
    double[] computeDerivatives() { return null; }
}

$ java -jar checkstyle-6.17-all.jar TestClass.java -T
CLASS_DEF -> CLASS_DEF [1:0]
|--MODIFIERS -> MODIFIERS [1:0]
|  `--LITERAL_PUBLIC -> public [1:0]
|--LITERAL_CLASS -> class [1:7]
|--IDENT -> TestClass [1:13]
`--OBJBLOCK -> OBJBLOCK [1:23]
    |--LCURLY -> { [1:23]
    |--METHOD_DEF -> METHOD_DEF [2:10] // expected 2:4 
    |  |--MODIFIERS -> MODIFIERS [2:10]
    |  |--TYPE -> TYPE [2:10]
    |  |  `--ARRAY_DECLARATOR -> [ [2:10]
    |  |      |--LITERAL_DOUBLE -> double [2:4] // as seen here
    |  |      `--RBRACK -> ] [2:11]
    |  |--IDENT -> computeDerivatives [2:13]
    |  |--LPAREN -> ( [2:31]
    |  |--PARAMETERS -> PARAMETERS [2:32]
    |  |--RPAREN -> ) [2:32]
    |  `--SLIST -> { [2:34]
    |      |--LITERAL_RETURN -> return [2:36]
    |      |  |--EXPR -> EXPR [2:43]
    |      |  |  `--LITERAL_NULL -> null [2:43]
    |      |  `--SEMI -> ; [2:47]
    |      `--RCURLY -> } [2:49]
    `--RCURLY -> } [3:0]

Migration notes: please read at #3145 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions