Skip to content

Commit

Permalink
Merge pull request #1090 from jenkinsci/pvs-studio-refactoring
Browse files Browse the repository at this point in the history
Pvs studio refactoring
  • Loading branch information
uhafner authored Sep 13, 2024
2 parents b4e540c + 6a1a02a commit 40fd342
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 72 deletions.
1 change: 1 addition & 0 deletions src/main/java/edu/hm/hafner/analysis/ModuleDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public interface FileSystem {
*
* @return the found file names
*/
@SuppressWarnings("AvoidObjectArrays") // TODO: change to list in next major release
String[] find(Path root, String pattern);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -22,52 +21,41 @@
*
* @author PVS-Studio Team
*/
class PlogMessage {
private String file = StringUtils.EMPTY;
private int lineNumber = 0;
private String errorCode = StringUtils.EMPTY;
private String message = StringUtils.EMPTY;
private String level = StringUtils.EMPTY;

public String getHash() {
return errorCode + message + file + lineNumber;
}

@Override
public String toString() {
return message;
}

String getFilePath() {
return file;
}
class PlogMessagesReader {
private int failWarningsCount;
private int falseAlarmCount;

public int getLine() {
return lineNumber;
}
/**
* Getting list messages from the report.
*
* @param readerFactory
* factory containing report file reader
*
* @return list plog messages
*/
List<PlogMessage> getMessagesFromReport(final ReaderFactory readerFactory) {
var plogDoc = readerFactory.readDocument();
plogDoc.getDocumentElement().normalize();

public String getType() {
return errorCode;
}
NodeList nList = plogDoc.getElementsByTagName("PVS-Studio_Analysis_Log");

public String getLevel() {
return level;
}
List<PlogMessage> plogMessages = new ArrayList<>();
for (int nodeCount = 0; nodeCount < nList.getLength(); nodeCount++) {
Node nNode = nList.item(nodeCount);

private static boolean skipMessage(final NodeList elements) {
return elements != null && elements.item(0) != null
&& equalsIgnoreCase(elements.item(0).getTextContent(), "true");
}
if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Check warning on line 46 in src/main/java/edu/hm/hafner/analysis/parser/pvsstudio/PlogMessagesReader.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 46 is only partially covered, one branch is missing
processNode(plogMessages, nNode);
}
}

private static boolean nodeNotNull(final NodeList elements) {
return elements != null && elements.item(0) != null && elements.item(0).getTextContent() != null;
}
if ((plogMessages.size() + falseAlarmCount) == 0 && failWarningsCount > 0) {

Check warning on line 51 in src/main/java/edu/hm/hafner/analysis/parser/pvsstudio/PlogMessagesReader.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 51 is only partially covered, 3 branches are missing
Logger.getLogger(PvsStudioParser.class.getName()).log(Level.SEVERE, "No messages were parsed!");

Check warning on line 52 in src/main/java/edu/hm/hafner/analysis/parser/pvsstudio/PlogMessagesReader.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered line

Line 52 is not covered by tests
}

private static boolean errorCodeIsValid(final String errorCode) {
return !(errorCode.isEmpty() || errorCode.charAt(0) != 'V');
return plogMessages;
}

private static void processNode(final List<PlogMessage> plogMessages, final Node node) {
private void processNode(final List<PlogMessage> plogMessages, final Node node) {
var eElement = (Element) node;

var nodeFalseAlarm = eElement.getElementsByTagName("FalseAlarm");
Expand Down Expand Up @@ -116,39 +104,50 @@ private static void processNode(final List<PlogMessage> plogMessages, final Node
plogMessages.add(msg);
}

private static int failWarningsCount = 0;
private static int falseAlarmCount = 0;
private boolean skipMessage(final NodeList elements) {
return elements != null && elements.item(0) != null

Check warning on line 108 in src/main/java/edu/hm/hafner/analysis/parser/pvsstudio/PlogMessagesReader.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 108 is only partially covered, 2 branches are missing
&& equalsIgnoreCase(elements.item(0).getTextContent(), "true");
}

/**
* Getting list messages from report.
*
* @param readerFactory
* factory containing report file reader
*
* @return list plog messages
*/
static List<PlogMessage> getMessagesFromReport(final ReaderFactory readerFactory) {
Document plogDoc = readerFactory.readDocument();
plogDoc.getDocumentElement().normalize();
private boolean nodeNotNull(final NodeList elements) {
return elements != null && elements.item(0) != null && elements.item(0).getTextContent() != null;

Check warning on line 113 in src/main/java/edu/hm/hafner/analysis/parser/pvsstudio/PlogMessagesReader.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 113 is only partially covered, 3 branches are missing
}

NodeList nList = plogDoc.getElementsByTagName("PVS-Studio_Analysis_Log");
private boolean errorCodeIsValid(final String errorCode) {
return !(errorCode.isEmpty() || errorCode.charAt(0) != 'V');
}

falseAlarmCount = 0;
failWarningsCount = 0;
static class PlogMessage {
private String file = StringUtils.EMPTY;
private int lineNumber = 0;
private String errorCode = StringUtils.EMPTY;
private String message = StringUtils.EMPTY;
private String level = StringUtils.EMPTY;

List<PlogMessage> plogMessages = new ArrayList<>();
for (int nodeCount = 0; nodeCount < nList.getLength(); nodeCount++) {
Node nNode = nList.item(nodeCount);
public String getHash() {
return errorCode + message + file + lineNumber;

Check warning on line 128 in src/main/java/edu/hm/hafner/analysis/parser/pvsstudio/PlogMessagesReader.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered line

Line 128 is not covered by tests
}

if (nNode.getNodeType() == Node.ELEMENT_NODE) {
processNode(plogMessages, nNode);
}
@Override
public String toString() {
return message;
}

if ((plogMessages.size() + falseAlarmCount) == 0 && failWarningsCount > 0) {
Logger.getLogger(PVSStudioParser.class.getName()).log(Level.SEVERE, "No messages were parsed!");
String getFilePath() {
return file;
}

return plogMessages;
public int getLine() {
return lineNumber;
}

public String getType() {
return errorCode;
}

public String getLevel() {
return level;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.parser.pvsstudio.PlogMessagesReader.PlogMessage;

/**
* A parser for the PVS-Studio static analyzer.
*
* @author PVS-Studio Team
*/
public class PVSStudioParser extends IssueParser {
public class PvsStudioParser extends IssueParser {
private static final long serialVersionUID = -7777775729854832128L;
private static final String SEVERITY_HIGH = "1";
private static final String SEVERITY_NORMAL = "2";
Expand All @@ -38,8 +39,9 @@ else if (SEVERITY_LOW.equals(level)) {
public Report parse(final ReaderFactory readerFactory) throws ParsingException, ParsingCanceledException {
try (IssueBuilder issueBuilder = new IssueBuilder()) {
var report = new Report();
var parser = new PlogMessagesReader();

for (PlogMessage plogMessage : PlogMessage.getMessagesFromReport(readerFactory)) {
for (PlogMessage plogMessage : parser.getMessagesFromReport(readerFactory)) {
issueBuilder.setFileName(plogMessage.getFilePath());

issueBuilder.setSeverity(getSeverity(plogMessage.getLevel()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.hm.hafner.analysis.registry;

import edu.hm.hafner.analysis.IssueParser;
import edu.hm.hafner.analysis.parser.pvsstudio.PVSStudioParser;
import edu.hm.hafner.analysis.parser.pvsstudio.PvsStudioParser;

/**
* A descriptor for the PVS-Studio static analyzer.
Expand All @@ -18,7 +18,7 @@ class PvsStudioDescriptor extends ParserDescriptor {

@Override
public IssueParser createParser(final Option... options) {
return new PVSStudioParser();
return new PvsStudioParser();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import edu.hm.hafner.analysis.registry.AbstractParserTest;

/**
* Tests the class {@link PVSStudioParser}.
* Tests the class {@link PvsStudioParser}.
*
* @author PVS-Studio Team
*/
class PVSStudioParserTest extends AbstractParserTest {
PVSStudioParserTest() {
class PvsStudioParserTest extends AbstractParserTest {
PvsStudioParserTest() {
super("TestReport.plog");
}

Expand Down Expand Up @@ -55,6 +55,6 @@ private String getFormedMessage(final String type, final String messageFromFile)

@Override
protected IssueParser createParser() {
return new PVSStudioParser();
return new PvsStudioParser();
}
}

0 comments on commit 40fd342

Please sign in to comment.