From 293731f009f5c0f274216d118487769ef0fd0a20 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Mon, 25 Jul 2016 21:34:40 +0200 Subject: [PATCH] Fixes #1508 [core] [java] PMD is leaking file handles Closes class loader after ant task is finished --- .../pmd/ant/internal/PMDTaskImpl.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/ant/internal/PMDTaskImpl.java b/pmd-core/src/main/java/net/sourceforge/pmd/ant/internal/PMDTaskImpl.java index e5bd978549..0c91f74439 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/ant/internal/PMDTaskImpl.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/ant/internal/PMDTaskImpl.java @@ -3,6 +3,7 @@ */ package net.sourceforge.pmd.ant.internal; +import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.PrintWriter; @@ -14,6 +15,14 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Handler; import java.util.logging.Level; +import org.apache.commons.io.IOUtils; +import org.apache.tools.ant.AntClassLoader; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.Path; + import net.sourceforge.pmd.PMD; import net.sourceforge.pmd.PMDConfiguration; import net.sourceforge.pmd.Report; @@ -37,14 +46,6 @@ import net.sourceforge.pmd.util.datasource.FileDataSource; import net.sourceforge.pmd.util.log.AntLogHandler; import net.sourceforge.pmd.util.log.ScopedLogHandlersManager; -import org.apache.commons.io.IOUtils; -import org.apache.tools.ant.AntClassLoader; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.types.FileSet; -import org.apache.tools.ant.types.Path; - public class PMDTaskImpl { private Path classpath; @@ -267,6 +268,13 @@ public class PMDTaskImpl { doTask(); } finally { logManager.close(); + tryCloseClassLoader(configuration.getClassLoader()); + } + } + + private void tryCloseClassLoader(ClassLoader classLoader) { + if (classLoader instanceof Closeable) { + IOUtils.closeQuietly((Closeable)classLoader); } }