Skip to content

Commit

Permalink
Issue checkstyle#3238: Java 8 Grammar: annotations on arrays and varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
esilkensen committed Dec 27, 2019
1 parent 7af5c57 commit 0122845
Show file tree
Hide file tree
Showing 6 changed files with 570 additions and 398 deletions.
17 changes: 13 additions & 4 deletions src/main/resources/com/puppycrawl/tools/checkstyle/grammar/java.g
Expand Up @@ -256,6 +256,14 @@ typeSpec[boolean addImagNode]
| builtInTypeSpec[addImagNode]
;
// A type specification for a variable length parameter is a type name with
// possible brackets afterwards that can end with an annotation.
variableLengthParameterTypeSpec
: (classOrInterfaceType[false] | builtInType)
(annotation)?
(lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK (annotation)?)*
;
// A class type specification is a class type with either:
// - possible brackets afterwards
// (which would make it an array type).
Expand Down Expand Up @@ -386,8 +394,8 @@ builtInTypeSpec[boolean addImagNode]
// A type name. which is either a (possibly qualified and parameterized)
// class name or a primitive (builtin) type
type
: classOrInterfaceType[false]
| builtInType
: ({LA(1) == AT}? annotations | )
(classOrInterfaceType[false] | builtInType)
;

/** A declaration is the creation of a reference or primitive-type variable
Expand Down Expand Up @@ -894,7 +902,7 @@ variableDeclarator![AST mods, AST t]
declaratorBrackets[AST typ]
: {#declaratorBrackets=typ;}
(lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK)*
((annotation)? lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK)*
;
varInitializer
Expand Down Expand Up @@ -968,7 +976,7 @@ parameterDeclarationList
;
variableLengthParameterDeclaration!
: pm:parameterModifier t:typeSpec[false] td:ELLIPSIS IDENT
: pm:parameterModifier t:variableLengthParameterTypeSpec td:ELLIPSIS IDENT
pd:declaratorBrackets[#t]
{#variableLengthParameterDeclaration = #(#[PARAMETER_DEF,"PARAMETER_DEF"],
pm, #([TYPE,"TYPE"],pd), td, IDENT);}
Expand Down Expand Up @@ -1591,6 +1599,7 @@ newArrayDeclarator
warnWhenFollowAmbig = false;
}
:
(annotation)?
lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);}
(expression)?
RBRACK
Expand Down
Expand Up @@ -132,4 +132,13 @@ public void testAnnotationInTypeParameters()
verify(checkConfig, getPath("InputAnnotations11.java"), expected);
}

@Test
public void testAnnotationOnVarargs()
throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(MemberNameCheck.class);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputAnnotations12.java"), expected);
}

}
Expand Up @@ -25,6 +25,8 @@ public static <T> void m2(T @Nullable [] array) {}
public void m3() throws NullPointerException, @Nullable ArrayIndexOutOfBoundsException {}
public void m4(InputRegressionJava8Class1 this) {}
public void m5(@Nullable InputRegressionJava8Class1 this) {}
public void m6(@Nullable String @Nullable ... vararg) {}
public String m7() @Nullable [] @Nullable [] { return null; }

{
List<String> vlist = new ArrayList<String>();
Expand All @@ -44,6 +46,9 @@ public void instructions() {
Map.@Nullable Entry e;
String str = (@Nullable String) "";
(new Inner3()).<@Nullable String>m();
Object arr = new @Nullable String @Nullable [3];
for (String a @Nullable [] : m7()) {}
Object arr2 = new @Nullable int[3];

// method reference
IntBinaryOperator ibo = Math::max;
Expand Down

0 comments on commit 0122845

Please sign in to comment.