This chapter continues from where Creating the workspace definition left us.

Static code analysis report using PMD

First we add the iwant pmd plugin to the project.

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdefdef/src/main/java/com/example/wsdefdef/IwantTutorialWorkspaceProvider.java"
package com.example.wsdefdef;
import org.fluentjava.iwant.api.javamodules.JavaSrcModule;
import org.fluentjava.iwant.api.wsdef.WorkspaceModuleContext;
import org.fluentjava.iwant.api.wsdef.WorkspaceModuleProvider;
public class IwantTutorialWorkspaceProvider implements WorkspaceModuleProvider {
@Override
public JavaSrcModule workspaceModule(WorkspaceModuleContext ctx) {
return JavaSrcModule.with().name("iwant-tutorial-wsdef")
.locationUnderWsRoot("as-iwant-tutorial-developer/i-have/wsdef")
.mainJava("src/main/java").mainDeps(ctx.iwantApiModules())
.mainDeps(ctx.wsdefdefModule()).end();
.mainDeps(ctx.wsdefdefModule())
.mainDeps(ctx.iwantPlugin().pmd().withDependencies()).end();
}
@Override
public String workspaceFactoryClassname() {
return "com.example.wsdef.IwanttutorialWorkspaceFactory";
}
}

We refresh eclipse settings so we can use the plugin in our workspace definition.

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/side-effect/eclipse-settings/effective
(0/1 S~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdefdef-main-classes)
(0/1 D! org.fluentjava.iwant.core.download.GnvArtifact ant-1.10.1.jar)
(0/1 D! org.fluentjava.iwant.core.download.GnvArtifact asm-3.2.jar)
(0/1 D! org.fluentjava.iwant.core.download.GnvArtifact commons-io-1.3.2.jar)
(0/1 D! org.fluentjava.iwant.core.download.GnvArtifact jaxen-1.1.4.jar)
(0/1 D! org.fluentjava.iwant.core.download.GnvArtifact pmd-4.3.jar)
(0/1 D! org.fluentjava.iwant.api.javamodules.JavaClasses iwant-plugin-pmd)
(0/1 D~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(0/1 D! org.fluentjava.iwant.api.core.Concatenated eclipse-settings.bin-refs)
(as-iwant-tutorial-developer/i-have/wsdef)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(as-iwant-tutorial-developer/i-have/wsdefdef)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)

After refreshing eclipse we define a module to test PMD on, and define a PMD report target for its main java.

~/iwant-tutorial $ $EDITOR "as-iwant-tutorial-developer/i-have/wsdef/src/main/java/com/example/wsdef/IwanttutorialWorkspace.java"
package com.example.wsdef;
import java.util.Arrays;
import java.util.List;
import org.fluentjava.iwant.api.core.HelloTarget;
import org.fluentjava.iwant.api.javamodules.JavaSrcModule;
import org.fluentjava.iwant.api.model.SideEffect;
import org.fluentjava.iwant.api.model.Target;
import org.fluentjava.iwant.api.wsdef.SideEffectDefinitionContext;
import org.fluentjava.iwant.api.wsdef.TargetDefinitionContext;
import org.fluentjava.iwant.api.wsdef.Workspace;
import org.fluentjava.iwant.eclipsesettings.EclipseSettings;
import org.fluentjava.iwant.plugin.pmd.PmdReport;
public class IwanttutorialWorkspace implements Workspace {
@Override
public List<? extends Target> targets(TargetDefinitionContext ctx) {
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"));
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"),
mainJavaPmdReportOf(pmdfodder()));
}
@Override
public List<? extends SideEffect> sideEffects(
SideEffectDefinitionContext ctx) {
return Arrays.asList(EclipseSettings.with().name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(), ctx.wsdefJavaModule())
.end());
.modules(pmdfodder()).end());
}
private static Target mainJavaPmdReportOf(JavaSrcModule mod) {
return PmdReport.with().name(mod.name() + "-main-java-pmd-report")
.from(mod.mainJavasAsPaths()).end();
}
private static JavaSrcModule pmdfodder() {
return JavaSrcModule.with().name("example-pmdfodder").mainJava("src")
.end();
}
}

Another eclipse settings refresh to create the new eclipse project.

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/side-effect/eclipse-settings/effective
(0/1 S~ org.fluentjava.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(example-pmdfodder)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(as-iwant-tutorial-developer/i-have/wsdef)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)
(as-iwant-tutorial-developer/i-have/wsdefdef)
( .project)
( .classpath)
( .settings/org.eclipse.jdt.core.prefs)
( .settings/org.eclipse.jdt.ui.prefs)

After importing the new project into eclipse we create some code with an PMD issue in it.

~/iwant-tutorial $ mkdir -p example-pmdfodder/src/com/example/pmdfodder
~/iwant-tutorial $ $EDITOR "example-pmdfodder/src/com/example/pmdfodder/PmdFodder.java"
package com.example.pmdfodder;
public class PmdFodder {
public void DiscouragedMethodName() {
// PMD shall complain about this method name
}
}

Now we can analyze the code.

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/list-of/targets
hello
example-pmdfodder-main-java-pmd-report
Output asserted
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/example-pmdfodder-main-java-pmd-report/as-path | xargs -r ls
(0/1 D! org.fluentjava.iwant.plugin.pmd.PmdReport example-pmdfodder-main-java-pmd-report)
Running PMD on [example-pmdfodder/src]
example-pmdfodder-main-java-pmd-report.html
example-pmdfodder-main-java-pmd-report.txt
example-pmdfodder-main-java-pmd-report.xml
ruleset.xml
~/iwant-tutorial $ cat as-iwant-tutorial-developer/.i-cached/target/example-pmdfodder-main-java-pmd-report/example-pmdfodder-main-java-pmd-report.txt

com/example/pmdfodder/PmdFodder.java:5 Method names should not start with capital letters
Output asserted
~/iwant-tutorial $ cat as-iwant-tutorial-developer/.i-cached/target/example-pmdfodder-main-java-pmd-report/example-pmdfodder-main-java-pmd-report.xml | wc -l
8
Output asserted