Skip to content

Commit

Permalink
Kotlin Maven and Gradle parsers #44
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 20, 2018
1 parent 4c058b8 commit 0f3c4e9
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ It supports:
* _Lint_ A common XML format, used by different linters.
* [_JCReport_](https://github.com/jCoderZ/fawkez/wiki/JcReport)
* [_Klocwork_](http://www.klocwork.com/products-services/klocwork/static-code-analysis)
* [_KotlinMaven_](https://github.com/JetBrains/kotlin) Output from Kotlin Maven Plugin.
* [_KotlinGradle_](https://github.com/JetBrains/kotlin) Output from Kotlin Gradle Plugin.
* [_MyPy_](https://pypi.python.org/pypi/mypy-lang)
* [_PCLint_](http://www.gimpel.com/html/pcl.htm) PC-Lint using the same output format as the Jenkins warnings plugin, [_details here_](https://wiki.jenkins.io/display/JENKINS/PcLint+options)
* [_PerlCritic_](https://github.com/Perl-Critic)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package se.bjurr.violations.lib.parsers;

import static java.lang.Integer.parseInt;
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.KOTLINGRADLE;
import static se.bjurr.violations.lib.util.Utils.isNullOrEmpty;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getLines;

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

public class KotlinGradleParser implements ViolationsParser {
@Override
public List<Violation> parseReportOutput(final String string) throws Exception {
List<Violation> violations = new ArrayList<>();
List<String> lines = getLines(string);
for (String lineString : lines) {
List<List<String>> partsPerLine = new ArrayList<>();
for (List<String> parts : partsPerLine) {
String filename = parts.get(1).trim();
Integer line = parseInt(parts.get(2));
Integer column = parseInt(parts.get(3));
String severity = parts.get(4).trim();
String message = parts.get(5).trim();
String rule = null;
if (!isNullOrEmpty(parts.get(7))) {
rule = parts.get(7).trim();
}
violations.add( //
violationBuilder() //
.setParser(KOTLINGRADLE) //
.setStartLine(line) //
.setColumn(column) //
.setFile(filename) //
.setSeverity(toSeverity(severity)) //
.setMessage(message) //
.setRule(rule) //
.build() //
);
}
}
return violations;
}

public SEVERITY toSeverity(final String severity) {
if (severity.equalsIgnoreCase("error")) {
return ERROR;
}
if (severity.equalsIgnoreCase("warning")) {
return WARN;
}
return INFO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package se.bjurr.violations.lib.parsers;

import static java.lang.Integer.parseInt;
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.KOTLINMAVEN;
import static se.bjurr.violations.lib.util.Utils.isNullOrEmpty;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getLines;

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

public class KotlinMavenParser implements ViolationsParser {
@Override
public List<Violation> parseReportOutput(final String string) throws Exception {
List<Violation> violations = new ArrayList<>();
List<String> lines = getLines(string);
for (String lineString : lines) {
List<List<String>> partsPerLine = new ArrayList<>();
for (List<String> parts : partsPerLine) {
String filename = parts.get(1).trim();
Integer line = parseInt(parts.get(2));
Integer column = parseInt(parts.get(3));
String severity = parts.get(4).trim();
String message = parts.get(5).trim();
String rule = null;
if (!isNullOrEmpty(parts.get(7))) {
rule = parts.get(7).trim();
}
violations.add( //
violationBuilder() //
.setParser(KOTLINMAVEN) //
.setStartLine(line) //
.setColumn(column) //
.setFile(filename) //
.setSeverity(toSeverity(severity)) //
.setMessage(message) //
.setRule(rule) //
.build() //
);
}
}
return violations;
}

public SEVERITY toSeverity(final String severity) {
if (severity.equalsIgnoreCase("error")) {
return ERROR;
}
if (severity.equalsIgnoreCase("warning")) {
return WARN;
}
return INFO;
}
}
4 changes: 4 additions & 0 deletions src/main/java/se/bjurr/violations/lib/reports/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import se.bjurr.violations.lib.parsers.JCReportParser;
import se.bjurr.violations.lib.parsers.JSHintParser;
import se.bjurr.violations.lib.parsers.KlocworkParser;
import se.bjurr.violations.lib.parsers.KotlinGradleParser;
import se.bjurr.violations.lib.parsers.KotlinMavenParser;
import se.bjurr.violations.lib.parsers.LintParser;
import se.bjurr.violations.lib.parsers.MyPyParser;
import se.bjurr.violations.lib.parsers.PCLintParser;
Expand Down Expand Up @@ -61,6 +63,8 @@ public enum Parser {
JSHINT(new JSHintParser()), //
LINT(new LintParser()), //
KLOCWORK(new KlocworkParser()), //
KOTLINMAVEN(new KotlinMavenParser()), //
KOTLINGRADLE(new KotlinGradleParser()), //
MYPY(new MyPyParser()), //
GOLINT(new GoLintParser()), //
GOOGLEERRORPRONE(new GoogleErrorProneParser()), //
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/se/bjurr/violations/lib/KotlinGradleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package se.bjurr.violations.lib;

import static se.bjurr.violations.lib.TestUtils.getRootFolder;

import org.junit.Test;

public class KotlinGradleTest {

@Test
public void testThatViolationsCanBeParsedGradle() {
final String rootFolder = getRootFolder();
}
}
13 changes: 13 additions & 0 deletions src/test/java/se/bjurr/violations/lib/KotlinMavenTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package se.bjurr.violations.lib;

import static se.bjurr.violations.lib.TestUtils.getRootFolder;

import org.junit.Test;

public class KotlinMavenTest {

@Test
public void testThatViolationsCanBeParsedGradle() {
final String rootFolder = getRootFolder();
}
}
3 changes: 3 additions & 0 deletions src/test/resources/kotlingradle/kotlin-gradle-example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
w: /Users/scottkennedy/project/src/main/java/com/example/Test.kt: (13, 87): Elvis operator (?:) always returns the left operand of non-nullable type String
w: /Users/wolfs/projects/gradle/build-tool-release/buildSrc/subprojects/configuration/src/main/kotlin/org/gradle/gradlebuild/dependencies/DependenciesMetadataRulesPlugin.kt: (75, 54): Unchecked cast: Any? to List<CapabilitySpec>
w: /home/bjerre/workspace/yet-another-kotlin-vs-java-comparison/src/main/kotlin/basics/controliiiflow/p01assigniiifromiiiif/Example.kt: (17, 9): Variable 'hejsan' is never used
18 changes: 18 additions & 0 deletions src/test/resources/kotlinmaven/kotlin-maven-example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/home/kocka/sources/kerub/src/test/kotlin/com/github/K0zka/kerub/host/ControllerManagerImplTest.kt
Warning:(34, 9) Kotlin: Variable 'controllerDynamic' is never used
/home/kocka/sources/kerub/src/test/kotlin/com/github/K0zka/kerub/host/HostCapabilitiesDiscovererTest.kt
Warning:(98, 45) Kotlin: Elvis operator (?:) always returns the left operand of non-nullable type kotlin.Long
Warning:(98, 53) Kotlin: Unnecessary safe call on a non-null receiver of type kotlin.String
Warning:(98, 63) Kotlin: Unnecessary safe call on a non-null receiver of type kotlin.Int
Warning:(153, 42) Kotlin: This syntax for lambda is deprecated. Use short lambda notation {a[: Int], b[: String] -> ...} or function expression instead.
Warning:(175, 7) Kotlin: Variable 'host' is never used
Warning:(181, 48) Kotlin: Unnecessary non-null assertion (!!) on a non-null receiver of type com.github.K0zka.kerub.host.HostCapabilitiesDiscoverer
Warning:(183, 40) Kotlin: Unnecessary safe call on a non-null receiver of type com.github.K0zka.kerub.model.HostCapabilities
Warning:(184, 45) Kotlin: Unnecessary safe call on a non-null receiver of type com.github.K0zka.kerub.model.HostCapabilities
/home/kocka/sources/kerub/src/test/kotlin/com/github/K0zka/kerub/host/SshClientUtilsTest.kt
Warning:(54, 37) Kotlin: This syntax for lambda is deprecated. Use short lambda notation {a[: Int], b[: String] -> ...} or function expression instead.
Warning:(64, 33) Kotlin: This syntax for lambda is deprecated. Use short lambda notation {a[: Int], b[: String] -> ...} or function expression instead.
/home/kocka/sources/kerub/src/test/kotlin/com/github/K0zka/kerub/hypervisor/kvm/UtilsTest.kt
Warning:(25, 7) Kotlin: Variable 'dom' is never used
/home/kocka/sources/kerub/src/test/kotlin/com/github/K0zka/kerub/utils/junix/dmi/DmiDecoderTest.kt
Warning:(127, 7) Kotlin: Variable 'devices' is never used

0 comments on commit 0f3c4e9

Please sign in to comment.