Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-71687] Intellij IDEA Inspection causes Exception during Jenkins Job fix #934

Merged
merged 6 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

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

<module.name>edu.hm.hafner.analysis.model</module.name>
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
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
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
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>
Loading