Skip to content

Commit

Permalink
Google error-prone #10
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jan 14, 2018
1 parent 04be590 commit f2720de
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 3 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
Changelog of Violations lib.

## Unreleased
### GitHub [#10](https://github.com/tomasbjerre/violations-lib/issues/10) Google error-prone

**Google error-prone**


[80b0e9e99d20eee](https://github.com/tomasbjerre/violations-lib/commit/80b0e9e99d20eee) Tomas Bjerre *2018-01-14 11:19:04*


## 1.51
### GitHub [#30](https://github.com/tomasbjerre/violations-lib/pull/30) Parameterize the max line length for the report table.

**Parameterize max width of reporter table**


[0be6456df10d23b](https://github.com/tomasbjerre/violations-lib/commit/0be6456df10d23b) Tomas Bjerre *2018-01-13 19:03:00*
[87c5ab7cb5f4ae7](https://github.com/tomasbjerre/violations-lib/commit/87c5ab7cb5f4ae7) Tomas Bjerre *2018-01-13 19:06:03*


## 1.50
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ It supports:
* [_Gendarme_](http://www.mono-project.com/docs/tools+libraries/tools/gendarme/)
* [_GoLint_](https://github.com/golang/lint)
* [_GoVet_](https://golang.org/cmd/vet/) Same format as GoLint.
* [_GoogleErrorProne_](https://github.com/google/error-prone)
* [_JSHint_](http://jshint.com/)
* _Lint_ A common XML format, used by different linters.
* [_JCReport_](https://github.com/jCoderZ/fawkez/wiki/JcReport)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package se.bjurr.violations.lib.parsers;

import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Parser.GOOGLEERRORPRONE;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

public class GoogleErrorProneParser implements ViolationsParser {
private static Pattern NEW_VIOLATION =
Pattern.compile("^((:[^/]*)|([^:]))([^:]+?):([^:]+?):([^:]+?):[^\\[]*\\[([^\\]]+?)](.*)");

@Override
public List<Violation> parseReportOutput(final String reportContent) throws Exception {
final List<Violation> found = new ArrayList<>();
String currentFilename = null;
Integer currentLine = null;
SEVERITY currentSeverity = null;
String currentRule = null;
String currentRuleMessage = null;
StringBuilder currentMessage = null;
final String[] lines = reportContent.split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
final Matcher matcher = NEW_VIOLATION.matcher(line);
if (matcher.find()) {
currentFilename = matcher.group(4).trim();
currentLine = Integer.parseInt(matcher.group(5));
currentSeverity = toSeverity(matcher.group(6));
currentRule = matcher.group(7).trim();
currentRuleMessage = matcher.group(8).trim();
currentMessage = new StringBuilder();
for (int j = i + 1; j < lines.length; j++) {
line = lines[j];
if (!line.startsWith(" ")) {
found.add(
violationBuilder() //
.setFile(currentFilename) //
.setMessage(currentRuleMessage + "\n\n" + currentMessage.toString()) //
.setParser(GOOGLEERRORPRONE) //
.setRule(currentRule) //
.setSeverity(currentSeverity) //
.setStartLine(currentLine) //
.build());
break;
}
i++;
currentMessage.append(line.trim());
}
}
}
return found;
}

private SEVERITY toSeverity(final String from) {
if (from.trim().equalsIgnoreCase("error")) {
return ERROR;
}
if (from.trim().equalsIgnoreCase("warning")) {
return WARN;
}
return INFO;
}
}
6 changes: 4 additions & 2 deletions src/main/java/se/bjurr/violations/lib/reports/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import se.bjurr.violations.lib.parsers.FxCopParser;
import se.bjurr.violations.lib.parsers.GendarmeParser;
import se.bjurr.violations.lib.parsers.GoLintParser;
import se.bjurr.violations.lib.parsers.GoogleErrorProneParser;
import se.bjurr.violations.lib.parsers.JCReportParser;
import se.bjurr.violations.lib.parsers.JSHintParser;
import se.bjurr.violations.lib.parsers.KlocworkParser;
Expand Down Expand Up @@ -60,6 +61,7 @@ public enum Parser {
KLOCWORK(new KlocworkParser()), //
MYPY(new MyPyParser()), //
GOLINT(new GoLintParser()), //
GOOGLEERRORPRONE(new GoogleErrorProneParser()), //
PERLCRITIC(new PerlCriticParser()), //
PITEST(new PiTestParser()), //
PMD(new PMDParser()), //
Expand All @@ -76,11 +78,11 @@ public enum Parser {
private static Logger LOG = Logger.getLogger(Parser.class.getSimpleName());
private transient ViolationsParser violationsParser;

private Parser(ViolationsParser violationsParser) {
private Parser(final ViolationsParser violationsParser) {
this.violationsParser = violationsParser;
}

public List<Violation> findViolations(List<File> includedFiles) {
public List<Violation> findViolations(final List<File> includedFiles) {
final List<Violation> violations = new ArrayList<>();
for (final File file : includedFiles) {
try {
Expand Down
83 changes: 83 additions & 0 deletions src/test/java/se/bjurr/violations/lib/GoogleErrorProneTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package se.bjurr.violations.lib;

import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsApi.violationsApi;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.reports.Parser.GOOGLEERRORPRONE;

import java.util.List;
import org.junit.Test;
import se.bjurr.violations.lib.model.Violation;

public class GoogleErrorProneTest {

@Test
public void testThatViolationsCanBeParsedGradle() {
final String rootFolder = getRootFolder();

final List<Violation> actual =
violationsApi() //
.withPattern(".*/googleErrorProne/googleErrorProne\\.log$") //
.inFolder(rootFolder) //
.findAll(GOOGLEERRORPRONE) //
.violations();

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

final Violation violation0 = actual.get(0);
assertThat(violation0.getMessage()) //
.endsWith("Splitter.on(\",\").split(link)) {'?");
assertThat(violation0.getFile()) //
.isEqualTo(
"/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubHelper.java");
assertThat(violation0.getSeverity()) //
.isEqualTo(WARN);
assertThat(violation0.getRule().get()) //
.isEqualTo("StringSplitter");
assertThat(violation0.getStartLine()) //
.isEqualTo(51);

final Violation violation4 = actual.get(4);
assertThat(violation4.getMessage()) //
.endsWith(", otherCommitTime);'?");
assertThat(violation4.getFile()) //
.isEqualTo(
"home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/git/TraversalWork.java");
assertThat(violation4.getSeverity()) //
.isEqualTo(WARN);
assertThat(violation4.getRule().get()) //
.isEqualTo("BoxedPrimitiveConstructor");
assertThat(violation4.getStartLine()) //
.isEqualTo(73);
}

@Test
public void testThatViolationsCanBeParsedMaven() {
final String rootFolder = getRootFolder();

final List<Violation> actual =
violationsApi() //
.withPattern(".*/googleErrorProne/googleErrorProneMaven\\.log$") //
.inFolder(rootFolder) //
.findAll(GOOGLEERRORPRONE) //
.violations();

assertThat(actual) //
.hasSize(1);

final Violation violation0 = actual.get(0);
assertThat(violation0.getMessage()) //
.endsWith("row new Exception();'?");
assertThat(violation0.getFile()) //
.isEqualTo("../examples/maven/error_prone_should_flag/src/main/java/Main.java");
assertThat(violation0.getSeverity()) //
.isEqualTo(ERROR);
assertThat(violation0.getRule().get()) //
.isEqualTo("DeadException");
assertThat(violation0.getStartLine()) //
.isEqualTo(20);
}
}
32 changes: 32 additions & 0 deletions src/test/resources/googleErrorProne/googleErrorProne.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
:googleErrorProne UP-TO-DATE
:googleJavaFormat NO-SOURCE
:compileJava/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubHelper.java:51: warning: [StringSplitter] Prefer Splitter to String.split
for (final String part : link.split(",")) {
^
(see http://errorprone.info/bugpattern/StringSplitter)
Did you mean 'for (final String part : Splitter.on(",").split(link)) {'?
/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubHelper.java:52: warning: [StringSplitter] Prefer Splitter to String.split
for (final String piece : part.split(";")) {
^
(see http://errorprone.info/bugpattern/StringSplitter)
Did you mean 'for (final String piece : Splitter.on(";").split(part)) {'?
/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java:24: warning: [StringSplitter] Prefer Splitter to String.split
for (String part : message.split("\n")) {
^
(see http://errorprone.info/bugpattern/StringSplitter)
Did you mean 'for (String part : Splitter.on("\n").split(message)) {'?
/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoData.java:76: warning: [StringSplitter] Prefer Splitter to String.split
.split("[/:\\.]");
^
(see http://errorprone.info/bugpattern/StringSplitter)
Did you mean 'List<String> parts ='?
/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/git/TraversalWork.java:73: warning: [BoxedPrimitiveConstructor] valueOf or autoboxing provides better time and space performance
return new Integer(selfCommitTime) //
^
(see http://errorprone.info/bugpattern/BoxedPrimitiveConstructor)
Did you mean 'return Integer.compare(selfCommitTime, otherCommitTime);'?
5 warnings


BUILD SUCCESSFUL in 5s
1 actionable task: 1 executed
11 changes: 11 additions & 0 deletions src/test/resources/googleErrorProne/googleErrorProneMaven.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
../examples/maven/error_prone_should_flag$ mvn compile
[INFO] Compiling 1 source file to .../examples/maven/error_prone_should_flag/target/classes
.../examples/maven/error_prone_should_flag/src/main/java/Main.java:20: error: [DeadException] Exception created but not thrown
new Exception();
^
(see http://errorprone.info/bugpattern/DeadException)
Did you mean 'throw new Exception();'?
1 error
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

0 comments on commit f2720de

Please sign in to comment.