Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkstyle fails with unusual annotations #8652

Closed
nrmancuso opened this issue Aug 11, 2020 · 2 comments · Fixed by #10435
Closed

Checkstyle fails with unusual annotations #8652

nrmancuso opened this issue Aug 11, 2020 · 2 comments · Fixed by #10435

Comments

@nrmancuso
Copy link
Member

nrmancuso commented Aug 11, 2020

Identified at #8634 (comment)
Check documentation: https://checkstyle.sourceforge.io/config_naming.html#ClassTypeParameterName

➜  IdeaProjects /usr/lib/jvm/java-14-openjdk/bin/javac --enable-preview --source 14 jdk14/test/langtools/tools/javac/annotations/typeAnnotations/newlocations/AllLocations.java
➜  IdeaProjects cat single-module-config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
    <!-- Here we use simple module to prove that we can parse files -->
    <module name="ClassTypeParameterName"/>
  </module>
</module>
➜  IdeaProjects cat jdk14/test/langtools/tools/javac/annotations/typeAnnotations/newlocations/AllLocations.java
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8027262
 * @summary Stress test for type annotatons
 * @compile AllLocations.java
 */

import java.util.function.Function;
import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.*;
import static java.lang.annotation.ElementType.*;
import java.io.*;
import java.lang.ref.WeakReference;

public class AllLocations {

    public class ParamStream<T> extends FileOutputStream {
        public ParamStream(File f) throws FileNotFoundException { super(f); }
    }

    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 <@H T extends @I Inner<@J ? extends @K String>> AllLocations(Object o) {}

    public @L Object @M [] @N [] arr = new @O Object @P [5] @Q [5];

    public Inner<@R ? extends @S Inner<@T ? extends @U Integer>> inner;

    public Function func(@V AllLocations this) {
        try (final ParamStream<@W Integer @X []> fs = new ParamStream<@Y Integer @Z []>(new File("testfile"))) {
            return @AA AllLocations.Inner<@AB String>::<@AC Integer>new;
        } catch(@AD Exception ex) {
            return null;
        }
    }

    public <@AE T extends @AF Inner<@AG Integer @AH []>> Function func2() {
        arr[0][0] = new @AI Inner((@AJ Object) arr[0]);
        return Ext.f((@AK Object) arr[0]) instanceof @AL Inner @AM [] @AN [] ?
            @AO @AP Ext::<@AQ @AR Integer> f :
            @AS @AT Ext::<@AU @AV Integer> f;
    }

    public Object func3(Object @AW [] arr) {
        Inner<@AX ? extends @AY Inner<@AZ ? extends @BA Integer>> loc;
        if (arr[0] instanceof @BB Inner @BC [] @BD [])
            return this.<Inner<@BE Integer @BF []>> func4();
        else
            return new <@BG Inner<@BH Integer>> @BI Inner<@BJ Inner<@BK Integer>>(null);
    }

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

  { Inner<@CC ? extends @CD Inner<@CE ? extends @CF Integer>> loc =
      new @CG Inner<@CH Inner<@CI Integer>>() {};
      Ext.func(Ext.func(@CJ WeakReference::new));
      Ext.func(Ext.func(@CK Ext::<@CL Integer>f));
      Ext.func((@CM Object a) -> { @CN Object b = a; return b; });
  }

}

class Ext {
    public static <@CO T> Object f(Object o) {
        return null;
    }
    public static Function func(Function f) { return f; }
}


