diff --git a/langtools/src/share/classes/com/sun/tools/javac/file/CloseableURLClassLoader.java b/langtools/src/share/classes/com/sun/tools/javac/file/CloseableURLClassLoader.java new file mode 100644 index 00000000000..a88a32cb097 --- /dev/null +++ b/langtools/src/share/classes/com/sun/tools/javac/file/CloseableURLClassLoader.java @@ -0,0 +1,105 @@ +/* + * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package com.sun.tools.javac.file; + +import java.io.Closeable; +import java.io.IOException; +import java.lang.reflect.Field; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.jar.JarFile; + +/** + * A URLClassLoader that also implements Closeable. + * Reflection is used to access internal data structures in the URLClassLoader, + * since no public API exists for this purpose. Therefore this code is somewhat + * fragile. Caveat emptor. + * @throws Error if the internal data structures are not as expected. + * + *
This is NOT part of any API supported by Sun Microsystems. If
+ * you write code that depends on this, you do so at your own risk.
+ * This code and its internal interfaces are subject to change or
+ * deletion without notice.
+ */
+class CloseableURLClassLoader
+ extends URLClassLoader implements Closeable {
+ CloseableURLClassLoader(URL[] urls, ClassLoader parent) throws Error {
+ super(urls, parent);
+ try {
+ getLoaders(); //proactive check that URLClassLoader is as expected
+ } catch (Throwable t) {
+ throw new Error("cannot create CloseableURLClassLoader", t);
+ }
+ }
+
+ /**
+ * Close any jar files that may have been opened by the class loader.
+ * Reflection is used to access the jar files in the URLClassLoader's
+ * internal data structures.
+ * @throws java.io.IOException if the jar files cannot be found for any
+ * reson, or if closing the jar file itself causes an IOException.
+ */
+ public void close() throws IOException {
+ try {
+ for (Object l: getLoaders()) {
+ if (l.getClass().getName().equals("sun.misc.URLClassPath$JarLoader")) {
+ Field jarField = l.getClass().getDeclaredField("jar");
+ JarFile jar = (JarFile) getField(l, jarField);
+ //System.err.println("CloseableURLClassLoader: closing " + jar);
+ jar.close();
+ }
+ }
+ } catch (Throwable t) {
+ IOException e = new IOException("cannot close class loader");
+ e.initCause(t);
+ throw e;
+ }
+ }
+
+ private ArrayList> getLoaders()
+ throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException
+ {
+ Field ucpField = URLClassLoader.class.getDeclaredField("ucp");
+ Object urlClassPath = getField(this, ucpField);
+ if (urlClassPath == null)
+ throw new AssertionError("urlClassPath not set in URLClassLoader");
+ Field loadersField = urlClassPath.getClass().getDeclaredField("loaders");
+ return (ArrayList>) getField(urlClassPath, loadersField);
+ }
+
+ private Object getField(Object o, Field f)
+ throws IllegalArgumentException, IllegalAccessException {
+ boolean prev = f.isAccessible();
+ try {
+ f.setAccessible(true);
+ return f.get(o);
+ } finally {
+ f.setAccessible(prev);
+ }
+ }
+
+}
diff --git a/langtools/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java b/langtools/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java
index 3ea1978b3ef..15a22400f6b 100644
--- a/langtools/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java
+++ b/langtools/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java
@@ -33,6 +33,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.lang.ref.SoftReference;
+import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
@@ -76,6 +77,7 @@ import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Options;
+import java.io.Closeable;
import static javax.tools.StandardLocation.*;
import static com.sun.tools.javac.main.OptionName.*;
@@ -131,6 +133,7 @@ public class JavacFileManager implements StandardJavaFileManager {
protected boolean mmappedIO;
protected boolean ignoreSymbolFile;
+ protected String classLoaderClass;
/**
* User provided charset (through javax.tools).
@@ -180,6 +183,7 @@ public class JavacFileManager implements StandardJavaFileManager {
mmappedIO = options.get("mmappedIO") != null;
ignoreSymbolFile = options.get("ignore.symbol.file") != null;
+ classLoaderClass = options.get("procloader");
}
public JavaFileObject getFileForInput(String name) {
@@ -747,8 +751,40 @@ public class JavacFileManager implements StandardJavaFileManager {
throw new AssertionError(e);
}
}
- return new URLClassLoader(lb.toArray(new URL[lb.size()]),
- getClass().getClassLoader());
+
+ URL[] urls = lb.toArray(new URL[lb.size()]);
+ ClassLoader thisClassLoader = getClass().getClassLoader();
+
+ // Bug: 6558476
+ // Ideally, ClassLoader should be Closeable, but before JDK7 it is not.
+ // On older versions, try the following, to get a closeable classloader.
+
+ // 1: Allow client to specify the class to use via hidden option
+ if (classLoaderClass != null) {
+ try {
+ Class extends ClassLoader> loader =
+ Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
+ Class>[] constrArgTypes = { URL[].class, ClassLoader.class };
+ Constructor extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
+ return constr.newInstance(new Object[] { urls, thisClassLoader });
+ } catch (Throwable t) {
+ // ignore errors loading user-provided class loader, fall through
+ }
+ }
+
+ // 2: If URLClassLoader implements Closeable, use that.
+ if (Closeable.class.isAssignableFrom(URLClassLoader.class))
+ return new URLClassLoader(urls, thisClassLoader);
+
+ // 3: Try using private reflection-based CloseableURLClassLoader
+ try {
+ return new CloseableURLClassLoader(urls, thisClassLoader);
+ } catch (Throwable t) {
+ // ignore errors loading workaround class loader, fall through
+ }
+
+ // 4: If all else fails, use plain old standard URLClassLoader
+ return new URLClassLoader(urls, thisClassLoader);
}
public Iterable