From 0f3c4e9b18f67ad26c1c517d8410583bb69f4b7a Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Thu, 20 Sep 2018 09:47:44 +0200 Subject: [PATCH] Kotlin Maven and Gradle parsers #44 --- README.md | 2 + .../lib/parsers/KotlinGradleParser.java | 59 +++++++++++++++++++ .../lib/parsers/KotlinMavenParser.java | 59 +++++++++++++++++++ .../bjurr/violations/lib/reports/Parser.java | 4 ++ .../violations/lib/KotlinGradleTest.java | 13 ++++ .../bjurr/violations/lib/KotlinMavenTest.java | 13 ++++ .../kotlingradle/kotlin-gradle-example.txt | 3 + .../kotlinmaven/kotlin-maven-example.txt | 18 ++++++ 8 files changed, 171 insertions(+) create mode 100644 src/main/java/se/bjurr/violations/lib/parsers/KotlinGradleParser.java create mode 100644 src/main/java/se/bjurr/violations/lib/parsers/KotlinMavenParser.java create mode 100644 src/test/java/se/bjurr/violations/lib/KotlinGradleTest.java create mode 100644 src/test/java/se/bjurr/violations/lib/KotlinMavenTest.java create mode 100644 src/test/resources/kotlingradle/kotlin-gradle-example.txt create mode 100644 src/test/resources/kotlinmaven/kotlin-maven-example.txt diff --git a/README.md b/README.md index d65c508c..c7b43537 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/main/java/se/bjurr/violations/lib/parsers/KotlinGradleParser.java b/src/main/java/se/bjurr/violations/lib/parsers/KotlinGradleParser.java new file mode 100644 index 00000000..d6b0766b --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/parsers/KotlinGradleParser.java @@ -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 parseReportOutput(final String string) throws Exception { + List violations = new ArrayList<>(); + List lines = getLines(string); + for (String lineString : lines) { + List> partsPerLine = new ArrayList<>(); + for (List 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; + } +} diff --git a/src/main/java/se/bjurr/violations/lib/parsers/KotlinMavenParser.java b/src/main/java/se/bjurr/violations/lib/parsers/KotlinMavenParser.java new file mode 100644 index 00000000..6cc7ba67 --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/parsers/KotlinMavenParser.java @@ -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 parseReportOutput(final String string) throws Exception { + List violations = new ArrayList<>(); + List lines = getLines(string); + for (String lineString : lines) { + List> partsPerLine = new ArrayList<>(); + for (List 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; + } +} diff --git a/src/main/java/se/bjurr/violations/lib/reports/Parser.java b/src/main/java/se/bjurr/violations/lib/reports/Parser.java index ed472c18..03e480a0 100644 --- a/src/main/java/se/bjurr/violations/lib/reports/Parser.java +++ b/src/main/java/se/bjurr/violations/lib/reports/Parser.java @@ -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; @@ -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()), // diff --git a/src/test/java/se/bjurr/violations/lib/KotlinGradleTest.java b/src/test/java/se/bjurr/violations/lib/KotlinGradleTest.java new file mode 100644 index 00000000..7985152b --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/KotlinGradleTest.java @@ -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(); + } +} diff --git a/src/test/java/se/bjurr/violations/lib/KotlinMavenTest.java b/src/test/java/se/bjurr/violations/lib/KotlinMavenTest.java new file mode 100644 index 00000000..07a93a55 --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/KotlinMavenTest.java @@ -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(); + } +} diff --git a/src/test/resources/kotlingradle/kotlin-gradle-example.txt b/src/test/resources/kotlingradle/kotlin-gradle-example.txt new file mode 100644 index 00000000..2b828129 --- /dev/null +++ b/src/test/resources/kotlingradle/kotlin-gradle-example.txt @@ -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 +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 diff --git a/src/test/resources/kotlinmaven/kotlin-maven-example.txt b/src/test/resources/kotlinmaven/kotlin-maven-example.txt new file mode 100644 index 00000000..0ddfa34d --- /dev/null +++ b/src/test/resources/kotlinmaven/kotlin-maven-example.txt @@ -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