Reputation: 349
I am using PMD
tool to find out Java code violations. I have installed an eclipse
plug in & it is working fine. But, having lot of similar violations & resolving each separately is tedious job.
Is there any way to write some program which access specific & similar types of rules and resolve them at one go?
Upvotes: 1
Views: 16441
Reputation: 2798
I did not find any of the "quick fix" buttons in VsCode, IntelliJ, nor Eclipse with PMD. The remove unused imports is already done for me by other linters, but was not able to also auto-fix all these PMD rules However, Cleanthat within Spotless within Maven was able to resolve/fix those PMD rules automatically for me.
I found that adding the maven
configuration settings below (for <properties>
and for <build>
) in my pom.xml
made it work.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>21</java.version>
<spotless.version>2.36.0</spotless.version> <!-- Had to manually downgrade, 2.9.0 does not work. -->
</properties>
<!-- Build Settings -->
<build>
<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless.version}</version>
<configuration>
<java>
<!-- Cleanthat will refactor your code, but it may break your style: apply it before your formatter -->
<cleanthat>
<mutators>
<mutator>SafeAndConsensual</mutator> <!-- This did nothing for me. -->
<!-- Manually including checks (mutators), does make cleanthat in spotless run those checks. -->
<mutator>LiteralsFirstInComparisons</mutator>
</mutators>
</cleanthat>
<removeUnusedImports>
<engine>cleanthat-javaparser-unnecessaryimport</engine>
</removeUnusedImports>
</java>
</configuration>
<executions>
<execution>
<goals>
<goal>apply</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I could spotless (which runs cleanthat) this with:
mvn spotless:check
And I could apply the PMD-auto fixes with:
mvn spotless:apply
Both (check
and apply
) complain that they cannot change the LiteralsFirstInComparisons
issues:
[INFO] Turning `something.something().equals(another.another)` into `another.another.equals(something.something)`
[WARNING] We failed turning `something.something().equals(another.another)` into `another.another.equals(something.something)
but when I use the apply, it does change/resolve them, and when I run it again, it does not detect those errors anymore (because they are resolved).
I did have to install anything manually on the system. Hope this helps someone!
Upvotes: 1
Reputation: 545
If you use eclipse-pmd, an alternative to the official PMD plug-in, you can fix many of the automatically resolvable problems at once.
To do this you have to open the Problems View
, select one of the PMD problems and use the Quick Fix
entry from the context menu. This opens the Quick Fix Dialog
where you can click on the button Select All
to select all occurences of the PMD problem and finally click Finish
to resolve all those PMD problems.
However, eclipse-pmd cannot fix all problems automatically. For those problems which it cannot fix, you can use Save Actions
(see Aditya's answer) or NetBean's custom refactorings.
With NetBean's custom refactorings you can define your own code refactorings and apply them everywhere in your code base.
Disclaimer: I am the creator of eclipse-pmd.
Upvotes: 0
Reputation: 17474
Adding to Aditya's excellent answer that already mentions save actions:
The PMD plugins for Eclipse offer Eclipse Quick Fixes for many PMD warnings. PMD-Eclipse even features a "Quick Fix All" feature, which may be just what you are looking for.
Upvotes: -1
Reputation: 1344
There are many violations which are identified by PMD. some of these could be
Remove Unncessary imports, unused variables etc.
So these can be removed by applying Save Actions
in eclipse.
To apply Save Actions in eclipse:
go to Window->Preferences-> Java -> Editor -> Save Actions
Configure them according to you.
For more info.
Upvotes: 3