From a82e8e462dee9ef6e31bd8730fcf58de0e3fa894 Mon Sep 17 00:00:00 2001 From: gibarsin Date: Wed, 22 Apr 2020 22:58:08 -0700 Subject: [PATCH] Close data sources even if a runtime exception or error is thrown in processFiles --- .../pmd/processor/AbstractPMDProcessor.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/processor/AbstractPMDProcessor.java b/pmd-core/src/main/java/net/sourceforge/pmd/processor/AbstractPMDProcessor.java index f6518a2d2a..07552dd3e8 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/processor/AbstractPMDProcessor.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/processor/AbstractPMDProcessor.java @@ -110,27 +110,29 @@ public abstract class AbstractPMDProcessor { // this is done manually without a try-with-resources public void processFiles(RuleSetFactory ruleSetFactory, List files, RuleContext ctx, List renderers) { - final RuleSets rs = createRuleSets(ruleSetFactory, ctx.getReport()); - configuration.getAnalysisCache().checkValidity(rs, configuration.getClassLoader()); - final SourceCodeProcessor processor = new SourceCodeProcessor(configuration); + try { + final RuleSets rs = createRuleSets(ruleSetFactory, ctx.getReport()); + configuration.getAnalysisCache().checkValidity(rs, configuration.getClassLoader()); + final SourceCodeProcessor processor = new SourceCodeProcessor(configuration); - for (final DataSource dataSource : files) { - // this is the real, canonical and absolute filename (not shortened) - String realFileName = dataSource.getNiceFileName(false, null); + for (final DataSource dataSource : files) { + // this is the real, canonical and absolute filename (not shortened) + String realFileName = dataSource.getNiceFileName(false, null); - runAnalysis(new PmdRunnable(dataSource, realFileName, renderers, ctx, rs, processor)); - } + runAnalysis(new PmdRunnable(dataSource, realFileName, renderers, ctx, rs, processor)); + } - // render base report first - general errors - renderReports(renderers, ctx.getReport()); + // render base report first - general errors + renderReports(renderers, ctx.getReport()); - // then add analysis results per file - collectReports(renderers); - - // in case we analyzed files within Zip Files/Jars, we need to close them after - // the analysis is finished - for (DataSource dataSource : files) { - IOUtils.closeQuietly(dataSource); + // then add analysis results per file + collectReports(renderers); + } finally { + // in case we analyzed files within Zip Files/Jars, we need to close them after + // the analysis is finished + for (DataSource dataSource : files) { + IOUtils.closeQuietly(dataSource); + } } }