Skip to content

walkmod/walkmod-dead-code-cleaner-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

walkmod-dead-code-cleaner-plugin

Build Status

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.

Example

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");
  }
}

Usage

Check that your walkmod version is at least 2.0.

  1. Add the walkmod-maven-plugin into your walkmod.xml as a configuration provider. This plugin will interpret your classpath accoding your pom.xml and will build your project to make the application classpath available for walkmod.

  2. Add the transformation dead-code-cleaner into your walkmod.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>

Contributing

If you want to hack on this, fork it, improve it and send me a pull request.

To get started using it, just clone it and call mvn install.

About

Walkmod plugin to remove unused variables, methods and fields

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages