Skip to content

MethodCountCheck: class counts include anonymous methods #4539

@rnveach

Description

@rnveach

Identified in regression at #4535 :

$ cat TestClass.java
public class TestClass {
    void method() {
        Runnable r = (new Runnable() {
                public void run() {
                }
            });
    }
}

$ 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="MethodCountCheck">
  <property name="maxTotal" value="1" />
</module>
    </module>
</module>

$ java -jar checkstyle-7.8.2-all.jar -c TestConfig.xml TestClass.java
Starting audit...
[ERROR] TestClass.java:1: Total number of methods is 2 (max allowed is 1). [MethodCount]
Audit done.
Checkstyle ends with 1 errors.

Method count is set to allow a max of 1 method per class.
Checkstyle incorrectly reports that the class TestClass has 2 methods. It is incorrectly including the anonymous method in the counting of TestClass. I don't think it should be included, as it is a separate class from TestClass.
I am expecting no violations on this code.

Attention: after fix, counts are expected to be lowered, so we should change our checkstyle_checks.xml

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