From b39ba3c560e94c369a43fdf21b444be523e88319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 5 Nov 2022 16:30:01 +0100 Subject: [PATCH] Add new tests Refs #3643 --- pmd-java/etc/grammar/Java.jjt | 16 ++-- .../pmd/lang/java/ast/JDKVersionTest.java | 20 +++++ .../java5/annotation_array_init.java | 5 ++ .../jdkversiontests/java5/generic_ctors.java | 19 +++++ .../java5/generic_super_ctor.java | 10 +++ .../java8/type_annotations.java | 73 +++++++++++++++++++ 6 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/annotation_array_init.java create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/generic_ctors.java create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/generic_super_ctor.java create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java8/type_annotations.java diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 86bb21588a..689c5ed766 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1483,7 +1483,7 @@ void VariableDeclaratorId() : { (LOOKAHEAD(2) t= "." { checkforBadExplicitReceiverParameter(); jjtThis.setExplicitReceiverParameter(); image=t.image + ".this"; } | t= { checkforBadExplicitReceiverParameter(); jjtThis.setExplicitReceiverParameter(); image = t.image;} - | t= { image = t.image; } ( "[" "]" { jjtThis.bumpArrayDepth(); })* + | t= { image = t.image; } ( (AnnotationNoNode())* "[" "]" { jjtThis.bumpArrayDepth(); })* ) { checkForBadAssertUsage(image, "a variable name"); @@ -1526,7 +1526,7 @@ void MethodDeclarator() : checkForBadEnumUsage(t.image, "a method name"); jjtThis.setImage( t.image ); } - FormalParameters() ( "[" "]" )* + FormalParameters() ( (AnnotationNoNode())* "[" "]" )* } @@ -1606,7 +1606,7 @@ void ClassOrInterfaceType(): { t= {s.append(t.image);} [ LOOKAHEAD(2) TypeArguments() ] - ( LOOKAHEAD(2) "." t= {s.append('.').append(t.image);} [ LOOKAHEAD(2) TypeArguments() ] )* + ( LOOKAHEAD(2) "." (AnnotationNoNode())* t= {s.append('.').append(t.image);} [ LOOKAHEAD(2) TypeArguments() ] )* {jjtThis.setImage(s.toString());} } @@ -1832,7 +1832,7 @@ void InstanceOfExpression() #InstanceOfExpression(>1): {} { RelationalExpression() - [ "instanceof" + [ "instanceof" (AnnotationNoNode())* ( LOOKAHEAD("final" | "@") {checkforBadInstanceOfPattern();} Pattern() | @@ -2103,11 +2103,11 @@ void AllocationExpression(): void ArrayDimsAndInits() : {} { + LOOKAHEAD((TypeAnnotation())* "[" "]") + ((AnnotationNoNode())* "[" "]" {jjtThis.bumpArrayDepth();})+ ArrayInitializer() +| ( LOOKAHEAD((TypeAnnotation())* "[" UnaryExprNotPmStart() ) (AnnotationNoNode())* "[" Expression() "]" {jjtThis.bumpArrayDepth();} )+ + ( LOOKAHEAD((TypeAnnotation())* "[") (AnnotationNoNode())* "[" "]" {jjtThis.bumpArrayDepth();} )* - LOOKAHEAD(2) - ( LOOKAHEAD(2) (TypeAnnotation())* "[" Expression() "]" {jjtThis.bumpArrayDepth();})+ ( LOOKAHEAD(2) "[" "]" {jjtThis.bumpArrayDepth();} )* -| - ( "[" "]" {jjtThis.bumpArrayDepth();})+ ArrayInitializer() } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java index 97fda69133..f2dfb3c082 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/JDKVersionTest.java @@ -84,6 +84,21 @@ public class JDKVersionTest { java5.parseResource("jdk15_varargs.java"); } + @Test + public void testGenericCtorCalls() { + java5.parseResource("java5/generic_ctors.java"); + } + + @Test + public void testGenericSuperCtorCalls() { + java5.parseResource("java5/generic_super_ctor.java"); + } + + @Test + public void testAnnotArrayInitializer() { + java5.parseResource("java5/annotation_array_init.java"); + } + @Test(expected = ParseException.class) public void testVarargsShouldFailWith14() { java4.parseResource("jdk15_varargs.java"); @@ -217,6 +232,11 @@ public class JDKVersionTest { java8.parse("public class Foo { private void bar() { } }"); } + @Test + public final void testTypeAnnotations() { + java8.parseResource("java8/type_annotations.java"); + } + @Test public final void testNestedPrivateMethods() { java8.parse("public interface Baz { public static class Foo { private void bar() { } } }"); diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/annotation_array_init.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/annotation_array_init.java new file mode 100644 index 0000000000..6ba1787267 --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/annotation_array_init.java @@ -0,0 +1,5 @@ +// From https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/grammar/antlr4/InputAntlr4AstRegressionSingleCommaInArrayInit.java +class AnnotationCommaArrayInit { + @Foo({,}) void b() { } + @interface Foo { int[] value(); } +} diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/generic_ctors.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/generic_ctors.java new file mode 100644 index 0000000000..abf6d12295 --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/generic_ctors.java @@ -0,0 +1,19 @@ + +// From https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/genericwhitespace/InputGenericWhitespaceDefault.java +// and https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/grammar/antlr4/InputAntlr4AstRegressionUncommon4.java +class GenericConstructor { + Object ok = new Object(); + Object okWithPackage = new java.lang.Object(); + Object ok2 = new Outer.Inner(); + Object o3 = new Outer().new NonStatic(); + Object o4 = new GenericOuter(); + Object o5 = new GenericOuter().new GenericInner(); +} +class Outer { + static class Inner {} + class NonStatic {} +} +class GenericOuter { + class GenericInner { } +} + diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/generic_super_ctor.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/generic_super_ctor.java new file mode 100644 index 0000000000..a762aac954 --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java5/generic_super_ctor.java @@ -0,0 +1,10 @@ +// From https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/grammar/InputRegressionJavaClass2.java +class c4 { + class c4a {} + + public c4() { super(); } +} +class c5 extends c4.c4a { + c5() { new c4().super(); } + c5(int a) { new c4().super(); } +} diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java8/type_annotations.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java8/type_annotations.java new file mode 100644 index 0000000000..9940d49cab --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java8/type_annotations.java @@ -0,0 +1,73 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +// From https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/InputNoWhitespaceBeforeAnnotations.java +// and https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/nowhitespaceafter/InputNoWhitespaceAfterArrayDeclarationsAndAnno.java +// and https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/nowhitespaceafter/InputNoWhitespaceAfterNewTypeStructure.java +// and https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/grammar/java8/InputAnnotations12.java +@Target(ElementType.TYPE_USE) +@interface NonNull {} + +class AnnotedArrayType { + @NonNull int @NonNull[] @NonNull[] field1; + @NonNull int @NonNull [] @NonNull [] field2; + public String m2()@NonNull[]@NonNull[] { return null; } + public String@NonNull[]@NonNull[] m2a() { return null; } + public void run() { + for (String a@NonNull[] : m2()) { + } + } + void vararg(@NonNull String @NonNull [] @NonNull ... vararg2) { } + public void vararg2(@NonNull int @NonNull ... vararg) {} + public void vararg3(@NonNull int[] @NonNull ... vararg) {} +} +// From https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/avoidnoargumentsuperconstructorcall/InputAvoidNoArgumentSuperConstructorCall.java +// and https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/grammar/antlr4/InputAntlr4AstRegressionAnnotationOnQualifiedTypes.java +// and https://github.com/checkstyle/checkstyle/blob/checkstyle-9.1/src/test/resources/com/puppycrawl/tools/checkstyle/grammar/antlr4/InputAntlr4AstRegressionNestedTypeParametersAndArrayDeclarators.java +@Target({ + ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.PARAMETER, + ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) +@interface TypeAnnotation { +} + +class Rectangle2D { + class Double{} +} +class TypeAnnotations { + // We can use type Annotations with generic type arguments + private Map.@TypeAnnotation Entry entry; + // Type annotations in instanceof statements + boolean isNonNull = "string" instanceof @TypeAnnotation String; + // java.awt.geom.Rectangle2D + public final Rectangle2D.@TypeAnnotation Double getRect1() { + return new Rectangle2D.Double(); + } + public final Rectangle2D.Double getRect2() { + return new Rectangle2D.@TypeAnnotation Double(); + } + public final Rectangle2D.Double getRect3() { + Rectangle2D.@TypeAnnotation Double rect = null; + int[][] i = new int @TypeAnnotation [1] @TypeAnnotation[]; + return rect; + } + + class Outer { + class Inner { + class Inner2 { + } + } + class GInner { + class GInner2 {} + } + class Static {} + class GStatic { + class GStatic2 {} + } + } + class MyList { } + class Test1 { + @TypeAnnotation Outer . @TypeAnnotation GInner<@TypeAnnotation MyList<@TypeAnnotation Object @TypeAnnotation[] @TypeAnnotation[]>> + .@TypeAnnotation GInner2<@TypeAnnotation Integer, @TypeAnnotation Object> @TypeAnnotation[] @TypeAnnotation[] f4arrtop; + } +}