Code Analysis - Finding Duplicate Code

java No Comments »

I tried Maven plugins for code analysis and finding duplicate/similar code.

CPD-PMD’s Copy/Paste Detector can be used for finding duplicate code, which is included in PMD, a Java code analysis tool.
run mvn pmd:cpd ,
The report is displayed at cpd.html file, and the results are very efficient.

Simian - Similarity Analyser is also an alternative for finding duplicates.

Code Analysis using PMD and Checkstyle

java No Comments »

We started code review using Crucible, so i searched for some code analysis maven plugins. PMD is a Java code analysis tool and used to find potential problems like unused code, duplicate code, unused variables… PMD is useful and easy:

pom.xml:

    <reporting>
        <outputDirectory>target/reporting/pmd</outputDirectory>
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-pmd-plugin</artifactId>
            </plugin>
        </plugins>
    </reporting>

If you get the following error, simply add target-jdk configuration:
Caused by: net.sourceforge.pmd.ast.ParseException: Can’t use generics unless running in JDK 1.5 mode!

    <reporting>
        <outputDirectory>target/reporting/pmd</outputDirectory>
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-pmd-plugin</artifactId>
              <configuration>
                  <targetJdk>1.5</targetJdk>
              </configuration>
            </plugin>
        </plugins>
    </reporting>

The PMD report is at pmd.html, but i couldn’t find categories, and more detailed reports.

We can also customize PMD rules & rulesets:

How to write a PMD rule?
How to make a new rule set?

CheckStyle is also a strong Maven 2 plugin for java code analysis. Simply running mvn checkstyle:checkstyle gives us detailed html/rss report(mvn site gives heap or memory error for some projects).

    <reporting>
    ……
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-checkstyle-plugin</artifactId>
            </plugin>
        </plugins>
    </reporting>

We can customize the reports generated by Maven’s site plugin. Simply add or overwrite <reports> tag at project.xml. The result gives us reports of project like dependencies, project info, project reports (CMD and PMD reports included), source repository, …

<reports>
    <report>index</report>
    <report>dependencies</report>
    <report>project-team</report>
    <report>mailing-list</report>
    <report>cim</report>
    <report>issue-tracking</report>
    <report>license</report>
    <report>scm</report>
    <report>maven-pmd-plugin</report>
    <report>maven-checkstyle-plugin</report>
</reports>

Format and Color Code Snippets For Blog

java No Comments »

I need formatting java code snippets at my blogs so i use an eclipse plugin, simply you can copy html code to editor.

java2html Eclipse Plugin

I think the most practical way to format html and xml code snippets is using:
http://www.manoli.net/csharpformat

Thanks to Truong Hong Thi for sharing :)

Edit: I also found a brilliant wordpress plugin for formatting source code.
SyntaxHighlighter

Easy to use, just add source language=’java’ tag :)


public class FileComponent implements Serializable{
private String fileName;
private String contentType;
private int size;
private String localPath;

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

}