This is a walkmod plugin to remove unused declarations (imports, variables and private fields, methods and types) in Java source files. It applies the clean procedure in cascade. For example, if a unused method uses some types that are just used in this method, the imports related to these types are also removed.
Let’s see an example. Take a look to this code. There is a private method called hello
that is never referenced and
also a variable in the goodBye
method that also is never used.
package example;
import java.util.*;
public class Foo{
private void hello(File file){
...
}
public void goodBye(){
String s="bye";
System.out.println("bye");
}
}
What this plugins generates is the following modified code:
package example;
public class Foo{
public void goodBye(){
System.out.println("bye");
}
}
Check that your walkmod version is at least 2.0.
-
Add the walkmod-maven-plugin into your
walkmod.xml
as a configuration provider. This plugin will interpret your classpath accoding yourpom.xml
and will build your project to make the application classpath available for walkmod. -
Add the transformation
dead-code-cleaner
into yourwalkmod.xml
.
<!DOCTYPE walkmod PUBLIC "-//WALKMOD//DTD" "http://www.walkmod.com/dtd/walkmod-1.1.dtd">
<walkmod>
<conf-providers>
<conf-provider type="maven"/>
</conf-providers>
<chain name="default">
<transformation type="dead-code-cleaner"/>
</chain>
</walkmod>
You can apply walkmod-dead-code-cleaner-plugin via walkmod.
$ walkmod apply
Or, you can also check which would be the modified classes typing:
$ walkmod check
Optionally, the plugin allows to select which elements are candidates to remove: private types, private methods, private fields, variables or imports:
<transformation type="dead-code-cleaner">
<param name="removeUnusedImports">true</param>
<param name="removeUnusedVariables">true</param>
<param name="removeUnusedClasses">true</param>
<param name="removeUnusedInterfaces">true</param>
<param name="removeUnusedAnnotationTypes">true</param>
<param name="removeUnusedEnumerations">true</param>
<param name="removeUnusedMethods">true</param>
<param name="removeUnusedFields">true</param>
</transformation>