Skip to content

Commit

Permalink
Merge branch 'master' into grypeParser
Browse files Browse the repository at this point in the history
  • Loading branch information
dtbaum authored Jul 26, 2023
2 parents ed60760 + 533ac7e commit b32c021
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 42 deletions.
2 changes: 1 addition & 1 deletion SUPPORTED-FORMATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,7 @@ analyze - iccxxxxcompiler_opts cstat2.c</pre></code>For details check the IAR C-
</tr>
<tr>
<td colspan="4">
:bulb: <p>Create a./pylintrc that contains:<p><code>msg-template={path}:{module}:{line}: [{msg_id}({symbol}), {obj}] {msg}</code></p></p><p>Start pylint using the command:<p><code>pylint --rcfile=./pylintrc CODE > pylint.log</code></p></p>
:bulb: <p>Start Pylint using this custom message template (can also be configured via a pylintrc configuration file):<p><code>pylint --msg-template='{path}:{line}: [{msg_id}, {obj}] {msg} ({symbol})' modules_or_packages > pylint.log</code></p></p>
</td>
</tr>
<tr>
Expand Down
14 changes: 11 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -54,7 +53,7 @@

<properties>
<scmTag>HEAD</scmTag>
<revision>11.4.0</revision>
<revision>11.5.0</revision>
<changelist>-SNAPSHOT</changelist>

<module.name>edu.hm.hafner.analysis.model</module.name>
Expand All @@ -71,7 +70,7 @@
<violations-lib.version>1.156.5</violations-lib.version>
<jsoup.version>1.16.1</jsoup.version>
<json.version>20230618</json.version>
<json-smart.version>2.4.11</json-smart.version>
<json-smart.version>2.5.0</json-smart.version>
<pmd.version>6.55.0</pmd.version>
<spotbugs.version>4.7.3</spotbugs.version>

Expand Down Expand Up @@ -477,6 +476,15 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>all</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
<forkCount>1C</forkCount>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public abstract class AbstractMavenLogParser extends LookaheadParser {
private static final String MAVEN_PLUGIN_SUFFIX = "-plugin";
static final String MAVEN_COMPILER_PLUGIN = MAVEN_PLUGIN_PREFIX + "compiler" + MAVEN_PLUGIN_SUFFIX;
static final String MAVEN_JAVADOC_PLUGIN = MAVEN_PLUGIN_PREFIX + "javadoc" + MAVEN_PLUGIN_SUFFIX;
static final String MAVEN_HPI_PLUGIN = MAVEN_PLUGIN_PREFIX + "hpi" + MAVEN_PLUGIN_SUFFIX;
static final String MAVEN_ENFORCER_PLUGIN = MAVEN_PLUGIN_PREFIX + "enforcer" + MAVEN_PLUGIN_SUFFIX;
private String goal = StringUtils.EMPTY;
private String module = StringUtils.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.util.IntegerParser;
import edu.hm.hafner.analysis.util.XmlElementUtil;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

Expand Down Expand Up @@ -126,7 +127,6 @@ private static String extractField(final Element diag, final XPathExpression exp
}

private static int extractIntField(final Element diag, final XPathExpression expr) throws XPathExpressionException {
String val = extractField(diag, expr);
return Integer.parseInt(val);
return IntegerParser.parseInt(extractField(diag, expr));
}
}
20 changes: 12 additions & 8 deletions src/main/java/edu/hm/hafner/analysis/parser/FlawfinderParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.LookaheadParser;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.util.IntegerParser;
import edu.hm.hafner.util.LookaheadStream;