@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 H { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface I { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface J { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface K { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface L { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface M { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface N { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface O { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface P { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface Q { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface R { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface S { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface T { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface U { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface V { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface W { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface X { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface Y { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface Z { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AA { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AB { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AC { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AD { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AE { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AF { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AG { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AH { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AI { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AJ { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AK { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AL { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AM { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AN { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AO { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AP { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AQ { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AR { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AS { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AT { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AU { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AV { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AW { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AX { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AY { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface AZ { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BA { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BB { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BC { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BD { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BE { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BF { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BG { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BH { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BI { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BJ { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface BK { }
@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 { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CC { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CD { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CE { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CF { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CG { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CH { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CI { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CJ { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CK { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CL { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CM { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CN { }
@Retention(RUNTIME) @Target({TYPE_USE}) @interface CO { }
➜  IdeaProjects java -jar /home/nick/IdeaProjects/checkstyle/target/checkstyle-8.36-SNAPSHOT-all.jar -c single-module-config.xml jdk14/test/langtools/tools/javac/annotations/typeAnnotations/newlocations/AllLocations.java
Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing jdk14/test/langtools/tools/javac/annotations/typeAnnotations/newlocations/AllLocations.java
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:311)
        at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:221)
        at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:408)
        at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:331)
        at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:190)
        at com.puppycrawl.tools.checkstyle.Main.main(Main.java:125)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: IllegalStateException occurred while parsing file /home/nick/IdeaProjects/jdk14/test/langtools/tools/javac/annotations/typeAnnotations/newlocations/AllLocations.java.
        at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:120)
        at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:149)
        at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:87)
        at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:337)
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:298)
        ... 5 more
Caused by: java.lang.IllegalStateException: /home/nick/IdeaProjects/jdk14/test/langtools/tools/javac/annotations/typeAnnotations/newlocations/AllLocations.java:78:53: expecting LBRACK, found 'Inner'
        at com.puppycrawl.tools.checkstyle.JavaParser$1.reportError(JavaParser.java:108)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeDefinition(GeneratedJavaRecognizer.java:424)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.compilationUnit(GeneratedJavaRecognizer.java:212)
        at com.puppycrawl.tools.checkstyle.JavaParser.parse(JavaParser.java:114)
        ... 9 more
Caused by: /home/nick/IdeaProjects/jdk14/test/langtools/tools/javac/annotations/typeAnnotations/newlocations/AllLocations.java:78:53: expecting LBRACK, found 'Inner'
        at antlr.Parser.match(Parser.java:211)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.classTypeSpec(GeneratedJavaRecognizer.java:908)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeArgument(GeneratedJavaRecognizer.java:1415)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeArguments(GeneratedJavaRecognizer.java:1326)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.newExpression(GeneratedJavaRecognizer.java:8920)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.primaryExpression(GeneratedJavaRecognizer.java:4895)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.postfixExpression(GeneratedJavaRecognizer.java:8586)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.castExpression(GeneratedJavaRecognizer.java:8522)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.unaryExpressionNotPlusMinus(GeneratedJavaRecognizer.java:8378)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.unaryExpression(GeneratedJavaRecognizer.java:8308)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.multiplicativeExpression(GeneratedJavaRecognizer.java:8169)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.additiveExpression(GeneratedJavaRecognizer.java:8120)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.shiftExpression(GeneratedJavaRecognizer.java:7998)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.relationalExpression(GeneratedJavaRecognizer.java:7886)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.equalityExpression(GeneratedJavaRecognizer.java:7837)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.andExpression(GeneratedJavaRecognizer.java:7808)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.exclusiveOrExpression(GeneratedJavaRecognizer.java:7779)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.inclusiveOrExpression(GeneratedJavaRecognizer.java:7750)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.logicalAndExpression(GeneratedJavaRecognizer.java:7721)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.logicalOrExpression(GeneratedJavaRecognizer.java:7692)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.conditionalExpression(GeneratedJavaRecognizer.java:2356)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.assignmentExpression(GeneratedJavaRecognizer.java:7441)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.expression(GeneratedJavaRecognizer.java:5336)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.traditionalStatement(GeneratedJavaRecognizer.java:5848)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.statement(GeneratedJavaRecognizer.java:4815)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.elseStatement(GeneratedJavaRecognizer.java:6138)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.traditionalStatement(GeneratedJavaRecognizer.java:5666)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.statement(GeneratedJavaRecognizer.java:4815)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.compoundStatement(GeneratedJavaRecognizer.java:4544)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.field(GeneratedJavaRecognizer.java:3159)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.classBlock(GeneratedJavaRecognizer.java:3416)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.classDefinition(GeneratedJavaRecognizer.java:646)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeDefinitionInternal(GeneratedJavaRecognizer.java:561)
        at com.puppycrawl.tools.checkstyle.grammar.GeneratedJavaRecognizer.typeDefinition(GeneratedJavaRecognizer.java:402)
        ... 11 more
Checkstyle ends with 1 errors.


Checkstyle should be able to parse this file, since it is compilable. There are a number of other openjdk files with annotations that cause exceptions when being parsed by Checkstyle:


jdk14/test/langtools/tools/javac/annotations/typeAnnotations/8013180/QualifiedName.java
jdk14/test/langtools/tools/javac/parser/SingleCommaAnnotationValue.java
jdk14/test/langtools/tools/javac/annotations/typeAnnotations/failures/CheckErrorsForSource7.java
jdk14/test/langtools/tools/javac/annotations/typeAnnotations/failures/T8011722.java
jdk14/test/langtools/tools/javac/annotations/typeAnnotations/newlocations/AllLocations.java
jdk14/test/langtools/tools/javac/annotations/typeAnnotations/newlocations/NestedTypes.java
jdk14/test/langtools/tools/javac/treeannotests/AnnoTreeTests.java

These should be added to any test input related to fixing this issue.

@nrmancuso nrmancuso changed the title Checkstyle fails on annotation in qualified name Checkstyle fails with annotations on types and qualified names Aug 11, 2020
@nrmancuso nrmancuso changed the title Checkstyle fails with annotations on types and qualified names Checkstyle fails with unusual annotations Aug 11, 2020
@romani romani added the antlr label Oct 7, 2020
@nrmancuso
Copy link
Member Author

Once this issue is fixed, we can remove the following BeforeExecutionExclusionFileFilter modules for the following files from openjdk14-excluded.files:
T8011722.java
AllLocations.java
AnnoTreeTests.java

nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 27, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 27, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jul 27, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 2, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 2, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 2, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 2, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 2, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 2, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 11, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 11, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 11, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 11, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 12, 2021
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 12, 2021
@romani romani added the bug label Aug 13, 2021
@romani romani added this to the 9.0 milestone Aug 13, 2021
@romani
Copy link
Member

romani commented Aug 13, 2021

Fix is merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants