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)
Related
When trying to add maven-checkstyle-plugin to my Java project, I'm facing some weird issues.
The checkstyle version is 3.1.0, that uses checkstyle version 8.19. Below is the checkstyle.xml the project is using:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="TreeWalker">
<module name="LineLength">
<property name="max" value="120"/>
</module>
</module>
and my the configuration on the pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugin>
Now, when I run mvn checkstyle:check the following message is displayed:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-checkstyle-plugin:3.1.0:check
(default-cli) on project myproject: Failed during
checkstyle configuration: LineLength is not allowed as a child in
Checker -> [Help 1]
If I update the checkstyle to make it look like this
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="LineLength">
<property name="max" value="120"/>
</module>
I get the following message
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-checkstyle-plugin:3.1.0:check
(default-cli) on project myproject: Failed during
checkstyle configuration: Property 'max' does not exist, please check
the documentation -> [Help 1]
I cannot reason what's happening since, to me, the error messages are misleading. What am I missing on this configuration?
Actually, LineLength should not be any more under TreeWalker but directly under Checker in the newer versions.
See here:
https://github.com/checkstyle/eclipse-cs/issues/190
The structure that matches the 1.3 DTD for your example is as follows (namely, you seem to be missing the Checker module).
<module name="Checker">
<module name="TreeWalker">
<module name="LineLength">
<property name="max" value="120"/>
</module>
</module>
</module>
I was receiving this error in Visual Studio Code and I needed to make sure that my checkstyle.xml version matched the checkstyle version that Visual Studio Code was using.
I went to Extensions->CheckStyle->Settings->Version and set it to match the appropriate version. I just set the version to match the version defined in my build.gradle.kts file.
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
i have installed wildfly 8.1 and because i have already a project configured to use EclipseLink, i have tried to configure wildfly to use it.
However, it always gives the same error :
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:166) [wildfly-server-8.1.0.Final.jar:8.1.0.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_25]
at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]
Caused by: javax.persistence.PersistenceException: JBAS011466: PersistenceProvider '
org.eclipse.persistence.jpa.PersistenceProvider
' not found
at org.jboss.as.jpa.processor.PersistenceUnitServiceHandler.lookupProvider(PersistenceUnitServiceHandler.java:990)
at org.jboss.as.jpa.processor.PersistenceUnitServiceHandler.addPuService(PersistenceUnitServiceHandler.java:258)
at org.jboss.as.jpa.processor.PersistenceUnitServiceHandler.handleWarDeployment(PersistenceUnitServiceHandler.java:191)
at org.jboss.as.jpa.processor.PersistenceUnitServiceHandler.deploy(PersistenceUnitServiceHandler.java:126)
at org.jboss.as.jpa.processor.PersistenceBeginInstallProcessor.deploy(PersistenceBeginInstallProcessor.java:52)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:159) [wildfly-server-8.1.0.Final.jar:8.1.0.Final]
... 5 more
I have followed the instructions of the official documentation, but nothing changed. I have added the eclipseLink's jar to "modules\system\layers\base\org\eclipse\persistence\main"
and the module.xml :
<module xmlns="urn:jboss:module:1.3" name="org.eclipse.persistence">
<resources>
<resource-root path="jipijapa-eclipselink-1.0.1.Final.jar"/>
<resource-root path="eclipselink.jar"/>
</resources>
<dependencies>
<module name="asm.asm"/>
<module name="javax.api"/>
<module name="javax.annotation.api"/>
<module name="javax.enterprise.api"/>
<module name="javax.persistence.api"/>
<module name="javax.transaction.api"/>
<module name="javax.validation.api"/>
<module name="javax.xml.bind.api"/>
<module name="org.antlr"/>
<module name="org.apache.commons.collections"/>
<module name="org.dom4j"/>
<module name="org.javassist"/>
<module name="org.jboss.as.jpa.spi"/>
<module name="org.jboss.logging"/>
<module name="org.jboss.vfs"/>
</dependencies>
</module>
Do you know what is the problem ?
Tks
Edit :
My persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="AppPu">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/H2Ds</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>
New lines in provider section of persistence.xml won't work (how could it help?).
Wildfly hasn't got provided eclipseLink implementation in libs. To fix this follow this steps:
Download eclipselink.jar (or copy from your maven repo)
Copy it to
destination :
...Wildfly\modules\system\layers\base\org\eclipse\persistence\main
Edit module.xml (same path). Add section
<resource-root path="eclipselink.jar"><filter><exclude path="javax/**" /></filter></resource-root>
After server restart everything should work.
Finally I solved the problem!
in fact, I have a composite unit, all persistence.xml are correct, but the persistence.xml which declared the composite unit was a bad statement of the provider:
<provider>
org.eclipse.persistence.jpa.PersistenceProvider
</provider>
replaced by :
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
and now work fine.
You need to add provider to persistence-unit in your persistence.xml:
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
The official documentaiton on how to configure the eclipselin module, and the required system property to active the vfs acrhieve factory impl are both given here:
https://docs.jboss.org/author/display/WFLY10/JPA+Reference+Guide#JPAReferenceGuide-UsingEclipseLink
For the module do something as:
<module xmlns="urn:jboss:module:1.1" name="org.eclipse.persistence">
<resources>
<resource-root path="jipijapa-eclipselink-10.0.0.Final.jar"/>
<resource-root path="eclipselink.jar">
<filter>
<exclude path="javax/**" />
</filter>
</resource-root>
</resources>
<dependencies>
<module name="asm.asm"/>
<module name="javax.api"/>
<module name="javax.annotation.api"/>
<module name="javax.enterprise.api"/>
<module name="javax.persistence.api"/>
<module name="javax.transaction.api"/>
<module name="javax.validation.api"/>
<module name="javax.xml.bind.api"/>
<module name="javax.ws.rs.api"/>
<module name="org.antlr"/>
<module name="org.apache.commons.collections"/>
<module name="org.dom4j"/>
<module name="org.jboss.as.jpa.spi"/>
<module name="org.jboss.logging"/>
<module name="org.jboss.vfs"/>
</dependencies>
</module>
For the system property do something as:
<system-properties>
...
<property name="eclipselink.archive.factory" value="org.jipijapa.eclipselink.JBossArchiveFactoryImpl"/>
</system-properties>
Finally, do not start adding to your persistence.xml any container sepcific properties like:
<property name="eclipselink.target-server" value="JBoss" />
This is completely wrong.
Wildfly is already taking care of puting the proper target server platform in its:
org.jipijapa.eclipselink.EclipseLinkPersistenceProviderAdaptor
Here is a sample of code from their class:
#SuppressWarnings({ "rawtypes", "unchecked" })
#Override
public void addProviderProperties(Map properties, PersistenceUnitMetadata pu) {
if (!pu.getProperties().containsKey(ECLIPSELINK_TARGET_SERVER)) {
properties.put(ECLIPSELINK_TARGET_SERVER, WildFlyServerPlatform.class.getName());
properties.put(ECLIPSELINK_ARCHIVE_FACTORY, JBossArchiveFactoryImpl.class.getName());
properties.put(ECLIPSELINK_LOGGING_LOGGER, JBossLogger.class.getName());
}
}
YOu might want to make sure you debug this class, to make sure it gets invoked and makes your dynamic persistence unit properties all proper.
But do not mess around with persistence.xml properties that are specific to the container. Wildfly is doing this properly and they add them themselves.
So just follow their documentation, that is the best advice.
in EAP with a servicepack there is the trap that the org.eclipse.persistence module might actually be in the .overlays/ tree (modules/system/layers/base/.overlays/).
I get the following error when I try to load a user defined check style template.
cannot initialize module TreeWalker - TreeWalker is not allowed as a parent of FileLength
I think it might be due to the incompatibilty with checkstyle version in my eclipse.
How will I be able to find the appropriate checkstyle version with checkstyle xml file
This is an error in the template, independent of the Checkstyle version. FileLength goes directly under Checker, not under TreeWalker.
Example:
<module name="Checker">
<property name="severity" value="warning"/>
<module name="TreeWalker">
<property name="tabWidth" value="4"/>
<module name="JavadocType"/>
<!-- many others -->
</module>
<module name="NewlineAtEndOfFile"/>
<module name="FileLength"/>
<module name="JavadocPackage"/>
<!-- others -->
</module>
Can anybody say why suppression filter doesn't work? It still generates javadoc errors for Candidate_ file.
checkstyle.xml
`...
<module name="SuppressionFilter">
<property name="file" value="C:/sts-projects/staffing4/trunk/config/suppressions.xml" />
</module>
<!-- Checks that a package-info.java file exists for each package. -->
<module name="JavadocPackage" />
...`
suppressions.xml
<suppressions>
<suppress checks="JavadocMethod" files="Candidate_.java" />
</suppressions>
Could it be because checkstyle.xml above references suppressions.xml while the file mentioned below that is suppression.xml?