Add new tests

Refs #3643
This commit is contained in:
Clément Fournier
2022-11-05 16:30:01 +01:00
parent cd075d2459
commit b39ba3c560
6 changed files with 135 additions and 8 deletions

View File

@ -1483,7 +1483,7 @@ void VariableDeclaratorId() :
{
(LOOKAHEAD(2) t=<IDENTIFIER> "." <THIS> { checkforBadExplicitReceiverParameter(); jjtThis.setExplicitReceiverParameter(); image=t.image + ".this"; }
| t=<THIS> { checkforBadExplicitReceiverParameter(); jjtThis.setExplicitReceiverParameter(); image = t.image;}
| t=<IDENTIFIER> { image = t.image; } ( "[" "]" { jjtThis.bumpArrayDepth(); })*
| t=<IDENTIFIER> { 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=<IDENTIFIER> {s.append(t.image);}
[ LOOKAHEAD(2) TypeArguments() ]
( LOOKAHEAD(2) "." t=<IDENTIFIER> {s.append('.').append(t.image);} [ LOOKAHEAD(2) TypeArguments() ] )*
( LOOKAHEAD(2) "." (AnnotationNoNode())* t=<IDENTIFIER> {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()
}

View File

@ -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() { } } }");

View File

@ -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(); }
}

View File

@ -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 <String>Object();
Object okWithPackage = new <String>java.lang.Object();
Object ok2 = new <String>Outer.Inner();
Object o3 = new <String>Outer().new <String>NonStatic();
Object o4 = new <String>GenericOuter<String>();
Object o5 = new <String>GenericOuter<String>().new <String>GenericInner<String>();
}
class Outer {
static class Inner {}
class NonStatic {}
}
class GenericOuter<T> {
class GenericInner<U> { }
}

View File

@ -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<A,B> {
class c4a {}
public c4() { <String>super(); }
}
class c5 extends c4.c4a {
c5() { new c4().super(); }
c5(int a) { new c4().<String>super(); }
}

View File

@ -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<X> {
class GInner2<Y, Z> {}
}
class Static {}
class GStatic<X, Y> {
class GStatic2<Z> {}
}
}
class MyList<K> { }
class Test1 {
@TypeAnnotation Outer . @TypeAnnotation GInner<@TypeAnnotation MyList<@TypeAnnotation Object @TypeAnnotation[] @TypeAnnotation[]>>
.@TypeAnnotation GInner2<@TypeAnnotation Integer, @TypeAnnotation Object> @TypeAnnotation[] @TypeAnnotation[] f4arrtop;
}
}