dogfood: Upgrade to PMD 6.17.0 and fix CloseResource issues

This commit is contained in:
Andreas Dangel
2019-07-31 20:18:42 +02:00
parent e4ce2faf78
commit 395e3453cb
22 changed files with 83 additions and 69 deletions

View File

@ -503,6 +503,7 @@ public class PMD {
final TimingReportRenderer renderer = new TextTimingReportRenderer();
try {
// Don't close this writer, we don't want to close stderr
@SuppressWarnings("PMD.CloseResource")
final Writer writer = new OutputStreamWriter(System.err);
renderer.render(timingReport, writer);
} catch (final IOException e) {

View File

@ -124,6 +124,8 @@ public abstract class AbstractAnalysisCache implements AnalysisCache {
final long currentAuxClassPathChecksum;
if (auxclassPathClassLoader instanceof URLClassLoader) {
// we don't want to close our aux classpath loader - we still need it...
@SuppressWarnings("PMD.CloseResource")
final URLClassLoader urlClassLoader = (URLClassLoader) auxclassPathClassLoader;
currentAuxClassPathChecksum = computeClassPathHash(urlClassLoader.getURLs());

View File

@ -133,6 +133,8 @@ public class CPDTask extends Task {
CPDRenderer renderer = createRenderer();
try {
@SuppressWarnings("PMD.CloseResource")
// will be closed via BufferedWriter/OutputStreamWriter chain down below
final OutputStream os;
if (outputFile == null) {
os = System.out;

View File

@ -11,6 +11,8 @@ import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Rule;
@ -24,6 +26,7 @@ import net.sourceforge.pmd.benchmark.TimedOperation;
import net.sourceforge.pmd.benchmark.TimedOperationCategory;
import net.sourceforge.pmd.renderers.Renderer;
import net.sourceforge.pmd.util.datasource.DataSource;
import net.sourceforge.pmd.util.datasource.ZipDataSource;
/**
* @author Romain Pelisse <belaran@gmail.com>
@ -116,6 +119,13 @@ public abstract class AbstractPMDProcessor {
runAnalysis(new PmdRunnable(dataSource, realFileName, renderers, ctx, rs, processor));
}
// in case we analyzed files within Zip Files/Jars, we need to close them now...
for (DataSource dataSource : files) {
if (dataSource instanceof ZipDataSource) {
IOUtils.closeQuietly((ZipDataSource) dataSource);
}
}
// render base report first - general errors
renderReports(renderers, ctx.getReport());

View File

@ -9,7 +9,6 @@ import static net.sourceforge.pmd.renderers.CodeClimateRule.CODECLIMATE_CATEGORI
import static net.sourceforge.pmd.renderers.CodeClimateRule.CODECLIMATE_REMEDIATION_MULTIPLIER;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@ -54,7 +53,6 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer {
@Override
public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
Writer writer = getWriter();
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
while (violations.hasNext()) {

View File

@ -5,7 +5,6 @@
package net.sourceforge.pmd.renderers;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import net.sourceforge.pmd.RuleViolation;
@ -30,7 +29,6 @@ public class EmacsRenderer extends AbstractIncrementingRenderer {
@Override
public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
Writer writer = getWriter();
StringBuilder buf = new StringBuilder();
while (violations.hasNext()) {
RuleViolation rv = violations.next();

View File

@ -78,7 +78,6 @@ public class HTMLRenderer extends AbstractIncrementingRenderer {
@Override
public void start() throws IOException {
Writer writer = getWriter();
writer.write("<html><head><title>PMD</title></head><body>" + PMD.EOL);
writer.write("<center><h3>PMD report</h3></center>");
writer.write("<center><h3>Problems found</h3></center>");
@ -88,13 +87,11 @@ public class HTMLRenderer extends AbstractIncrementingRenderer {
@Override
public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
Writer writer = getWriter();
glomRuleViolations(writer, violations);
}
@Override
public void end() throws IOException {
Writer writer = getWriter();
writer.write("</table>");
glomProcessingErrors(writer, errors);
if (showSuppressedViolations) {

View File

@ -51,7 +51,6 @@ public class IDEAJRenderer extends AbstractIncrementingRenderer {
classAndMethodName = getProperty(CLASS_AND_METHOD_NAME);
fileName = getProperty(FILE_NAME);
Writer writer = getWriter();
if (".method".equals(classAndMethodName)) {
// working on a directory tree
renderDirectoy(writer, violations);

View File

@ -5,7 +5,6 @@
package net.sourceforge.pmd.renderers;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import net.sourceforge.pmd.PMD;
@ -51,7 +50,6 @@ public class TextPadRenderer extends AbstractIncrementingRenderer {
@Override
public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
Writer writer = getWriter();
StringBuffer buf = new StringBuffer();
while (violations.hasNext()) {
RuleViolation rv = violations.next();

View File

@ -5,7 +5,6 @@
package net.sourceforge.pmd.renderers;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import net.sourceforge.pmd.PMD;
@ -30,7 +29,6 @@ public class TextRenderer extends AbstractIncrementingRenderer {
@Override
public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
Writer writer = getWriter();
StringBuilder buf = new StringBuilder();
while (violations.hasNext()) {
@ -45,7 +43,6 @@ public class TextRenderer extends AbstractIncrementingRenderer {
@Override
public void end() throws IOException {
Writer writer = getWriter();
StringBuilder buf = new StringBuilder(500);
for (Report.ProcessingError error : errors) {

View File

@ -5,7 +5,6 @@
package net.sourceforge.pmd.renderers;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import net.sourceforge.pmd.PMD;
@ -41,7 +40,6 @@ public class VBHTMLRenderer extends AbstractIncrementingRenderer {
return;
}
Writer writer = getWriter();
StringBuilder sb = new StringBuilder(500);
String filename = null;
String lineSep = PMD.EOL;
@ -84,7 +82,6 @@ public class VBHTMLRenderer extends AbstractIncrementingRenderer {
@Override
public void end() throws IOException {
Writer writer = getWriter();
StringBuilder sb = new StringBuilder();
writer.write("<br>");

View File

@ -5,7 +5,6 @@
package net.sourceforge.pmd.renderers;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
@ -51,7 +50,6 @@ public class XMLRenderer extends AbstractIncrementingRenderer {
useUTF8 = true;
}
Writer writer = getWriter();
StringBuilder buf = new StringBuilder(500);
buf.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>").append(PMD.EOL);
createVersionAttr(buf);
@ -64,7 +62,6 @@ public class XMLRenderer extends AbstractIncrementingRenderer {
@Override
public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
Writer writer = getWriter();
StringBuilder buf = new StringBuilder(500);
String filename = null;
@ -117,7 +114,6 @@ public class XMLRenderer extends AbstractIncrementingRenderer {
@Override
public void end() throws IOException {
Writer writer = getWriter();
StringBuilder buf = new StringBuilder(500);
// errors
for (Report.ProcessingError pe : errors) {

View File

@ -85,7 +85,10 @@ public class XSLTRenderer extends XMLRenderer {
if (xslt == null) {
throw new FileNotFoundException("Can't find XSLT file: " + this.xsltFilename);
}
this.prepareTransformer(xslt);
try (InputStream stream = xslt) {
this.prepareTransformer(stream);
}
// Now we build the XML file
super.start();
}
@ -115,11 +118,9 @@ public class XSLTRenderer extends XMLRenderer {
// First we finish the XML report
super.end();
// Now we transform it using XSLT
Writer writer = super.getWriter();
if (writer instanceof StringWriter) {
StringWriter w = (StringWriter) writer;
StringBuffer buffer = w.getBuffer();
Document doc = this.getDocument(buffer.toString());
Document doc = this.getDocument(w.toString());
this.transform(doc);
} else {
// Should not happen !

View File

@ -100,6 +100,9 @@ public final class FileUtil {
}
if (!file.isDirectory()) {
if (fileLocation.endsWith(".zip") || fileLocation.endsWith(".jar")) {
@SuppressWarnings("PMD.CloseResource")
// the zip file can't be closed here, it needs to be closed at the end of the PMD run
// see net.sourceforge.pmd.processor.AbstractPMDProcessor#processFiles(...)
ZipFile zipFile;
try {
zipFile = new ZipFile(fileLocation);

View File

@ -4,6 +4,7 @@
package net.sourceforge.pmd.util.datasource;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
@ -12,7 +13,7 @@ import java.util.zip.ZipFile;
/**
* DataSource implementation to read data from an entry in a zip or jar file.
*/
public class ZipDataSource implements DataSource {
public class ZipDataSource implements DataSource, Closeable {
private final ZipFile zipFile;
private final ZipEntry zipEntry;
@ -69,6 +70,7 @@ public class ZipDataSource implements DataSource {
if (getClass() != obj.getClass()) {
return false;
}
@SuppressWarnings("PMD.CloseResource")
ZipDataSource other = (ZipDataSource) obj;
if (zipEntry == null) {
if (other.zipEntry != null) {
@ -86,4 +88,11 @@ public class ZipDataSource implements DataSource {
}
return true;
}
@Override
public void close() throws IOException {
if (zipFile != null) {
zipFile.close();
}
}
}

View File

@ -567,11 +567,10 @@ public class Designer implements ClipboardOwner {
final RuleSet rs = new RuleSetFactory().createSingleRuleRuleSet(dfaGraphRule);
RuleContext ctx = new RuleContext();
ctx.setSourceCodeFile(new File("[no filename]." + languageVersion.getLanguage().getExtensions().get(0)));
StringReader reader = new StringReader(codeEditorPane.getText());
PMDConfiguration config = new PMDConfiguration();
config.setDefaultLanguageVersion(languageVersion);
try {
try (StringReader reader = new StringReader(codeEditorPane.getText())) {
new SourceCodeProcessor(config).processSourceCode(reader, new RuleSets(rs), ctx);
// } catch (PMDException pmde) {
// loadTreeData(new ExceptionNode(pmde));

View File

@ -51,23 +51,24 @@ public class CPPTokenizer extends JavaCCTokenizer {
return test;
}
BufferedReader reader = new BufferedReader(new StringReader(test));
StringBuilder filtered = new StringBuilder(test.length());
String line;
boolean skip = false;
while ((line = reader.readLine()) != null) {
if (skipBlocksStart.equalsIgnoreCase(line.trim())) {
skip = true;
} else if (skip && skipBlocksEnd.equalsIgnoreCase(line.trim())) {
skip = false;
try (BufferedReader reader = new BufferedReader(new StringReader(test))) {
StringBuilder filtered = new StringBuilder(test.length());
String line;
boolean skip = false;
while ((line = reader.readLine()) != null) {
if (skipBlocksStart.equalsIgnoreCase(line.trim())) {
skip = true;
} else if (skip && skipBlocksEnd.equalsIgnoreCase(line.trim())) {
skip = false;
}
if (!skip) {
filtered.append(line);
}
// always add a new line to keep the line-numbering
filtered.append(PMD.EOL);
}
if (!skip) {
filtered.append(line);
}
// always add a new line to keep the line-numbering
filtered.append(PMD.EOL);
return filtered.toString();
}
return filtered.toString();
}
@Override

View File

@ -164,9 +164,9 @@ public class AbstractLanguageVersionTest {
String[] rulesets = rulesetFilenames.split(",");
for (String r : rulesets) {
InputStream stream = rl.loadClassPathResourceAsStream(r);
assertNotNull(stream);
stream.close();
try (InputStream stream = rl.loadClassPathResourceAsStream(r)) {
assertNotNull(stream);
}
RuleSet ruleset = factory.createRuleSet(r);
assertNotNull(ruleset);
}

View File

@ -267,12 +267,13 @@ public abstract class AbstractRuleSetFactoryTest {
private boolean validateAgainstSchema(String fileName)
throws IOException, RuleSetNotFoundException, ParserConfigurationException, SAXException {
InputStream inputStream = loadResourceAsStream(fileName);
boolean valid = validateAgainstSchema(inputStream);
if (!valid) {
System.err.println("Validation against XML Schema failed for: " + fileName);
try (InputStream inputStream = loadResourceAsStream(fileName)) {
boolean valid = validateAgainstSchema(inputStream);
if (!valid) {
System.err.println("Validation against XML Schema failed for: " + fileName);
}
return valid;
}
return valid;
}
private boolean validateAgainstSchema(InputStream inputStream)
@ -285,12 +286,13 @@ public abstract class AbstractRuleSetFactoryTest {
private boolean validateAgainstDtd(String fileName)
throws IOException, RuleSetNotFoundException, ParserConfigurationException, SAXException {
InputStream inputStream = loadResourceAsStream(fileName);
boolean valid = validateAgainstDtd(inputStream);
if (!valid) {
System.err.println("Validation against DTD failed for: " + fileName);
try (InputStream inputStream = loadResourceAsStream(fileName)) {
boolean valid = validateAgainstDtd(inputStream);
if (!valid) {
System.err.println("Validation against DTD failed for: " + fileName);
}
return valid;
}
return valid;
}
private boolean validateAgainstDtd(InputStream inputStream)
@ -316,23 +318,22 @@ public abstract class AbstractRuleSetFactoryTest {
file = "<?xml version=\"1.0\"?>" + PMD.EOL + "<!DOCTYPE ruleset>" + PMD.EOL + file;
}
InputStream modifiedStream = new ByteArrayInputStream(file.getBytes());
saxParser.parse(modifiedStream, validateDefaultHandler.resetValid());
modifiedStream.close();
try (InputStream modifiedStream = new ByteArrayInputStream(file.getBytes())) {
saxParser.parse(modifiedStream, validateDefaultHandler.resetValid());
}
return validateDefaultHandler.isValid();
}
private String readFullyToString(InputStream inputStream) throws IOException {
StringBuilder buf = new StringBuilder(64 * 1024);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append(PMD.EOL);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append(PMD.EOL);
}
return buf.toString();
}
reader.close();
return buf.toString();
}
private static InputStream loadResourceAsStream(String resource) throws RuleSetNotFoundException {

View File

@ -12,6 +12,7 @@ import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
@ -54,12 +55,15 @@ public abstract class BaseCLITest {
@After
public void tearDown() {
IOUtils.closeQuietly(System.out);
System.setOut(originalOut);
System.setErr(originalErr);
}
protected void createTestOutputFile(String filename) {
try {
@SuppressWarnings("PMD.CloseResource")
PrintStream out = new PrintStream(Files.newOutputStream(new File(filename).toPath()));
System.setOut(out);
System.setErr(out);

View File

@ -155,6 +155,7 @@ public class AbstractVmNode extends AbstractNode implements VmNode {
*/
@Deprecated
public void dump(final String prefix, final boolean recurse, final Writer writer) {
@SuppressWarnings("PMD.CloseResource")
final PrintWriter printWriter = writer instanceof PrintWriter ? (PrintWriter) writer : new PrintWriter(writer);
printWriter.println(toString(prefix));
if (children != null && recurse) {

View File

@ -104,7 +104,7 @@ Objective-C, Perl, PHP, PLSQL, Python, Ruby, Salesforce.com Apex, Scala, Swift a
<argLine>-Xmx512m -Dfile.encoding=${project.build.sourceEncoding}</argLine>
<pmd.build-tools.version>3</pmd.build-tools.version>
<pmd.build-tools.version>4-SNAPSHOT</pmd.build-tools.version>
<pmd-designer.version>6.17.0</pmd-designer.version>
</properties>
@ -361,12 +361,12 @@ Objective-C, Perl, PHP, PLSQL, Python, Ruby, Salesforce.com Apex, Scala, Swift a
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
<version>6.15.0</version>
<version>6.17.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>6.15.0</version>
<version>6.17.0</version>
</dependency>
<!-- This contains the dogfood ruleset -->
<dependency>