I got custom checkstyle.xml file with entry for MethodCount like this:
<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. Can i somehow exclude this methods? Or is this considered a bad practice to not count those?
You can create suppression.xml file:
<?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
<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
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.
But if you have real objects adding setter/getters for each field is considering a bad practice.
Not very sure if it's what you need but you can ignore getter and setter methods from inspections:
Goto Settings (CTRL+Alt+S),
then Editor->Inspections->Java->Class metrics->Class with too many methods
Related
I'm trying to obfuscate a JAR file using the yguard 3.0.0 maven plugin. The obfuscated JAR is as almost as expected, it is shrinked and with all the private methods and variables renamed. I need that package names will be renamed too and this is performed, but the spring XML files needed to start my Tomcat are not being updated with the obfuscated packages.
My ant task is the following:
<configuration>
<tasks>
<property name="runtime-classpath" refid="maven.runtime.classpath"/>
<taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" classpath="${runtime-classpath}"/>
<yguard>
<inoutpair in="C:/test/webapp.jar" out="C:/test/webapp_obfuscated.jar" />
<shrink>
<property name="error-checking" value="pedantic"/>
</shrink>
<rename>
<adjust replaceContent="true" replaceName="true">
<include name="ApplicationContext.xml"/>
</adjust>
</rename>
</yguard>
</tasks>
</configuration>
Note that here in my example, I'm only trying to deal with the ApplicationContext.xml, but this file remains with the same classnames that the no obfuscated version. I'm sure that the yguard task is doing something into my ApplicationContext.xml because I have a tag in the file and the path to a file is properly obfuscated but the classnames and the other stuff no:
<!-- Properties ldap -->
<bean id="ldapProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" scope="singleton">
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:A/A/B/E/ldap.properties</value> <--Obfuscated!-->
</list>
</property>
</bean>
<bean id="authenticationBO" class="com.grifols.grb.authentication.bo.AuthenticationBO" scope="singleton">
<property name="dbAccess" ref="dbAccessGRB"/>
<property name="usersSecurityBO" ref="usersSecurityBO" />
<property name="settings" ref="settings" />
<property name="ldapProperties" ref="ldapProperties" />
</bean>
According to the Yguard documentation I think that I only have to use replaceContent="true" and detail which file but I'm not
Any idea? I really appreciate any help you can provide.
Ivan
yGuard is able to replace either file/path names or class names in resource files, but not both at once.
I.e.
<adjust replaceContent="true">
will adjust
<example>
<class>com.yworks.yguard.StringReplacer</class>
<file>com/yworks/yguard/StringReplacer.properties</file>
</example>
to
<example>
<class>com.yworks.yguard.StringReplacer</class>
<file>A/A/A/SR.properties</file>
</example>
With
<adjust replaceContent="true" replaceContentSeparator=".">
the result will be
<example>
<class>A.A.A.SR</class>
<file>com/yworks/yguard/StringReplacer.properties</file>
</example>
However, the desired result
<example>
<class>A.A.A.SR</class>
<file>A/A/A/SR.properties</file>
</example>
is not (yet) supported.
In our project we want to ensure that the private constants always start with _ (underscore) and rest all do not start with an underscore.
The checkstyle check ConstantName fails to treat the interface constants as public and applies the rules of private modifier.
We are using checkstyle 8.35 in our gradle project to analyse java code (OpenJdk 11, Gradle 6.4).
Below is the code snippet of Interface with constants.
public interface MyInterface() {
int MAX_SIZE = 1024;
//Some methods here
}
Checkstyle configuration for ConstantName check is as below
<module name="ConstantName">
<property name="format" value="^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
<property name="applyToPrivate" value="false"/>
</module>
<module name="ConstantName">
<property name="format" value="^_[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
<property name="applyToPublic" value="false"/>
<property name="applyToProtected" value="false"/>
<property name="applyToPackage" value="false"/>
</module>
Post running the checkstyle analysis the error is reported for MAX_SIZE as Name 'MAX_SIZE' must match pattern '^_[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'. while our expectation is No Errors.
Judging by a quick scan of the source code, this is a bug. They're only considering the constant to be public if the keyword public is present (which is the case in classes, but not interfaces).
I would suggest reporting this as an issue at https://github.com/checkstyle/checkstyle/issues
How can I limit an annotation 's target only to be 'ElementType.TYPE_USE', and any other use would be rejected by 'maven checkstyle' plugin
I could only think of RegexpMultiline, if you want to do it with Checkstyle. You might configure it like so (regex):
<module name="RegexpMultiline">
<property name="format" value="\bElementType\s*\.\s*(?!TYPE_USE)\w+\b"/>
<property name="message" value="Illegal ElementType. Only TYPE_USE is permitted."/>
<property name="fileExtensions" value="java"/>
</module>
This solution is far from perfect, as it will also trigger on comments, and is not limited to annotations. Write a custom check if you need more control.
I am using checkstyle to check violation in my code. One of the module in my configuration is about duplicate code. Since I am using StrictDuplicateCode I get violation on duplication for javaDoc as well.
Can anyone guide me to achieve my goal?
In the meantime, I tried following BUT it doesn’t work:
To suppress duplication with java doc I created a separate xml file (JavaDocSup.xml) with following content
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress checks="JavadocStyleCheck"
files="SomeClass.java"
/>
</suppressions>
Then I added following code in my main configuration file. Following is the code for my config file.
<?xml version="1.0" encoding="UTF-8"?>
<!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="severity" value="warning"/>
<module name="TreeWalker">
<module name="MethodLength">
<property name="max" value="50"/>
</module>
<module name="ParameterNumber">
<property name="max" value="4"/>
</module>
<module name="CyclomaticComplexity"/>
</module>
<module name="StrictDuplicateCode">
<property name="fileExtensions" value="java"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="${samedir}/JavaDocSup.xml"/>
</module>
</module>
When I run the checkstyle on my code, it still detects the duplicate lines in the Java doc.
Is the suppress checks="JavadocStyleCheck” is correct?
Any help will be highly appreciated. Thanks.
Checkstyle's StrictDuplicateCode is found to be inefficient, hence it's thrown away (since Checkstyle 6.2). Try using other tools like PMD's CPD, etc.
If you want to stick to current Checkstyle version, you can try adding <property name="min" value="20"/> to throw the violation only if at least 20 lines are matching.
(Some clone detection tools are mentioned under Tools section in Wikipedia)
I have an annotation like:
#ComponentScan(
basePackages = {
"com.example.foo",
"com.example.bar"
} // <--- false positive reported in this line
)
public class FooBar extends WebMvcConfigurerAdapter {
...
}
And a Checkstyle configuration of:
<module name="AnnotationUseStyle" />
<module name="Indentation">
<property name="basicOffset" value="2" />
<property name="braceAdjustment" value="0" />
<property name="caseIndent" value="2" />
</module>
When I run my project through Checkstyle, I get an error stating "assign child at indentation level 2 not at correct indentation, 4". This is referencing line 5 of my code example above, i.e. the closing parenthetical for the basePackages property.
What configuration change to Checkstyle would I need to make for this annotation to validate correctly?
It's a known issue in checkstyle: github.com/checkstyle/checkstyle/issues/553
As a workaround you can set lineWrappingIndentation property to zero:
<module name="Indentation">
<property name="lineWrappingIndentation" value="0"/>
</module>
But in this case you will also need to remove extra indentations after line breaks, e.g.
return getCalculator().
calculate(...);
instead of
return getCalculator().
calculate(...);