diff --git a/docs/pages/pmd/devdocs/major_contributions/adding_new_cpd_language.md b/docs/pages/pmd/devdocs/major_contributions/adding_new_cpd_language.md index 4d518453ba..7b0b185b26 100644 --- a/docs/pages/pmd/devdocs/major_contributions/adding_new_cpd_language.md +++ b/docs/pages/pmd/devdocs/major_contributions/adding_new_cpd_language.md @@ -132,4 +132,4 @@ public class DartTokenizerTest extends CpdTextComparisonTest { } } -``` \ No newline at end of file +``` diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java index 1544d2e512..efd893f774 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java @@ -222,7 +222,7 @@ public class PMD { // in case we analyzed files within Zip Files/Jars, we need to close them after // the analysis is finished - Exception closed = FileUtil.closeAll(files); + Exception closed = IOUtil.closeAll(files); if (closed != null) { if (ex != null) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/processor/PmdRunnable.java b/pmd-core/src/main/java/net/sourceforge/pmd/processor/PmdRunnable.java index abbd307a25..a9247cab61 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/processor/PmdRunnable.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/processor/PmdRunnable.java @@ -96,7 +96,7 @@ class PmdRunnable implements Runnable { TimeTracker.finishThread(); } - public void processSource(RuleContext ruleCtx, LanguageVersion languageVersion, RuleSets ruleSets) throws IOException, FileAnalysisException { + private void processSource(RuleContext ruleCtx, LanguageVersion languageVersion, RuleSets ruleSets) throws IOException, FileAnalysisException { String fullSource = DataSource.readToString(dataSource, configuration.getSourceEncoding()); String filename = dataSource.getNiceFileName(false, null); @@ -116,7 +116,7 @@ class PmdRunnable implements Runnable { } } - private RootNode parse(Parser parser, ParserTask task) throws IOException { + private RootNode parse(Parser parser, ParserTask task) { try (TimedOperation to = TimeTracker.startOperation(TimedOperationCategory.PARSER)) { return parser.parse(task); } @@ -127,7 +127,7 @@ class PmdRunnable implements Runnable { RuleSets ruleSets, RuleContext ctx, LanguageVersion languageVersion, - String filename) throws FileAnalysisException, IOException { + String filename) throws FileAnalysisException { ParserTask task = new ParserTask( languageVersion, diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/reporting/FileAnalysisListener.java b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/FileAnalysisListener.java index c842b832e8..a4590a7727 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/reporting/FileAnalysisListener.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/FileAnalysisListener.java @@ -13,7 +13,7 @@ import net.sourceforge.pmd.Report.ProcessingError; import net.sourceforge.pmd.Report.SuppressedViolation; import net.sourceforge.pmd.RuleViolation; import net.sourceforge.pmd.internal.util.AssertionUtil; -import net.sourceforge.pmd.util.FileUtil; +import net.sourceforge.pmd.util.IOUtil; /** * A handler for events occuring during analysis of a single file. Instances @@ -117,7 +117,7 @@ public interface FileAnalysisListener extends AutoCloseable { @Override public void close() throws Exception { - Exception composed = FileUtil.closeAll(list); + Exception composed = IOUtil.closeAll(list); if (composed != null) { throw composed; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/reporting/GlobalAnalysisListener.java b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/GlobalAnalysisListener.java index a455f550e7..40563b1de2 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/reporting/GlobalAnalysisListener.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/GlobalAnalysisListener.java @@ -21,7 +21,7 @@ import net.sourceforge.pmd.lang.ast.FileAnalysisException; import net.sourceforge.pmd.renderers.Renderer; import net.sourceforge.pmd.util.BaseResultProducingCloseable; import net.sourceforge.pmd.util.CollectionUtil; -import net.sourceforge.pmd.util.FileUtil; +import net.sourceforge.pmd.util.IOUtil; import net.sourceforge.pmd.util.datasource.DataSource; /** @@ -129,7 +129,7 @@ public interface GlobalAnalysisListener extends AutoCloseable { @Override public void close() throws Exception { - Exception composed = FileUtil.closeAll(myList); + Exception composed = IOUtil.closeAll(myList); if (composed != null) { throw composed; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/FileUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/FileUtil.java index 4ea35e9d30..ce4582e9e1 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/FileUtil.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/FileUtil.java @@ -43,32 +43,6 @@ public final class FileUtil { private FileUtil() { } - /** - * Close all closeable resources in order. If any exception occurs, - * it is saved and returned. If more than one exception occurs, the - * following are accumulated as suppressed violations. - * - * @param closeables Resources to close - * - * @return An exception, or null if no 'close' routine threw - */ - @SuppressWarnings("PMD.CloseResource") // false-positive - public static Exception closeAll(Collection closeables) { - Exception composed = null; - for (AutoCloseable it : closeables) { - try { - it.close(); - } catch (Exception e) { - if (composed == null) { - composed = e; - } else { - composed.addSuppressed(e); - } - } - } - return composed; - } - /** * Helper method to get a filename without its extension * diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/IOUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/IOUtil.java index 1ef21f6135..c24461a866 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/IOUtil.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/IOUtil.java @@ -17,6 +17,7 @@ import java.nio.charset.UnsupportedCharsetException; import java.nio.file.Files; import java.security.AccessController; import java.security.PrivilegedAction; +import java.util.Collection; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -97,4 +98,29 @@ public final class IOUtil { } } + /** + * Close all closeable resources in order. If any exception occurs, + * it is saved and returned. If more than one exception occurs, the + * following are accumulated as suppressed exceptions in the first. + * + * @param closeables Resources to close + * + * @return An exception, or null if no 'close' routine threw + */ + @SuppressWarnings("PMD.CloseResource") // false-positive + public static Exception closeAll(Collection closeables) { + Exception composed = null; + for (AutoCloseable it : closeables) { + try { + it.close(); + } catch (Exception e) { + if (composed == null) { + composed = e; + } else { + composed.addSuppressed(e); + } + } + } + return composed; + } } diff --git a/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/PLSQLXPathRuleTest.java b/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/PLSQLXPathRuleTest.java index 80cdfb7840..504540ae87 100644 --- a/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/PLSQLXPathRuleTest.java +++ b/pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/PLSQLXPathRuleTest.java @@ -17,7 +17,7 @@ import net.sourceforge.pmd.lang.rule.xpath.XPathVersion; */ public class PLSQLXPathRuleTest extends AbstractPLSQLParserTst { - private final String source = + private static final String SOURCE = "create or replace\n" + "package pkg_xpath_problem\n" + "AS\n" + " PROCEDURE pkg_minimal\n" + " IS\n" + " a_variable VARCHAR2(1);\n" + " BEGIN \n" + " --PRAGMA INLINE(output,'YES');\n" + " a_variable := 'Y' ;\n" + " END ;\n" + "end pkg_xpath_problem;\n" + "/\n"; @@ -29,7 +29,7 @@ public class PLSQLXPathRuleTest extends AbstractPLSQLParserTst { * See https://sourceforge.net/p/pmd/bugs/1166/ */ @Test - public void testXPathRule1() throws Exception { + public void testXPathRule1() { testOnVersion(XPathVersion.XPATH_1_0); } @@ -37,7 +37,7 @@ public class PLSQLXPathRuleTest extends AbstractPLSQLParserTst { * See https://sourceforge.net/p/pmd/bugs/1166/ */ @Test - public void testXPathRule1Compatibility() throws Exception { + public void testXPathRule1Compatibility() { testOnVersion(XPathVersion.XPATH_1_0_COMPATIBILITY); } @@ -45,17 +45,17 @@ public class PLSQLXPathRuleTest extends AbstractPLSQLParserTst { * See https://sourceforge.net/p/pmd/bugs/1166/ */ @Test - public void testXPathRule2() throws Exception { + public void testXPathRule2() { testOnVersion(XPathVersion.XPATH_2_0); } - private void testOnVersion(XPathVersion xpath10) throws Exception { + private void testOnVersion(XPathVersion xpath10) { XPathRule rule = new XPathRule(xpath10, "//PrimaryPrefix"); rule.setLanguage(LanguageRegistry.getLanguage(PLSQLLanguageModule.NAME)); rule.setMessage("Test Violation"); - Report report = plsql.executeRule(rule, source); + Report report = plsql.executeRule(rule, SOURCE); Assert.assertEquals(2, report.getViolations().size()); }