Skip to content

Commit

Permalink
Issue checkstyle#8652: Checkstyle fails with unusual annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
nrmancuso committed Aug 2, 2021
1 parent be179ff commit c40f507
Show file tree
Hide file tree
Showing 5 changed files with 1,077 additions and 14 deletions.
21 changes: 9 additions & 12 deletions src/main/java/com/puppycrawl/tools/checkstyle/JavaAstVisitor.java
Expand Up @@ -629,21 +629,17 @@ public DetailAstImpl visitLastFormalParameter(

@Override
public DetailAstImpl visitQualifiedName(CheckstyleJavaParser.QualifiedNameContext ctx) {
DetailAstImpl ast = visit(ctx.id(0));
final DetailAstImpl ast = visit(ctx.id());
final DetailAstPair currentAst = new DetailAstPair();
DetailAstPair.addASTChild(currentAst, ast);

for (int i = 1; i < ctx.children.size(); i += 2) {

// Token to become root
final DetailAstImpl temp = create((Token) ctx.children.get(i).getPayload());
DetailAstPair.makeAstRoot(currentAst, temp);

// Token to become child or next sibling
final Object child = ctx.children.get(i + 1).getPayload();
ast = visit((ParseTree) child);

DetailAstPair.addASTChild(currentAst, ast);
for (int i = 0; i < ctx.extended.size(); i++) {
final ParserRuleContext extendedContext = ctx.extended.get(i);
final DetailAstImpl dot = create(extendedContext.start);
DetailAstPair.makeAstRoot(currentAst, dot);
final List<ParseTree> childList = extendedContext
.children.subList(1, extendedContext.children.size());
processChildren(dot, childList);
}
return currentAst.getRoot();
}
Expand Down Expand Up @@ -1711,6 +1707,7 @@ public DetailAstImpl visitCreator(CheckstyleJavaParser.CreatorContext ctx) {
@Override
public DetailAstImpl visitCreatedNameObject(CheckstyleJavaParser.CreatedNameObjectContext ctx) {
final DetailAstPair currentAST = new DetailAstPair();
DetailAstPair.addASTChild(currentAST, visit(ctx.annotations()));
DetailAstPair.addASTChild(currentAST, visit(ctx.id()));
DetailAstPair.addASTChild(currentAST, visit(ctx.typeArgumentsOrDiamond()));

Expand Down
Expand Up @@ -356,7 +356,11 @@ lastFormalParameter
;

qualifiedName
: id (DOT id)*
: id extended+=qualifiedNameExtended*
;

qualifiedNameExtended
: DOT annotations[false] id
;

literal
Expand Down Expand Up @@ -750,7 +754,8 @@ creator
;

createdName
: id typeArgumentsOrDiamond? extended+=createdNameExtended* #createdNameObject
: annotations[false] id typeArgumentsOrDiamond?
extended+=createdNameExtended* #createdNameObject
| primitiveType #createdNamePrimitive
;

Expand Down
Expand Up @@ -266,4 +266,10 @@ public void testUncommon4() throws Exception {
getPath("InputAntlr4AstRegressionUncommon4.java"),
JavaParser.Options.WITH_COMMENTS);
}

@Test
public void testUnusualAnnotation() throws Exception {
verifyAst(getPath("InputAntlr4AstRegressionUnusualAnnotation.txt"),
getPath("InputAntlr4AstRegressionUnusualAnnotation.java"));
}
}
@@ -0,0 +1,62 @@
package com.puppycrawl.tools.checkstyle.grammar.antlr4;

import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.ref.WeakReference;
import java.util.function.Function;

public class InputAntlr4AstRegressionUnusualAnnotation {

public class Inner<S> {
public Inner() {}
public <@A T> Inner(@B Object o) {}
public <@C T> Object g(Inner<@D S> this, Object @E [] o) {
return new @F int @G [5];
}
}

public <@BL T extends @BO Inner<@BP Integer @BQ []>>
@BR Inner<@BS Inner<@BT String>> func4() {
return (@BU Inner<@BV Inner<@BW String>>)
new <@BX Inner<@BY Integer>> @BZ Inner<@CA Inner<@CB String>>(null);
}

class Test {
class InnerException extends Exception { }
void foo() throws @C Test.@C InnerException { }
}

@Target(ElementType.TYPE_USE) @interface C { }
}

@Retention(RUNTIME) @Target({TYPE_USE}) @interface A { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface B { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface C { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface D { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface E { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface F { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface G { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BL { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BM { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BN { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BO { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BP { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BQ { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BR { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BS { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BT { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BU { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BV { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BW { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BX { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BY { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BZ { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CA { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CB { }

0 comments on commit c40f507

Please sign in to comment.