<< Static code analysis report using PMD | ^ Site index ^ | ^ Tutorial index ^ | Defining a web archive (war) >>

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

Static code analysis report using findbugs

First we add the iwant findbugs 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 net.sf.iwant.api.javamodules.JavaSrcModule;
import net.sf.iwant.api.wsdef.IwantWorkspaceProvider;
import net.sf.iwant.api.wsdef.WorkspaceDefinitionContext;
public class IwantTutorialWorkspaceProvider implements IwantWorkspaceProvider {
@Override
public JavaSrcModule workspaceModule(WorkspaceDefinitionContext ctx) {
return JavaSrcModule.with().name("iwant-tutorial-wsdef")
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().findbugs().withDependencies())
.end();
}
@Override
public String workspaceClassname() {
return "com.example.wsdef.IwanttutorialWorkspace";
}
}

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~ net.sf.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdefdef-main-classes)
(0/1 D! net.sf.iwant.core.download.SvnExported iwant-plugin-findbugs-main-java)
svn exporting (may take a while) https://svn.code.sf.net/p/iwant/code/trunk/optional/iwant-plugin-findbugs/src/main/java@796
(0/1 D! net.sf.iwant.core.download.Downloaded commons-io-1.3.2.jar)
(0/1 D! net.sf.iwant.core.download.SvnExported iwant-plugin-ant-main-java)
svn exporting (may take a while) https://svn.code.sf.net/p/iwant/code/trunk/optional/iwant-plugin-ant/src/main/java@796
(0/1 D! net.sf.iwant.core.download.Downloaded ant-1.9.4.jar)
(0/1 D! net.sf.iwant.core.download.Downloaded ant-launcher-1.9.4.jar)
(0/1 D! net.sf.iwant.api.javamodules.JavaClasses iwant-plugin-ant)
(0/1 D! net.sf.iwant.api.javamodules.JavaClasses iwant-plugin-findbugs)
(0/1 D~ net.sf.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(0/1 D! net.sf.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 findbugs on, and define a findbugs 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 net.sf.iwant.api.core.HelloTarget;
import net.sf.iwant.api.javamodules.JavaClassesAndSources;
import net.sf.iwant.api.javamodules.JavaSrcModule;
import net.sf.iwant.api.model.SideEffect;
import net.sf.iwant.api.model.Target;
import net.sf.iwant.api.wsdef.IwantWorkspace;
import net.sf.iwant.api.wsdef.SideEffectDefinitionContext;
import net.sf.iwant.core.download.TestedIwantDependencies;
import net.sf.iwant.eclipsesettings.EclipseSettings;
import net.sf.iwant.plugin.findbugs.FindbugsDistribution;
import net.sf.iwant.plugin.findbugs.FindbugsReport;
public class IwanttutorialWorkspace implements IwantWorkspace {
@Override
public List<? extends Target> targets() {
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"));
return Arrays.asList(new HelloTarget("hello", "hello from iwant\n"),
mainJavaFindbugsReportOf(findbugsfodder()));
}
@Override
public List<? extends SideEffect> sideEffects(
SideEffectDefinitionContext ctx) {
return Arrays
.asList(EclipseSettings
.with()
.name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(),
ctx.wsdefJavaModule()).end());
return Arrays.asList(EclipseSettings.with().name("eclipse-settings")
.modules(ctx.wsdefdefJavaModule(), ctx.wsdefJavaModule())
.modules(findbugsfodder()).end());
}
private static Target mainJavaFindbugsReportOf(JavaSrcModule mod) {
return FindbugsReport
.with()
.name(mod.name() + "-main-java-findbugs-report")
.using(FindbugsDistribution.ofVersion("2.0.2"),
TestedIwantDependencies.antJar(),
TestedIwantDependencies.antLauncherJar())
.classesToAnalyze(
new JavaClassesAndSources(mod.mainArtifact(), mod
.mainJavasAsPaths())).end();
}
private static JavaSrcModule findbugsfodder() {
return JavaSrcModule.with().name("example-findbugsfodder")
.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~ net.sf.iwant.api.javamodules.JavaClasses iwant-tutorial-wsdef-main-classes)
(example-findbugsfodder)
( .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 a findbugs issues in it.

~/iwant-tutorial $ mkdir -p example-findbugsfodder/src/com/example/findbugsfodder
~/iwant-tutorial $ $EDITOR "example-findbugsfodder/src/com/example/findbugsfodder/FindbugsFodder.java"
package com.example.findbugsfodder;
public class FindbugsFodder {
@SuppressWarnings("null")
public void nullReference(Object o) {
if (o == null) {
System.out.println(o.toString());
}
}
}

Now we can analyze the code.

~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/list-of/targets
hello
example-findbugsfodder-main-java-findbugs-report
Output asserted
~/iwant-tutorial $ as-iwant-tutorial-developer/with/bash/iwant/target/example-findbugsfodder-main-java-findbugs-report/as-path | xargs -I x find x -type f
(0/1 D! net.sf.iwant.core.download.Downloaded findbugs-2.0.2.tar.gz)
(0/1 D! net.sf.iwant.plugin.findbugs.FindbugsDistribution findbugs-2.0.2)
Expanding: /home/hacker/.net.sf.iwant/cached/UnmodifiableUrl/http%3A/%2Fdownloads.sourceforge.net/project/findbugs/findbugs/2.0.2/findbugs-2.0.2.tar.gz into /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/findbugs-2.0.2
(0/1 D! net.sf.iwant.api.javamodules.JavaClasses example-findbugsfodder-main-classes)
(0/1 D! net.sf.iwant.plugin.findbugs.FindbugsReport example-findbugsfodder-main-java-findbugs-report)
Buildfile: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-findbugsfodder-main-java-findbugs-report/build.xml

findbugs-home:

findbugs-task-classpath:

findbugs-report:
[mkdir] Created dir: /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-findbugsfodder-main-java-findbugs-report/findbugs-report
[findbugs] Executing findbugs from ant task
[findbugs] Running FindBugs...
[findbugs] Warnings generated: 2
[findbugs] Calculating exit code...
[findbugs] Setting 'bugs found' flag (1)
[findbugs] Exit code set to: 1
[findbugs] Java Result: 1
[findbugs] Output saved to /home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-findbugsfodder-main-java-findbugs-report/findbugs-report/example-findbugsfodder-main-java-findbugs-report.html

BUILD SUCCESSFUL
Total time: 27 seconds
/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-findbugsfodder-main-java-findbugs-report/build.xml
/home/hacker/iwant-tutorial/as-iwant-tutorial-developer/.i-cached/target/example-findbugsfodder-main-java-findbugs-report/findbugs-report/example-findbugsfodder-main-java-findbugs-report.html
~/iwant-tutorial $ grep 'Null pointer dereference.*FindbugsFodder' as-iwant-tutorial-developer/.i-cached/target/example-findbugsfodder-main-java-findbugs-report/findbugs-report/example-findbugsfodder-main-java-findbugs-report.html
<td>Null pointer dereference of o in com.example.findbugsfodder.FindbugsFodder.nullReference(Object)</td>
Output asserted
<< Static code analysis report using PMD | ^ Site index ^ | ^ Tutorial index ^ | Defining a web archive (war) >>