Skip to content

Commit

Permalink
Only most specific FindBugs line
Browse files Browse the repository at this point in the history
 * Also SLF4J and some debug logging.
  • Loading branch information
tomasbjerre committed Mar 4, 2016
1 parent 186b65f commit b50c549
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 23 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ repositories {

dependencies {
compile 'com.google.guava:guava:11.0.1'
compile 'org.slf4j:slf4j-api:1.7.18'
testCompile 'org.slf4j:slf4j-simple:1.7.18'
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:2.3.0'
}
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/se/bjurr/violations/lib/ViolationsReporterApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import java.io.File;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.reports.Reporter;

public class ViolationsReporterApi {
private static Logger LOG = LoggerFactory.getLogger(ViolationsReporterApi.class);
private String pattern;
private Reporter reporter;
private File startFile;
Expand Down Expand Up @@ -40,6 +44,20 @@ public ViolationsReporterApi inFolder(String folder) {

public List<Violation> violations() {
List<File> includedFiles = findAllReports(startFile, pattern);
return reporter.findViolations(includedFiles);
if (LOG.isDebugEnabled()) {
LOG.debug("Found " + includedFiles.size() + " reports:");
for (File f : includedFiles) {
LOG.debug(f.getAbsolutePath());
}
}
List<Violation> foundViolations = reporter.findViolations(includedFiles);
if (LOG.isDebugEnabled()) {
LOG.debug("Found " + foundViolations.size() + " violations:");
for (Violation v : foundViolations) {
LOG.debug(v.getReporter() + " (" + v.getRule() + ") " + v.getFile() + " " + v.getStartLine() + " -> "
+ v.getEndLine());
}
}
return foundViolations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public List<Violation> parseFile(File file) throws Exception {
SEVERITY severity = toSeverity(rank);

List<String> sourceLineChunks = getChunks(bugInstanceChunk, "<SourceLine", "/>");
List<Violation> candidates = newArrayList();
for (String sourceLineChunk : sourceLineChunks) {
Optional<Integer> startLine = findIntegerAttribute(sourceLineChunk, "start");
Optional<Integer> endLine = findIntegerAttribute(sourceLineChunk, "end");
Expand All @@ -51,7 +52,7 @@ public List<Violation> parseFile(File file) throws Exception {
}
String filename = getAttribute(sourceLineChunk, "sourcepath");
String classname = getAttribute(sourceLineChunk, "classname");
violations.add(//
candidates.add(//
violationBuilder()//
.setReporter(FINDBUGS)//
.setMessage(message)//
Expand All @@ -65,6 +66,13 @@ public List<Violation> parseFile(File file) throws Exception {
.build()//
);
}
if (!candidates.isEmpty()) {
/**
* Last one is the most specific, first 2 may be class and method when the
* third is source line.
*/
violations.add(candidates.get(candidates.size() - 1));
}
}
return violations;
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/se/bjurr/violations/lib/FindbugsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public void before() {
.violations();

assertThat(actual)//
.hasSize(13);
.hasSize(5);
}

@Test
public void testThatEqualsUseHashCodeCanBeParsed() {
Iterable<Violation> equalsUseHashCode = filter(actual, filterRule("HE_EQUALS_USE_HASHCODE"));
assertThat(equalsUseHashCode)//
.hasSize(2);
.hasSize(1);
}

@Test
Expand All @@ -55,7 +55,7 @@ public void testThatViolationsCanBeParsed() {
.startsWith("equals method always returns true")//
.doesNotContain("CDATA");
assertThat(actual.get(0).getStartLine())//
.isEqualTo(3);
.isEqualTo(17);
assertThat(actual.get(0).getEndLine())//
.isEqualTo(17);
assertThat(actual.get(0).getRule().get())//
Expand Down
33 changes: 16 additions & 17 deletions src/test/resources/findbugs/main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<BugCollection version="3.0.1" sequence="0" timestamp="1457030109384" analysisTimestamp="1457030109406" release="">
<BugCollection version="3.0.1" sequence="0" timestamp="1457111362731" analysisTimestamp="1457111362747" release="">
<Project projectName="">
<Jar>/build/classes/main/se/bjurr/violations/lib/example/MyClass.class</Jar>
<Jar>/build/classes/main/se/bjurr/violations/lib/example/OtherClass.class</Jar>
Expand Down Expand Up @@ -54,27 +54,26 @@
</Field>
</BugInstance>
<Errors errors="0" missingClasses="0"></Errors>
<FindBugsSummary timestamp="Thu, 3 Mar 2016 19:35:09 +0100" total_classes="2" referenced_classes="13" total_bugs="5" total_size="26" num_packages="1" java_version="1.8.0_72" vm_version="25.72-b15" cpu_seconds="5.49" clock_seconds="1.24" peak_mbytes="102.97" alloc_mbytes="3552.00" gc_seconds="0.03" priority_2="1" priority_1="4">
<FindBugsSummary timestamp="Fri, 4 Mar 2016 18:09:22 +0100" total_classes="2" referenced_classes="13" total_bugs="5" total_size="26" num_packages="1" java_version="1.8.0_72" vm_version="25.72-b15" cpu_seconds="4.23" clock_seconds="0.94" peak_mbytes="169.99" alloc_mbytes="3552.00" gc_seconds="0.02" priority_2="1" priority_1="4">
<PackageStats package="se.bjurr.violations.lib.example" total_bugs="5" total_types="2" total_size="26" priority_2="1" priority_1="4">
<ClassStats class="se.bjurr.violations.lib.example.MyClass" sourceFile="MyClass.java" interface="false" size="13" bugs="3" priority_1="3"/>
<ClassStats class="se.bjurr.violations.lib.example.OtherClass" sourceFile="OtherClass.java" interface="false" size="13" bugs="2" priority_2="1" priority_1="1"/>
</PackageStats>
<FindBugsProfile>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.ClassInfoAnalysisEngine" totalMilliseconds="211" invocations="329" avgMicrosecondsPerInvocation="641" maxMicrosecondsPerInvocation="19207" standardDeviationMircosecondsPerInvocation="1602"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.FieldItemSummary" totalMilliseconds="68" invocations="13" avgMicrosecondsPerInvocation="5241" maxMicrosecondsPerInvocation="19992" standardDeviationMircosecondsPerInvocation="6232"/>
<ClassProfile name="edu.umd.cs.findbugs.util.TopologicalSort" totalMilliseconds="53" invocations="297" avgMicrosecondsPerInvocation="180" maxMicrosecondsPerInvocation="17700" standardDeviationMircosecondsPerInvocation="1039"/>
<ClassProfile name="edu.umd.cs.findbugs.OpcodeStack$JumpInfoFactory" totalMilliseconds="50" invocations="67" avgMicrosecondsPerInvocation="752" maxMicrosecondsPerInvocation="5131" standardDeviationMircosecondsPerInvocation="917"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.MethodGenFactory" totalMilliseconds="44" invocations="9" avgMicrosecondsPerInvocation="4945" maxMicrosecondsPerInvocation="41857" standardDeviationMircosecondsPerInvocation="13053"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.FindNoSideEffectMethods" totalMilliseconds="43" invocations="13" avgMicrosecondsPerInvocation="3358" maxMicrosecondsPerInvocation="15506" standardDeviationMircosecondsPerInvocation="4287"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.OverridingEqualsNotSymmetrical" totalMilliseconds="32" invocations="13" avgMicrosecondsPerInvocation="2530" maxMicrosecondsPerInvocation="12969" standardDeviationMircosecondsPerInvocation="4066"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.JavaClassAnalysisEngine" totalMilliseconds="26" invocations="28" avgMicrosecondsPerInvocation="941" maxMicrosecondsPerInvocation="12701" standardDeviationMircosecondsPerInvocation="2387"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.NoteDirectlyRelevantTypeQualifiers" totalMilliseconds="26" invocations="13" avgMicrosecondsPerInvocation="2018" maxMicrosecondsPerInvocation="10377" standardDeviationMircosecondsPerInvocation="2759"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.FunctionsThatMightBeMistakenForProcedures" totalMilliseconds="22" invocations="13" avgMicrosecondsPerInvocation="1716" maxMicrosecondsPerInvocation="8580" standardDeviationMircosecondsPerInvocation="2434"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.TypeDataflowFactory" totalMilliseconds="19" invocations="9" avgMicrosecondsPerInvocation="2161" maxMicrosecondsPerInvocation="15566" standardDeviationMircosecondsPerInvocation="4768"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.ValueNumberDataflowFactory" totalMilliseconds="19" invocations="9" avgMicrosecondsPerInvocation="2128" maxMicrosecondsPerInvocation="14427" standardDeviationMircosecondsPerInvocation="4384"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.CFGFactory" totalMilliseconds="18" invocations="9" avgMicrosecondsPerInvocation="2102" maxMicrosecondsPerInvocation="15667" standardDeviationMircosecondsPerInvocation="4806"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.ClassDataAnalysisEngine" totalMilliseconds="17" invocations="330" avgMicrosecondsPerInvocation="53" maxMicrosecondsPerInvocation="813" standardDeviationMircosecondsPerInvocation="84"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.BuildStringPassthruGraph" totalMilliseconds="16" invocations="13" avgMicrosecondsPerInvocation="1304" maxMicrosecondsPerInvocation="6743" standardDeviationMircosecondsPerInvocation="1815"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.ClassInfoAnalysisEngine" totalMilliseconds="174" invocations="329" avgMicrosecondsPerInvocation="530" maxMicrosecondsPerInvocation="13394" standardDeviationMircosecondsPerInvocation="1293"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.FieldItemSummary" totalMilliseconds="49" invocations="13" avgMicrosecondsPerInvocation="3777" maxMicrosecondsPerInvocation="15202" standardDeviationMircosecondsPerInvocation="4551"/>
<ClassProfile name="edu.umd.cs.findbugs.OpcodeStack$JumpInfoFactory" totalMilliseconds="37" invocations="67" avgMicrosecondsPerInvocation="553" maxMicrosecondsPerInvocation="3880" standardDeviationMircosecondsPerInvocation="688"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.FindNoSideEffectMethods" totalMilliseconds="33" invocations="13" avgMicrosecondsPerInvocation="2600" maxMicrosecondsPerInvocation="11695" standardDeviationMircosecondsPerInvocation="3249"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.MethodGenFactory" totalMilliseconds="31" invocations="9" avgMicrosecondsPerInvocation="3541" maxMicrosecondsPerInvocation="30050" standardDeviationMircosecondsPerInvocation="9374"/>
<ClassProfile name="edu.umd.cs.findbugs.util.TopologicalSort" totalMilliseconds="30" invocations="297" avgMicrosecondsPerInvocation="103" maxMicrosecondsPerInvocation="1365" standardDeviationMircosecondsPerInvocation="182"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.JavaClassAnalysisEngine" totalMilliseconds="23" invocations="28" avgMicrosecondsPerInvocation="838" maxMicrosecondsPerInvocation="12712" standardDeviationMircosecondsPerInvocation="2382"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.OverridingEqualsNotSymmetrical" totalMilliseconds="23" invocations="13" avgMicrosecondsPerInvocation="1804" maxMicrosecondsPerInvocation="9191" standardDeviationMircosecondsPerInvocation="2854"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.NoteDirectlyRelevantTypeQualifiers" totalMilliseconds="19" invocations="13" avgMicrosecondsPerInvocation="1477" maxMicrosecondsPerInvocation="7441" standardDeviationMircosecondsPerInvocation="1969"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.FunctionsThatMightBeMistakenForProcedures" totalMilliseconds="16" invocations="13" avgMicrosecondsPerInvocation="1277" maxMicrosecondsPerInvocation="5480" standardDeviationMircosecondsPerInvocation="1672"/>
<ClassProfile name="edu.umd.cs.findbugs.detect.BuildObligationPolicyDatabase" totalMilliseconds="15" invocations="13" avgMicrosecondsPerInvocation="1155" maxMicrosecondsPerInvocation="4487" standardDeviationMircosecondsPerInvocation="1378"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.ValueNumberDataflowFactory" totalMilliseconds="13" invocations="9" avgMicrosecondsPerInvocation="1469" maxMicrosecondsPerInvocation="10038" standardDeviationMircosecondsPerInvocation="3050"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.CFGFactory" totalMilliseconds="12" invocations="9" avgMicrosecondsPerInvocation="1410" maxMicrosecondsPerInvocation="10245" standardDeviationMircosecondsPerInvocation="3137"/>
<ClassProfile name="edu.umd.cs.findbugs.classfile.engine.bcel.TypeDataflowFactory" totalMilliseconds="12" invocations="9" avgMicrosecondsPerInvocation="1385" maxMicrosecondsPerInvocation="9807" standardDeviationMircosecondsPerInvocation="2996"/>
</FindBugsProfile>
</FindBugsSummary>
<ClassFeatures></ClassFeatures>
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/pmd/main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<pmd version="5.2.3" timestamp="2016-03-03T19:35:11.351">
<pmd version="5.2.3" timestamp="2016-03-04T18:09:24.205">
<file name="/src/main/java/se/bjurr/violations/lib/example/MyClass.java">
<violation beginline="9" endline="11" begincolumn="10" endcolumn="3" rule="EmptyIfStmt" ruleset="Empty Code" package="se.bjurr.violations.lib.example" class="MyClass" method="npe" externalInfoUrl="http://pmd.sourceforge.net/pmd-5.2.3/pmd-java/rules/java/empty.html#EmptyIfStmt" priority="3">
Avoid empty if statements
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/simplelogger.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.slf4j.simpleLogger.defaultLogLevel=debug

0 comments on commit b50c549

Please sign in to comment.