从CheckStyle的方法计数中排除getter和setter

时间:2018-07-13 07:50:27

标签: java intellij-idea checkstyle

我得到了自定义checkstyle.xml文件,其中带有MethodCount条目,如下所示:

<module name="MethodCount">
    <property name="maxTotal" value="20"/>
    <property name="maxPrivate" value="10"/>
    <property name="maxPublic" value="10"/>
    <property name="severity" value="error"/>
</module>

但是,这给另一个由Web服务提供的具有getter和setter的大型模型类造成了问题。我可以以某种方式排除这种方法吗?还是不计入这些是不明智的做法?

2 个答案:

答案 0 :(得分:1)

您可以创建suppression.xml文件:

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
    "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
    "https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
    <suppress files="\w*(Dto.java|Entity.java)\b" checks="MethodCount"/>
</suppressions>

并将其指向checkstyle.xml

<module name="SuppressionFilter">
    <property name="file" value="./suppression.xml"/>
</module>

然后,您将取消检查MethodCountEntity.javaDto.java结尾的文件

因此,您不能仅抑制吸气剂/阻气剂。通常,对于诸如实体或dto之类的数据结构,使用getter和setter的字段超过5个就不是问题。

但是,如果您有真实的对象,则在每个字段中添加setter / getter就是一种不好的做法。

答案 1 :(得分:1)

不太确定这是否是您需要的,但是您可以从检查中忽略getter和setter方法:

转到Settings CTRL + Alt + S ), 然后编辑器->检查-> Java->类指标->使用太多方法的类

enter image description here

相关问题