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 59daadf
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 8 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,51 @@
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.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<List<String>> partsPerLine =
getLines(string, "([^:])+?:([^:]*)[^\\d]+?(\\d+?)[^\\d]+?(\\d+?)[^:]+?:(.*)");
for (List<String> parts : partsPerLine) {
String severity = parts.get(1).trim();
String filename = parts.get(2).trim();
Integer line = parseInt(parts.get(3));
Integer column = parseInt(parts.get(4));
String message = parts.get(5).trim();
violations.add( //
violationBuilder() //
.setParser(KOTLINGRADLE) //
.setStartLine(line) //
.setColumn(column) //
.setFile(filename) //
.setSeverity(toSeverity(severity)) //
.setMessage(message) //
.build() //
);
}
return violations;
}

public SEVERITY toSeverity(final String severity) {
if (severity.equalsIgnoreCase("e")) {
return ERROR;
}
if (severity.equalsIgnoreCase("w")) {
return WARN;
}
return INFO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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.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<List<String>> partsPerLine =
getLines(string, "\\[([^\\]]+?)\\]([^:]*)[^\\d]+?(\\d+?)[^\\d]+?(\\d+?)[^\\)]+?\\)(.*)");
for (List<String> parts : partsPerLine) {
String severity = parts.get(1).trim();
String filename = parts.get(2).trim();
Integer line = parseInt(parts.get(3));
Integer column = parseInt(parts.get(4));
String message = parts.get(5).trim();
violations.add( //
violationBuilder() //
.setParser(KOTLINMAVEN) //
.setStartLine(line) //
.setColumn(column) //
.setFile(filename) //
.setSeverity(toSeverity(severity)) //
.setMessage(message) //
.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
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,26 @@ public static List<List<String>> getLines(final String string, final String rege
final List<List<String>> results = new ArrayList<>();
final Pattern pattern = Pattern.compile(regexpPerLine);
for (final String line : string.split("\n")) {
final Matcher matcher = pattern.matcher(line);
if (!matcher.find()) {
continue;
final List<String> found = getLineParts(pattern, line);
if (found != null) {
results.add(found);
}
final List<String> lineParts = new ArrayList<>();
for (int g = 0; g <= matcher.groupCount(); g++) {
lineParts.add(matcher.group(g));
}
results.add(lineParts);
}
return results;
}

public static List<String> getLineParts(final Pattern pattern, final String line) {
final Matcher matcher = pattern.matcher(line);
if (!matcher.find()) {
return null;
}
final List<String> lineParts = new ArrayList<>();
for (int g = 0; g <= matcher.groupCount(); g++) {
lineParts.add(matcher.group(g));
}
return lineParts;
}

/**
* Match one regexp at a time. Remove the matched part from the string, trim, and match next
* regexp on that string...
Expand Down
42 changes: 42 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,42 @@
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.WARN;
import static se.bjurr.violations.lib.reports.Parser.KOTLINGRADLE;

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

public class KotlinGradleTest {

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

final List<Violation> actual =
violationsApi() //
.withPattern(".*/kotlingradle/.*\\.txt") //
.inFolder(rootFolder) //
.findAll(KOTLINGRADLE) //
.violations();

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

final Violation violation0 = actual.get(0);
assertThat(violation0.getMessage()) //
.isEqualTo(
"Elvis operator (?:) always returns the left operand of non-nullable type String");
assertThat(violation0.getFile()) //
.isEqualTo("/Users/scottkennedy/project/src/main/java/com/example/Test.kt");
assertThat(violation0.getSeverity()) //
.isEqualTo(WARN);
assertThat(violation0.getRule()) //
.isEqualTo("");
assertThat(violation0.getParser()) //
.isEqualTo(KOTLINGRADLE);
}
}
42 changes: 42 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,42 @@
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.WARN;
import static se.bjurr.violations.lib.reports.Parser.KOTLINMAVEN;

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

public class KotlinMavenTest {

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

final List<Violation> actual =
violationsApi() //
.withPattern(".*/kotlinmaven/.*\\.txt") //
.inFolder(rootFolder) //
.findAll(KOTLINMAVEN) //
.violations();

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

final Violation violation0 = actual.get(0);
assertThat(violation0.getMessage()) //
.isEqualTo("Unchecked cast: AssetDao<*> to DaoOperations.Read<T, UUID>");
assertThat(violation0.getFile()) //
.isEqualTo(
"/home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/data/hub/AnyAssetDaoImpl.kt");
assertThat(violation0.getSeverity()) //
.isEqualTo(WARN);
assertThat(violation0.getRule()) //
.isEqualTo("");
assertThat(violation0.getParser()) //
.isEqualTo(KOTLINMAVEN);
}
}
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
39 changes: 39 additions & 0 deletions src/test/resources/kotlinmaven/kotlin-maven-example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/data/hub/AnyAssetDaoImpl.kt: (44, 63) Unchecked cast: AssetDao<*> to DaoOperations.Read<T, UUID>
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/data/hub/AnyEntityDaoImpl.kt: (18, 27) Unchecked cast: KClass<out Entity<*>> to KClass<out Asset>
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/data/hub/AnyEntityDaoImpl.kt: (25, 30) Unchecked cast: KClass<out Entity<*>> to KClass<out Asset>
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/planner/Plan.kt: (14, 12) Parameter 'step' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/planner/Plan.kt: (14, 44) Parameter 'plan' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/planner/PlanComparator.kt: (50, 8) Name shadowed: first
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/planner/PlanComparator.kt: (50, 8) Variable 'first' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/planner/PlanComparator.kt: (51, 8) Name shadowed: second
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/planner/PlanComparator.kt: (51, 8) Variable 'second' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/planner/execution/PlanExecutorImpl.kt: (152, 14) Unchecked cast: StepExecutor<*>? to StepExecutor<AbstractOperationalStep>
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/planner/steps/base/AbstractUnAllocateFactory.kt: (17, 21) Unchecked cast: VirtualStorageAllocation to S
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/planner/steps/vstorage/migrate/live/libvirt/LibvirtMigrateVirtualStorageDeviceExecutor.kt: (13, 10) Unreachable code
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/services/impl/AbstractAssetService.kt: (92, 50) Unchecked cast: List<Asset> to List<T>
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/services/impl/VirtualStorageDeviceServiceImpl.kt: (49, 42) 'allocation: VirtualStorageAllocation' is deprecated. Use allocations
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/services/impl/VirtualStorageDeviceServiceImpl.kt: (51, 43) 'allocation: VirtualStorageAllocation' is deprecated. Use allocations
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/services/socket/InternalMessageListenerImpl.kt: (41, 36) The corresponding parameter in the supertype 'InternalMessageListener' is named 'sessionId'. This may cause problems when calling this function with named arguments.
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/services/socket/SpringSocketClientConnection.kt: (60, 48) Unchecked cast: KClass<Entity<*>> to KClass<out Entity<out Any>>
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/AnyUtils.kt: (7, 1) Expected performance impact of inlining 'public inline fun NOP(): Unit defined in com.github.kerubistan.kerub.utils in file AnyUtils.kt' is insignificant. Inlining works best for functions with parameters of functional types
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/AnyUtils.kt: (9, 1) Expected performance impact of inlining 'public inline fun NOP(comment: String): Unit defined in com.github.kerubistan.kerub.utils in file AnyUtils.kt' is insignificant. Inlining works best for functions with parameters of functional types
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/AnyUtils.kt: (9, 16) Parameter 'comment' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/artemis/MqInit.kt: (20, 10) 'deployQueue(SimpleString!, SimpleString!, SimpleString!, Boolean, Boolean): Queue!' is deprecated. Deprecated in Java
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/ipmi/IOUtils.kt: (8, 24) Parameter 'timeout' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/ipmi/IpmiClient.kt: (49, 18) Parameter 'address' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/ipmi/IpmiClient.kt: (49, 35) Parameter 'onSession' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/ispn/InfinispanJsonExternalizer.kt: (19, 33) Unchecked cast: Class<out Any> to Class<out Entity<*>>
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/fw/ipfw/Ipfw.kt: (6, 11) Parameter 'session' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/fw/ipfw/Ipfw.kt: (6, 35) Parameter 'port' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/fw/ipfw/Ipfw.kt: (6, 46) Parameter 'proto' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/fw/ipfw/Ipfw.kt: (10, 12) Parameter 'session' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/fw/ipfw/Ipfw.kt: (10, 36) Parameter 'port' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/fw/ipfw/Ipfw.kt: (10, 47) Parameter 'proto' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/mount/FSTab.kt: (10, 11) Parameter 'session' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/storagemanager/lvm/LvmLv.kt: (137, 42) Parameter 'vgName' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/sysctl/BsdSysCtl.kt: (13, 7) Variable 'props' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/virt/vbox/VBoxManage.kt: (50, 9) Variable 'device' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/virt/vbox/VBoxManage.kt: (147, 25) 'allocation: VirtualStorageAllocation' is deprecated. Use allocations
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/virt/vbox/VBoxManage.kt: (187, 8) Parameter 'listener' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/virt/vbox/VBoxManage.kt: (190, 57) Parameter 'size' is never used
[WARNING] /home/bjerre/workspace/kerub/src/main/kotlin/com/github/kerubistan/kerub/utils/junix/zfs/Zfs.kt: (16, 3) Unreachable code

0 comments on commit 59daadf

Please sign in to comment.