Skip to content

MethodCountCheck: add ANNOTATION_DEF to toknes to avoid NPE #4534

@rnveach

Description

@rnveach

Found at #4532 (comment) .

I surprised that this code is compilable, I thought only methods signatures could be in annotation declaration.

from https://docs.oracle.com/javase/tutorial/java/annotations/declaring.html

Annotation types are a form of interface

$ javac TextClass.java

$ cat TestClass.java
@interface TestClass {
    Object object = new Object(){
        @Override
        public String toString() {
            return new String();
        }
    };
}

$ cat TestConfig.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8"/>

    <module name="TreeWalker">
      <module name="MethodCount">
        <property name="maxTotal" value="35"/>
      </module>
    </module>
</module>

$ java -jar checkstyle-7.8.2-all.jar -c TestConfig.xml TestClass.java
Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing TestClass.java
    at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:295)
    at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:213)
    at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:425)
    at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:359)
    at com.puppycrawl.tools.checkstyle.Main.main(Main.java:174)
Caused by: java.lang.NullPointerException
    at com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck$MethodCounter.access$000(MethodCountCheck.java:218)
    at com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.raiseCounter(MethodCountCheck.java:140)
    at com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck.visitToken(MethodCountCheck.java:112)
    at com.puppycrawl.tools.checkstyle.TreeWalker.notifyVisit(TreeWalker.java:370)
    at com.puppycrawl.tools.checkstyle.TreeWalker.processIter(TreeWalker.java:507)
    at com.puppycrawl.tools.checkstyle.TreeWalker.walk(TreeWalker.java:312)
    at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:184)
    at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:78)
    at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:316)
    at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:286)
    ... 4 more
Checkstyle ends with 1 errors.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions