diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/grammars/java.g b/src/main/resources/com/puppycrawl/tools/checkstyle/grammars/java.g index cfcaa1601..acc6d5f18 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/grammars/java.g +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/grammars/java.g @@ -260,7 +260,10 @@ typeSpec[boolean addImagNode] // - generic type arguments after classTypeSpec[boolean addImagNode] : classOrInterfaceType[addImagNode] - (options{greedy=true;}: lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK)* + (options{greedy=true; }: + ({LA(1) == AT}? annotations + | ) + lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK)* { if ( addImagNode ) { #classTypeSpec = #(#[TYPE,"TYPE"], #classTypeSpec); @@ -269,21 +272,26 @@ classTypeSpec[boolean addImagNode] ; classOrInterfaceType[boolean addImagNode] - : ({LA(1) == AT}? annotations - | ) - IDENT (options{warnWhenFollowAmbig=false;}: typeArguments[addImagNode])? - (options{greedy=true; }: // match as many as possible - DOT^ - IDENT (options{warnWhenFollowAmbig=false;}: typeArguments[addImagNode])? - )* - ; + : ({LA(1) == AT}? annotations + | ) + IDENT + (options{warnWhenFollowAmbig=false;}: typeArguments[addImagNode])? + + (options{greedy=true; }: // match as many as possible + DOT^ + ({LA(1) == AT}? annotations + | ) + IDENT + (options{warnWhenFollowAmbig=false;}: typeArguments[addImagNode])? + )* + ; // A generic type argument is a class type, a possibly bounded wildcard type or a built-in type array typeArgument[boolean addImagNode] : ( ({LA(1) == AT}? annotations | ) ( classTypeSpec[addImagNode] - | builtInTypeArraySpec[addImagNode] + | builtInTypeSpec[addImagNode] | wildcardType[addImagNode]) ) {#typeArgument = #(#[TYPE_ARGUMENT,"TYPE_ARGUMENT"], #typeArgument);} @@ -356,21 +364,15 @@ typeArgumentBounds[boolean addImagNode] (options{greedy=true;}: lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK)* ; -// A builtin type array specification is a builtin type with brackets afterwards -builtInTypeArraySpec[boolean addImagNode] - : builtInType - (options{greedy=true;}: lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK)+ - { - if ( addImagNode ) { - #builtInTypeArraySpec = #(#[TYPE,"TYPE"], #builtInTypeArraySpec); - } - } - ; // A builtin type specification is a builtin type with possible brackets // afterwards (which would make it an array type). builtInTypeSpec[boolean addImagNode] - : builtInType (lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK)* + : builtInType + (options{greedy=true; }: + ({LA(1) == AT}? annotations + | ) + lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK)* { if ( addImagNode ) { #builtInTypeSpec = #(#[TYPE,"TYPE"], #builtInTypeSpec); diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/grammars/java8/AnnotationsOnArrayTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/grammars/java8/AnnotationsOnArrayTest.java new file mode 100644 index 000000000..55fa41fd9 --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/grammars/java8/AnnotationsOnArrayTest.java @@ -0,0 +1,50 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2016 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.grammars.java8; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.lang3.ArrayUtils; +import org.junit.Test; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck; + +public class AnnotationsOnArrayTest extends BaseCheckTestSupport { + @Override + protected String getNonCompilablePath(String filename) throws IOException { + return super.getNonCompilablePath("grammars" + File.separator + + "java8" + File.separator + filename); + } + + @Test + public void testCanParse() + throws Exception { + final DefaultConfiguration checkConfig = + createCheckConfig(MemberNameCheck.class); + final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY; + verify(checkConfig, getNonCompilablePath("InputAnnotationsOnArray.java"), + expected); + + } + +} diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/grammars/java8/TypeUseAnnotationsOnQualifiedTypesTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/grammars/java8/TypeUseAnnotationsOnQualifiedTypesTest.java new file mode 100644 index 000000000..5173cf0be --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/grammars/java8/TypeUseAnnotationsOnQualifiedTypesTest.java @@ -0,0 +1,50 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2016 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.grammars.java8; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.lang3.ArrayUtils; +import org.junit.Test; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck; + +public class TypeUseAnnotationsOnQualifiedTypesTest extends BaseCheckTestSupport { + @Override + protected String getNonCompilablePath(String filename) throws IOException { + return super.getNonCompilablePath("grammars" + File.separator + + "java8" + File.separator + filename); + } + + @Test + public void testCanParse() + throws Exception { + final DefaultConfiguration checkConfig = + createCheckConfig(MemberNameCheck.class); + final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY; + verify(checkConfig, getNonCompilablePath("InputTypeUseAnnotationsOnQualifiedTypes.java"), + expected); + + } + +} diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/grammars/java8/InputAnnotationsOnArray.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/grammars/java8/InputAnnotationsOnArray.java new file mode 100644 index 000000000..364e658f9 --- /dev/null +++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/grammars/java8/InputAnnotationsOnArray.java @@ -0,0 +1,35 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.grammars.java8; + +import static java.lang.annotation.ElementType.TYPE_USE; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +public final class InputAnnotationsOnArray { + + private InputAnnotationsOnArray() { + } + + public static T[] checkNotNullContents(T @Nullable [] array) { + if (array == null) { + throw new NullPointerException(); + } + + return array; + } + + public static T[][] checkNotNullContents2(T @Nullable [] @Nullable [] array) { + if (array == null) { + throw new NullPointerException(); + } + + return array; + } +} + +@Retention(RetentionPolicy.CLASS) +@Target({ TYPE_USE }) +@interface Nullable { +} diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/grammars/java8/InputTypeUseAnnotationsOnQualifiedTypes.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/grammars/java8/InputTypeUseAnnotationsOnQualifiedTypes.java new file mode 100644 index 000000000..e4d4f02c1 --- /dev/null +++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/grammars/java8/InputTypeUseAnnotationsOnQualifiedTypes.java @@ -0,0 +1,31 @@ +//Compilable with Java8 +package com.puppycrawl.tools.checkstyle.grammars.java8; + +import java.awt.geom.Rectangle2D; +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; + +public class InputTypeUseAnnotationsOnQualifiedTypes { + /* Causes parse failure */ + Rectangle2D.@Ann Double rect = null; + + /* Causes parse failure */ + public final Rectangle2D.@Ann Double getRect1() { + return new Rectangle2D.Double(); + } + + /* Causes parse failure */ + public final Rectangle2D.Double getRect2() { + return new Rectangle2D.@Ann Double(); + } + + /* Amazingly does not cause parse failure */ + public final Rectangle2D.Double getRect3() { + Rectangle2D.@Ann Double rect = null; + return rect; + } +} + +@Target({ ElementType.TYPE_USE }) +@interface Ann { +}