gpt4 book ai didi

java - 使用 CheckStyle 阻止对内部接口(interface)/类的公共(public)访问

转载 作者:行者123 更新时间:2023-12-04 07:56:36 27 4
gpt4 key购买 nike

有没有办法更新 checkstyle 设置以只允许封装私有(private)内部接口(interface)或类?例如


public class A {
...
public interface B { // should flag it as error
..
}
}

public class A {
...
interface B { // valid syntax
..
}
}

最佳答案

您可以使用MatchXPath为了这:


<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">

<module name="Checker">
<property name="charset" value="UTF-8"/>
<property name="haltOnException" value="true"/>
<property name="severity" value="error"/>

<module name="TreeWalker">
<module name="MatchXpath">
<property name="query" value="/CLASS_DEF/OBJBLOCK/(CLASS_DEF | INTERFACE_DEF)/MODIFIERS[(*)]"/>
<message key="matchxpath.match"
value="Only package private inner interfaces or classes are allowed."/>
</module>
</module>
</module>



public class A {

public interface B { // violation

}

public class C { // violation

}

public enum D { // ok, enum not specified in config

}

class E { // ok, no modifier

}

interface F { ok, no modifier

}

private class G { // violation

}

private interface H { // violation

}
}



➜ src java -jar checkstyle-8.41-all.jar -c config.xml A.java
Starting audit...
[ERROR] A.java:3:5: Only package private inner interfaces or classes are allowed. [MatchXpath]
[ERROR] A.java:7:5: Only package private inner interfaces or classes are allowed. [MatchXpath]
[ERROR] A.java:23:5: Only package private inner interfaces or classes are allowed. [MatchXpath]
[ERROR] A.java:27:5: Only package private inner interfaces or classes are allowed. [MatchXpath]
Audit done.
Checkstyle ends with 4 errors.


关于java - 使用 CheckStyle 阻止对内部接口(interface)/类的公共(public)访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66674810/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com