/**
Expand Down Expand Up @@ -35,15 +36,8 @@ protected Optional<Issue> createIssue(final Matcher matcher, final LookaheadStre
final IssueBuilder builder) {
String message = matcher.group("message");
String category = matcher.group("category");
int severity = Integer.parseInt(matcher.group("severity"));
Severity priority = Severity.WARNING_LOW;

if (severity >= FLAWFINDER_HIGH_THRESHOLD) {
priority = Severity.WARNING_HIGH;
}
else if (severity >= FLAWFINDER_NORMAL_THRESHOLD) {
priority = Severity.WARNING_NORMAL;
}
var priority = extractPriority(IntegerParser.parseInt(matcher.group("severity")));

return builder.setFileName(matcher.group("file"))
.setLineStart(matcher.group("line"))
Expand All @@ -52,4 +46,14 @@ else if (severity >= FLAWFINDER_NORMAL_THRESHOLD) {
.setSeverity(priority)
.buildOptional();
}

private Severity extractPriority(final int severity) {
if (severity >= FLAWFINDER_HIGH_THRESHOLD) {
return Severity.WARNING_HIGH;
}
else if (severity >= FLAWFINDER_NORMAL_THRESHOLD) {
return Severity.WARNING_NORMAL;
}
return Severity.WARNING_LOW;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Optional;

import edu.hm.hafner.analysis.util.IntegerParser;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -43,7 +44,7 @@ private Report parseProblems(final List<Element> elements) {
if (problemClass.isPresent()) {
Element problem = problemClass.get();
issueBuilder.setFileName(stripPathPrefix(file))
.setLineStart(Integer.parseInt(getChildValue(element, "line")))
.setLineStart(IntegerParser.parseInt(getChildValue(element, "line")))
.setCategory(StringEscapeUtils.unescapeXml(getValue(problem)))
.setMessage(StringEscapeUtils.unescapeXml(getChildValue(element, "description")))
.setModuleName(StringEscapeUtils.unescapeXml(getChildValue(element, "module")))
Expand Down
32 changes: 17 additions & 15 deletions src/main/java/edu/hm/hafner/analysis/parser/JavacParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ public class JavacParser extends AbstractMavenLogParser {
private static final String ERROR_PRONE_URL_PATTERN = "\\s+\\(see https?://\\S+\\s*\\)";

private static final String JAVAC_WARNING_PATTERN
= "^(?:\\S+\\s+)?" // optional preceding arbitrary number of characters that are not a
// whitespace followed by whitespace. This can be used for timestamps.
= "^(?:\\S+\\s+)?" // optional preceding arbitrary number of characters that are not a
// whitespace followed by whitespace. This can be used for timestamps.
+ "(?:(?:\\[(WARNING|ERROR)\\]|w:|e:)\\s+)" // optional [WARNING] or [ERROR] or w: or e:
+ "([^\\[\\(]*):\\s*" // group 1: filename
+ "[\\[\\(]" // [ or (
+ "(\\d+)[.,;]*" // group 2: line number
+ "\\s?(\\d+)?" // group 3: optional column
+ "[\\]\\)]\\s*" // ] or )
+ ":?" // optional :
+ "(?:\\[(\\w+)\\])?" // group 4: optional category
+ "\\s*(.*)$"; // group 5: message
+ "(((\\/?[a-zA-Z]|file):)?[^\\[\\(:]*):" // group 2: filename starting path with C:\ or /C:\ or file:/// or /
+ "(" // start group 5
+ "(\\s*[\\[\\(]?)?" // optional ( or [
+ "(\\d+)" // group 7 line
+ "[.,;]?\\s?:?" // separator
+ "(\\d+)?" // group 8 column
+ "[\\]\\)]?\\s*:?\\s?" // optional ) or ] or whitespace or :
+ ")" // end group 5
+ "(?:\\[(\\w+)\\])?" // group 9: optional category
+ "\\s*(.*)$"; // group 10: message

private static final String SEVERITY_ERROR = "ERROR";
private static final String SEVERITY_ERROR_SHORT = "e:";
Expand All @@ -51,7 +53,7 @@ public JavacParser() {
@Override
protected boolean isLineInteresting(final String line) {
return (line.contains("[") || line.contains("w:") || line.contains("e:"))
&& !hasGoals(MAVEN_JAVADOC_PLUGIN);
&& !hasGoals(MAVEN_JAVADOC_PLUGIN, MAVEN_HPI_PLUGIN);
}

@Override
Expand All @@ -69,14 +71,14 @@ protected Optional<Issue> createIssue(final Matcher matcher, final LookaheadStre
builder.setSeverity(Severity.WARNING_NORMAL);
}

String message = matcher.group(6);
String category = guessCategoryIfEmpty(matcher.group(5), message);
String message = matcher.group(10);
String category = guessCategoryIfEmpty(matcher.group(9), message);

// get rid of leading / from windows compiler output JENKINS-66738
return builder.setFileName(RegExUtils.replaceAll(matcher.group(2), "^/([a-zA-Z]):", "$1:"))
.setLineStart(matcher.group(3))
.setLineStart(matcher.group(7))
.setType(StringUtils.defaultString(getGoal(), DEFAULT_GOAL))
.setColumnStart(matcher.group(4))
.setColumnStart(matcher.group(8))
.setCategory(category)
.setMessage(message)
.buildOptional();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.util.IntegerParser;
import edu.hm.hafner.analysis.util.XmlElementUtil;

import static java.lang.Integer.*;

/**
* Parses a StyleCop XML report files.
*
Expand Down Expand Up @@ -105,7 +104,7 @@ private String getString(final Element element, final String name) {
*/
private int getLineNumber(final Element violation) {
if (violation.hasAttribute("LineNumber")) {
return parseInt(violation.getAttribute("LineNumber"));
return IntegerParser.parseInt(violation.getAttribute("LineNumber"));
}
else {
return 0;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/edu/hm/hafner/analysis/parser/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ private LineRangeList readLineRanges(final XPath path, final NodeList lineRanges
ranges.add(new LineRange(start, end));
}
catch (NumberFormatException e) {
// Invalid value in xml.
// Ignore invalid values in xml
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ public IssueParser createParser(final Option... options) {

@Override
public String getHelp() {
return "<p>Create a ./pylintrc that contains:"
+ "<p><code>msg-template={path}:{module}:{line}: [{msg_id}({symbol}), {obj}] {msg}</code></p>"
+ "</p>"
+ "<p>Start pylint using the command:"
+ "<p><code>pylint --rcfile=./pylintrc CODE > pylint.log</code></p>"
return "<p>Start Pylint using this custom message template (can also be configured via a pylintrc configuration file):"
+ "<p><code>pylint --msg-template='{path}:{line}: [{msg_id}, {obj}] {msg} ({symbol})' modules_or_packages > pylint.log</code></p>"
+ "</p>";
}

Expand Down
12 changes: 11 additions & 1 deletion src/test/java/edu/hm/hafner/analysis/ArchitectureTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.hm.hafner.analysis;
package edu.hm.hafner.analysis; //NOPMD - suppressed TooManyStaticImports

import javax.xml.parsers.SAXParser;

Expand All @@ -12,6 +12,10 @@

import edu.hm.hafner.util.ArchitectureRules;

import static com.tngtech.archunit.core.domain.JavaAccess.Predicates.*;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.*;
import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.*;
import static com.tngtech.archunit.lang.conditions.ArchPredicates.*;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;

/**
Expand All @@ -22,6 +26,12 @@
@SuppressWarnings("hideutilityclassconstructor")
@AnalyzeClasses(packages = "edu.hm.hafner.analysis")
class ArchitectureTest {
/** Replace all calls of {@link Integer#parseInt(String)} with IntegerParser alternative. */
@ArchTest
static final ArchRule NO_INTEGER_PARSE_INT =
noClasses().should().callCodeUnitWhere(targetOwner(is(type(Integer.class))).and(target(name("parseInt"))))
.because("only save IntegerParser.parseInt should be used to parse integer values");

/** Digester must not be used directly, rather use a SecureDigester instance. */
@ArchTest
static final ArchRule NO_DIGESTER_CONSTRUCTOR_CALLED =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void assertThatIssuesArePresent(final Report report, final SoftAsserti
@Test
void issue56235() {
Report warnings = parse("issue56235.xml");
assertThat(warnings).hasSize(6);
assertThat(warnings).hasSize(7);

Iterator<Issue> iterator = warnings.iterator();

Expand Down Expand Up @@ -101,6 +101,14 @@ void issue56235() {
"Method invocation <code>getCodeFragment</code> may produce <code>NullPointerException</code>")
.hasFileName(
"$PROJECT_DIR$/src/test/java/edu/hm/hafner/analysis/parser/dry/cpd/CpdParserTest.java");
softly.assertThat(iterator.next())
.hasSeverity(Severity.WARNING_LOW)
.hasCategory("@NotNull/@Nullable problems")
.hasLineStart(0)
.hasLineEnd(0)
.hasMessage(
"Not 'edu.umd.cs.findbugs.annotations.Nullable' but 'org.jetbrains.annotations.Nullable' would be used for code generation.")
.hasFileName("$PROJECT_DIR$/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java");
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/edu/hm/hafner/analysis/parser/JavacParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,48 @@ void shouldParseJavaWarningsInMavenCompilerPlugin() {
.hasLineStart(194)
.hasFileName("/home/runner/work/warnings-ng-plugin/warnings-ng-plugin/plugin/target/generated-test-sources/assertj-assertions/io/jenkins/plugins/analysis/core/assertions/Assertions.java");
}

/**
* Parses a warning log written by Gradle containing 2 Kotlin warnings.
* One in kotlin 1.8 style and the other one in the old style.
*/
@Test
void kotlin18WarningStyle() {
Report warnings = parse("kotlin-1_8.txt");

assertThat(warnings).hasSize(7);

assertThat(warnings.get(0)).hasSeverity(Severity.WARNING_NORMAL)
.hasLineStart(214)
.hasColumnStart(35)
.hasFileName("/project/app/src/main/java/ui/Activity.kt");
assertThat(warnings.get(1)).hasSeverity(Severity.WARNING_NORMAL)
.hasLineStart(424)
.hasColumnStart(29)
.hasFileName("/project/app/src/main/java/ui/Activity.kt");
assertThat(warnings.get(2)).hasSeverity(Severity.WARNING_NORMAL)
.hasLineStart(425)
.hasColumnStart(29)
.hasFileName("/project/app/src/main/java/ui/Activity.kt")
.hasCategory("Deprecation")
.hasMessage("deprecated: Serializable! to kotlin.collections.HashMap<String, String> /* = java.util.HashMap<String, String> */");
assertThat(warnings.get(3)).hasSeverity(Severity.WARNING_NORMAL)
.hasLineStart(424)
.hasColumnStart(29)
.hasFileName("/project/app/src/main/java/ui/Activity.kt");
assertThat(warnings.get(4)).hasSeverity(Severity.WARNING_NORMAL)
.hasLineStart(123)
.hasColumnStart(456);
assertThat(warnings.get(5)).hasSeverity(Severity.WARNING_NORMAL)
.hasLineStart(426)
.hasColumnStart(29)
.hasMessage("Unchecked cast: Serializable! to kotlin.collections.HashMap<String, String> /* = java.util.HashMap<String, String> */");
assertThat(warnings.get(6)).hasSeverity(Severity.WARNING_NORMAL)
.hasLineStart(8)
.hasColumnStart(27)
.hasCategory("Deprecation")
.hasFileName("file:///project/src/main/java/com/app/ui/model/Activity.kt")
.hasMessage("'PackageStats' is deprecated. Deprecated in Java");
}
}

5 changes: 5 additions & 0 deletions src/test/resources/archunit_ignore_patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@

// Assertions.assertTimeoutPreemptively from JUnit 5 is ok to use
.*org.junit.jupiter.api.Assertions.assertTimeoutPreemptively.*

// Here Integer.parseInt is ok to use since the exception is caught
.*edu.hm.hafner.analysis.parser.XmlParser.readLineRanges.*
.*edu.hm.hafner.analysis.registry.DryDescriptor.convertThreshold.*
.*edu.hm.hafner.analysis.util.IntegerParser.parseInt.*
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Constant conditions &amp; exceptions</problem_class>
<description>Method invocation &lt;code&gt;getCodeFragment&lt;/code&gt; may produce &lt;code&gt;NullPointerException&lt;/code&gt;</description>
</problem>
<problem>
<file>file://$PROJECT_DIR$/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java</file>
<module>analysis-model</module>
<package>edu.hm.hafner.analysis</package>
<entry_point TYPE="field" FQNAME="edu.hm.hafner.analysis.IssueBuilder moduleName" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">@NotNull/@Nullable problems</problem_class>
<description>Not 'edu.umd.cs.findbugs.annotations.Nullable' but 'org.jetbrains.annotations.Nullable' would be used for code generation.</description>
</problem>
</problems>
13 changes: 13 additions & 0 deletions src/test/resources/edu/hm/hafner/analysis/parser/kotlin-1_8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
> Configure project :app
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
app: 'annotationProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.android.databinding:compiler:3.0.1'.
w: /project/app/src/main/java/ui/Activity.kt: (214, 35): Unchecked cast: Serializable! to kotlin.collections.HashMap<String, String> /* = java.util.HashMap<String, String> */
w: /project/app/src/main/java/ui/Activity.kt:424:29 Unchecked cast: Serializable! to kotlin.collections.HashMap<String, String> /* = java.util.HashMap<String, String> */
e: /project/app/src/main/java/ui/Activity.kt:425:29 deprecated: Serializable! to kotlin.collections.HashMap<String, String> /* = java.util.HashMap<String, String> */
w: /project/app/src/main/java/ui/Activity.kt:424:29 Serializable! to kotlin.collections.HashMap<String, String> /* = java.util.HashMap<String, String> */
w: /project/app/src/main/java/ui/Activity.kt:123:456 Unchecked cast: Serializable! to kotlin.collections.HashMap<String, String> /* = java.util.HashMap<String, String> */
w: c:\project\app\src\main\java\ui\Activity.kt:426:29 Unchecked cast: Serializable! to kotlin.collections.HashMap<String, String> /* = java.util.HashMap<String, String> */
w: file:///project/src/main/java/com/app/ui/model/Activity.kt:8:27 'PackageStats' is deprecated. Deprecated in Java

0 comments on commit b32c021

Please sign in to comment.