[英]Exclude getters and setters from method count in CheckStyle
I got custom checkstyle.xml
file with entry for MethodCount like this: 我得到了带有方法计数条目的自定义
checkstyle.xml
文件,如下所示:
<module name="MethodCount">
<property name="maxTotal" value="20"/>
<property name="maxPrivate" value="10"/>
<property name="maxPublic" value="10"/>
<property name="severity" value="error"/>
</module>
However this creates a problem for huge model classes with getters and setters, which are provided by another web service. 但是,这给具有getter和setter的大型模型类带来了问题,这些模型类由另一个Web服务提供。 Can i somehow exclude this methods?
我可以以某种方式排除这种方法吗? Or is this considered a bad practice to not count those?
还是不计入这些是不正确的做法?
You can create suppression.xml
file: 您可以创建
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>
And point it in checkstyle.xml
并将其指向
checkstyle.xml
<module name="SuppressionFilter">
<property name="file" value="./suppression.xml"/>
</module>
Then you will suppress check MethodCount
for files ending with Entity.java
or Dto.java
然后,你将抑制检查
MethodCount
与结尾的文件Entity.java
或Dto.java
AFAIK you cannot suppress only getters/setters. 不能仅禁止吸气剂/阻气剂。 Generally, for data structures like entities or dtos it is not a problem for having more than 5 fields with getters and setters.
通常,对于诸如实体或dto之类的数据结构,具有5个以上具有getter和setter的字段不是问题。
But if you have real objects adding setter/getters for each field is considering a bad practice. 但是,如果您有真实的对象,则为每个字段添加setter / getters是一种不好的做法。
Not very sure if it's what you need but you can ignore getter and setter methods from inspections: 不太确定这是否是您需要的,但是您可以从检查中忽略getter和setter方法:
Goto Settings
( CTRL + Alt + S ), then Editor->Inspections->Java->Class metrics->Class with too many methods 转到
Settings
( CTRL + Alt + S ),然后使用过多的方法编辑器->检查-> Java->类指标->类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.