From 9626c11dc66e7e1ca757e64b9493cd0fdb4b56c4 Mon Sep 17 00:00:00 2001 From: mso Date: Mon, 24 Jul 2023 15:35:09 +0200 Subject: [PATCH 1/6] make setLineStart(Integer.parseInt(getChildValue(element, "line"))); optional + add test --- .../hafner/analysis/parser/IdeaInspectionParser.java | 4 +++- .../analysis/parser/IdeaInspectionParserTest.java | 10 +++++++++- .../edu/hm/hafner/analysis/parser/issue56235.xml | 8 ++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java b/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java index fa0a1963e..878c1f44a 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java @@ -43,11 +43,13 @@ private Report parseProblems(final List elements) { if (problemClass.isPresent()) { Element problem = problemClass.get(); issueBuilder.setFileName(stripPathPrefix(file)) - .setLineStart(Integer.parseInt(getChildValue(element, "line"))) .setCategory(StringEscapeUtils.unescapeXml(getValue(problem))) .setMessage(StringEscapeUtils.unescapeXml(getChildValue(element, "description"))) .setModuleName(StringEscapeUtils.unescapeXml(getChildValue(element, "module"))) .setSeverity(getPriority(problem.getAttribute("severity"))); + if(!getChildValue(element, "line").equals("-")){ + issueBuilder.setLineStart(Integer.parseInt(getChildValue(element, "line"))); + } problems.add(issueBuilder.buildAndClean()); } } diff --git a/src/test/java/edu/hm/hafner/analysis/parser/IdeaInspectionParserTest.java b/src/test/java/edu/hm/hafner/analysis/parser/IdeaInspectionParserTest.java index a3b3765c2..fab0ecf3f 100644 --- a/src/test/java/edu/hm/hafner/analysis/parser/IdeaInspectionParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/parser/IdeaInspectionParserTest.java @@ -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 iterator = warnings.iterator(); @@ -101,6 +101,14 @@ void issue56235() { "Method invocation getCodeFragment may produce NullPointerException") .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"); } } } diff --git a/src/test/resources/edu/hm/hafner/analysis/parser/issue56235.xml b/src/test/resources/edu/hm/hafner/analysis/parser/issue56235.xml index 6a7704a1d..3126f3ab2 100644 --- a/src/test/resources/edu/hm/hafner/analysis/parser/issue56235.xml +++ b/src/test/resources/edu/hm/hafner/analysis/parser/issue56235.xml @@ -58,4 +58,12 @@ Constant conditions & exceptions Method invocation <code>getCodeFragment</code> may produce <code>NullPointerException</code> + + file://$PROJECT_DIR$/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java + analysis-model + edu.hm.hafner.analysis + + @NotNull/@Nullable problems + Not 'edu.umd.cs.findbugs.annotations.Nullable' but 'org.jetbrains.annotations.Nullable' would be used for code generation. + From 195d13e89bebb2fea10a4db6e387a8f4ca1d477a Mon Sep 17 00:00:00 2001 From: mso Date: Mon, 24 Jul 2023 15:43:44 +0200 Subject: [PATCH 2/6] make setLineStart(Integer.parseInt(getChildValue(element, "line"))); optional + add test --- .../edu/hm/hafner/analysis/parser/IdeaInspectionParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java b/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java index 878c1f44a..9349d0161 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java @@ -48,7 +48,7 @@ private Report parseProblems(final List elements) { .setModuleName(StringEscapeUtils.unescapeXml(getChildValue(element, "module"))) .setSeverity(getPriority(problem.getAttribute("severity"))); if(!getChildValue(element, "line").equals("-")){ - issueBuilder.setLineStart(Integer.parseInt(getChildValue(element, "line"))); + issueBuilder.setLineStart(Integer.parseInt(getChildValue(element, "line")));// } problems.add(issueBuilder.buildAndClean()); } From 92d5b1052351c10629377ffd805c00c82363ee73 Mon Sep 17 00:00:00 2001 From: mso Date: Mon, 24 Jul 2023 15:55:41 +0200 Subject: [PATCH 3/6] make setLineStart(Integer.parseInt(getChildValue(element, "line"))); optional + add test --- .../edu/hm/hafner/analysis/parser/IdeaInspectionParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java b/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java index 9349d0161..878c1f44a 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java @@ -48,7 +48,7 @@ private Report parseProblems(final List elements) { .setModuleName(StringEscapeUtils.unescapeXml(getChildValue(element, "module"))) .setSeverity(getPriority(problem.getAttribute("severity"))); if(!getChildValue(element, "line").equals("-")){ - issueBuilder.setLineStart(Integer.parseInt(getChildValue(element, "line")));// + issueBuilder.setLineStart(Integer.parseInt(getChildValue(element, "line"))); } problems.add(issueBuilder.buildAndClean()); } From 22f68a0167a4de952bfb2a67ce9b9896f3e27632 Mon Sep 17 00:00:00 2001 From: mso Date: Wed, 26 Jul 2023 10:13:36 +0200 Subject: [PATCH 4/6] change Integer.parseInt to IntegerParser.parseInt --- .../edu/hm/hafner/analysis/parser/IdeaInspectionParser.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java b/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java index 878c1f44a..78188b6b5 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/IdeaInspectionParser.java @@ -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; @@ -43,13 +44,11 @@ private Report parseProblems(final List elements) { if (problemClass.isPresent()) { Element problem = problemClass.get(); issueBuilder.setFileName(stripPathPrefix(file)) + .setLineStart(IntegerParser.parseInt(getChildValue(element, "line"))) .setCategory(StringEscapeUtils.unescapeXml(getValue(problem))) .setMessage(StringEscapeUtils.unescapeXml(getChildValue(element, "description"))) .setModuleName(StringEscapeUtils.unescapeXml(getChildValue(element, "module"))) .setSeverity(getPriority(problem.getAttribute("severity"))); - if(!getChildValue(element, "line").equals("-")){ - issueBuilder.setLineStart(Integer.parseInt(getChildValue(element, "line"))); - } problems.add(issueBuilder.buildAndClean()); } } From 8fa5b293cb727adbd6188d31f71e3e5101fbc732 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Wed, 26 Jul 2023 11:30:10 +0200 Subject: [PATCH 5/6] Replace `Integer.parseInt` with `IntegerParser.parseInt`. --- .../parser/ClangAnalyzerPlistParser.java | 4 ++-- .../analysis/parser/FlawfinderParser.java | 20 +++++++++++-------- .../analysis/parser/StyleCopParser.java | 5 ++--- .../hm/hafner/analysis/parser/XmlParser.java | 3 +-- .../hm/hafner/analysis/ArchitectureTest.java | 12 ++++++++++- .../resources/archunit_ignore_patterns.txt | 5 +++++ 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/java/edu/hm/hafner/analysis/parser/ClangAnalyzerPlistParser.java b/src/main/java/edu/hm/hafner/analysis/parser/ClangAnalyzerPlistParser.java index 96ce84f3a..ff9a17d46 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/ClangAnalyzerPlistParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/ClangAnalyzerPlistParser.java @@ -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; @@ -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)); } } diff --git a/src/main/java/edu/hm/hafner/analysis/parser/FlawfinderParser.java b/src/main/java/edu/hm/hafner/analysis/parser/FlawfinderParser.java index 7be982ad5..6659ef054 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/FlawfinderParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/FlawfinderParser.java @@ -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; /** @@ -35,15 +36,8 @@ protected Optional 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")) @@ -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; + } } diff --git a/src/main/java/edu/hm/hafner/analysis/parser/StyleCopParser.java b/src/main/java/edu/hm/hafner/analysis/parser/StyleCopParser.java index 47809ba98..284839762 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/StyleCopParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/StyleCopParser.java @@ -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. * @@ -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; diff --git a/src/main/java/edu/hm/hafner/analysis/parser/XmlParser.java b/src/main/java/edu/hm/hafner/analysis/parser/XmlParser.java index 5be1a1f3d..02a52fc05 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/XmlParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/XmlParser.java @@ -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 } - } } } diff --git a/src/test/java/edu/hm/hafner/analysis/ArchitectureTest.java b/src/test/java/edu/hm/hafner/analysis/ArchitectureTest.java index dd86d784e..ce25ee811 100644 --- a/src/test/java/edu/hm/hafner/analysis/ArchitectureTest.java +++ b/src/test/java/edu/hm/hafner/analysis/ArchitectureTest.java @@ -1,4 +1,4 @@ -package edu.hm.hafner.analysis; +package edu.hm.hafner.analysis; //NOPMD - suppressed TooManyStaticImports import javax.xml.parsers.SAXParser; @@ -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.*; /** @@ -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 = diff --git a/src/test/resources/archunit_ignore_patterns.txt b/src/test/resources/archunit_ignore_patterns.txt index 653fad059..e07ede8b1 100644 --- a/src/test/resources/archunit_ignore_patterns.txt +++ b/src/test/resources/archunit_ignore_patterns.txt @@ -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.* From f75ab99d873c0d302df8f1d9fcd1235935e1e67f Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Wed, 26 Jul 2023 12:01:28 +0200 Subject: [PATCH 6/6] Bump version to 11.4.2. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7b3129a4e..7e82b829e 100644 --- a/pom.xml +++ b/pom.xml @@ -53,7 +53,7 @@ HEAD - 11.5.0 + 11.4.2 -SNAPSHOT edu.hm.hafner.analysis.model