From d69bb1fc49ffcfe3b8f799c261a5d41adc8bafdd Mon Sep 17 00:00:00 2001 From: Ville Oikarinen Date: Mon, 19 Feb 2024 15:52:34 +0200 Subject: [PATCH] findbugs exclude filter support --- .../plugin/findbugs/FindbugsDistribution.java | 3 + .../iwant/plugin/findbugs/FindbugsReport.java | 29 ++++++++-- .../plugin/findbugs/FindbugsReportTest.java | 57 +++++++++++++------ 3 files changed, 69 insertions(+), 20 deletions(-) diff --git a/optional/iwant-plugin-findbugs/src/main/java/org/fluentjava/iwant/plugin/findbugs/FindbugsDistribution.java b/optional/iwant-plugin-findbugs/src/main/java/org/fluentjava/iwant/plugin/findbugs/FindbugsDistribution.java index 1ed351b7..a12fd9bb 100644 --- a/optional/iwant-plugin-findbugs/src/main/java/org/fluentjava/iwant/plugin/findbugs/FindbugsDistribution.java +++ b/optional/iwant-plugin-findbugs/src/main/java/org/fluentjava/iwant/plugin/findbugs/FindbugsDistribution.java @@ -15,6 +15,9 @@ public class FindbugsDistribution extends TargetBase { public static FindbugsDistribution _4_7_3 = new FindbugsDistribution( "4.7.3", "9739f70965c4b89a365419af9c688934"); + public static FindbugsDistribution _4_8_3 = new FindbugsDistribution( + "4.8.3", "dae83e21c1c3014ea177c882bb72c1e9"); + public FindbugsDistribution(String version, String md5sum) { super("spotbugs-" + version); this.version = version; diff --git a/optional/iwant-plugin-findbugs/src/main/java/org/fluentjava/iwant/plugin/findbugs/FindbugsReport.java b/optional/iwant-plugin-findbugs/src/main/java/org/fluentjava/iwant/plugin/findbugs/FindbugsReport.java index 8921054e..62902140 100644 --- a/optional/iwant-plugin-findbugs/src/main/java/org/fluentjava/iwant/plugin/findbugs/FindbugsReport.java +++ b/optional/iwant-plugin-findbugs/src/main/java/org/fluentjava/iwant/plugin/findbugs/FindbugsReport.java @@ -26,11 +26,12 @@ public class FindbugsReport extends TargetBase { private final Path antJar; private final Path antLauncherJar; private final FindbugsOutputFormat outputFormat; + private final Path excludeFile; public FindbugsReport(String name, List classesToAnalyze, List auxClasses, FindbugsDistribution findbugs, Path antJar, Path antLauncherJar, - FindbugsOutputFormat outputFormat) { + FindbugsOutputFormat outputFormat, Path excludeFile) { super(name); this.classesToAnalyze = classesToAnalyze; this.auxClasses = auxClasses; @@ -42,6 +43,7 @@ public FindbugsReport(String name, this.findbugs = findbugs; this.antJar = antJar; this.antLauncherJar = antLauncherJar; + this.excludeFile = excludeFile; } public static FindbugsReportSpex with() { @@ -57,10 +59,12 @@ public static class FindbugsReportSpex { private Path antJar; private Path antLauncherJar; private FindbugsOutputFormat outputFormat = FindbugsOutputFormat.HTML; + private Path excludeFile; public FindbugsReport end() { return new FindbugsReport(name, classesToAnalyze, auxClasses, - findbugs, antJar, antLauncherJar, outputFormat); + findbugs, antJar, antLauncherJar, outputFormat, + excludeFile); } public FindbugsReportSpex name(String name) { @@ -129,6 +133,11 @@ public FindbugsReportSpex outputFormat( return this; } + public FindbugsReportSpex exclude(Path excludeFile) { + this.excludeFile = excludeFile; + return this; + } + } @Override @@ -148,6 +157,9 @@ protected IngredientsAndParametersDefined ingredientsAndParameters( } iUse.ingredients("auxClasses", auxClasses); + if (excludeFile != null) { + iUse.ingredients("excludeFile", excludeFile); + } iUse.parameter("output-format", outputFormat); return iUse.nothingElse(); } @@ -172,6 +184,11 @@ private String antScript(TargetEvaluationContext ctx) { .pathWithoutBackslashes(findbugs.homeDirectory(ctx)); String destString = ctx.iwant() .pathWithoutBackslashes(ctx.cached(this)); + String excludeFilePath = null; + if (excludeFile != null) { + excludeFilePath = ctx.iwant() + .pathWithoutBackslashes(ctx.cached(excludeFile)); + } StringBuilder xml = new StringBuilder(); xml.append( @@ -203,8 +220,12 @@ private String antScript(TargetEvaluationContext ctx) { xml.append(" \n"); xml.append(" \n"); - xml.append("\n"); + + "." + outputFormat + "\"\n"); + if (excludeFilePath != null) { + xml.append(" excludeFilter=\"").append(excludeFilePath) + .append("\""); + } + xml.append(">\n"); for (JavaClassesAndSources cs : classesToAnalyze) { xml.append(" "; + + // all rules in use: + + FindbugsReport fullReport = FindbugsReport.with() + .name("oneclass-report") .using(distroToTest(), antJar(), antLauncherJar()) .classesToAnalyze(new JavaClassesAndSources(classes, src)) .end(); - report.path(ctx); + fullReport.path(ctx); + assertTrue(htmlReportContent(fullReport).contains(npeWarningSnippet)); - String htmlReportContent = htmlReportContent(report); - assertTrue(htmlReportContent - .contains("Null pointer dereference of ? in " - + "org.fluentjava.iwant.plugin.findbugs.testfodder.ClassWithFindbugsIssues." - + "nullReference(Object)")); + // report with excluded NPE rule: + + wsRootHasFile("exclude.xml", ""); + + FindbugsReport filteredReport = FindbugsReport.with() + .name("filtered-oneclass-report") + .using(distroToTest(), antJar(), antLauncherJar()) + .classesToAnalyze(new JavaClassesAndSources(classes, src)) + .exclude(Source.underWsroot("exclude.xml")).end(); + filteredReport.path(ctx); + assertFalse( + htmlReportContent(filteredReport).contains(npeWarningSnippet)); } public void testReportDoesNotDetectProblemIfDependencyNotInAuxclasses()