-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Closed
Description
Child of #8452
Check documentation: https://checkstyle.sourceforge.io/config_metrics.html#ClassFanOutComplexity
From check documentation:
Checks the number of other classes a given class relies on. Also the square of this has been shown to indicate the amount of maintenance required in functional programs (on a file basis) at least.
➜ full-record-grammar /usr/lib/jvm/java-14-openjdk/bin/javac --enable-preview --source 14 TestClass.java
Note: TestClass.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
➜ full-record-grammar cat 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">
<module name="ClassFanOutComplexity">
<property name="max" value="2"/>
</module>
</module>
</module>
➜ full-record-grammar cat TestClass.java
import javax.naming.NamingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
class TestClass { // violation
private class InnerClass {
public List _list = new ArrayList();
}
private class AnotherInnerClass {
public String _string = "";
}
public Set _set = /*block comment*/new HashSet();
public Map _map = new HashMap();
public String _string = "";
public int[] _intArray = new int[0];
public InnerClass _innerClass;
public AnotherInnerClass _anotherInnerClass;
public void foo() throws NamingException {
}
}
record MyRecord1(boolean a, boolean b) { // should be violation
private class InnerClass {
public List _list = new ArrayList();
}
private class AnotherInnerClass {
public String _string = "";
}
public static Set _set = /*block comment*/new HashSet();
public static Map _map = new HashMap();
public static String _string = "";
public static int[] _intArray = new int[0];
public static InnerClass _innerClass;
public static AnotherInnerClass _anotherInnerClass;
public void foo() throws NamingException {
}
}
➜ full-record-grammar java $RUN_LOCALE -jar ~/IdeaProjects/checkstyle/target/checkstyle-8.35-SNAPSHOT-all.jar -c config.xml TestClass.java
Starting audit...
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:9:1: Class Fan-Out Complexity is 3 (max allowed is 2). [ClassFanOutComplexity]
Audit done.
Checkstyle ends with 1 errors.
This check does not work with record definitions, so we will need to add support for record syntax to this check.
This check is not configurable to target only classes or only interfaces, or enums or annotations. So addition of record is fine.