8210009: Source Launcher classloader should support getResource and getResourceAsStream
Reviewed-by: mchung, plevart
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/Main.java Thu Sep 06 12:10:59 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/Main.java Thu Sep 06 16:15:32 2018 -0700
@@ -27,8 +27,11 @@
import java.io.BufferedInputStream;
import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@@ -37,20 +40,28 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
+import java.util.NoSuchElementException;
import java.util.ResourceBundle;
import javax.lang.model.SourceVersion;
@@ -59,7 +70,6 @@
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
-import javax.tools.JavaFileManager.Location;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
@@ -531,6 +541,10 @@
* {@code findClass} to find classes in the in-memory cache.
*/
private static class MemoryClassLoader extends ClassLoader {
+ /**
+ * The map of classes known to this class loader, indexed by
+ * {@link ClassLoader#name binary name}.
+ */
private final Map<String, byte[]> map;
MemoryClassLoader(Map<String, byte[]> map, ClassLoader parent) {
@@ -546,5 +560,117 @@
}
return defineClass(name, bytes, 0, bytes.length);
}
+
+ @Override
+ public URL findResource(String name) {
+ String binaryName = toBinaryName(name);
+ if (binaryName == null || map.get(binaryName) == null) {
+ return null;
+ }
+
+ URLStreamHandler handler = this.handler;
+ if (handler == null) {
+ this.handler = handler = new MemoryURLStreamHandler();
+ }
+
+ try {
+ return new URL(PROTOCOL, null, -1, name, handler);
+ } catch (MalformedURLException e) {
+ return null;
+ }
+ }
+
+ @Override
+ public Enumeration<URL> findResources(String name) {
+ return new Enumeration<URL>() {
+ private URL next = findResource(name);
+
+ @Override
+ public boolean hasMoreElements() {
+ return (next != null);
+ }
+
+ @Override
+ public URL nextElement() {
+ if (next == null) {
+ throw new NoSuchElementException();
+ }
+ URL u = next;
+ next = null;
+ return u;
+ }
+ };
+ }
+
+ /**
+ * Converts a "resource name" (as used in the getResource* methods)
+ * to a binary name if the name identifies a class, or null otherwise.
+ * @param name the resource name
+ * @return the binary name
+ */
+ private String toBinaryName(String name) {
+ if (!name.endsWith(".class")) {
+ return null;
+ }
+ return name.substring(0, name.length() - DOT_CLASS_LENGTH).replace('/', '.');
+ }
+
+ private static final int DOT_CLASS_LENGTH = ".class".length();
+ private final String PROTOCOL = "sourcelauncher-" + getClass().getSimpleName() + hashCode();
+ private URLStreamHandler handler;
+
+ /**
+ * A URLStreamHandler for use with URLs returned by MemoryClassLoader.getResource.
+ */
+ private class MemoryURLStreamHandler extends URLStreamHandler {
+ @Override
+ public URLConnection openConnection(URL u) {
+ if (!u.getProtocol().equalsIgnoreCase(PROTOCOL)) {
+ throw new IllegalArgumentException(u.toString());
+ }
+ return new MemoryURLConnection(u, map.get(toBinaryName(u.getPath())));
+ }
+
+ }
+
+ /**
+ * A URLConnection for use with URLs returned by MemoryClassLoader.getResource.
+ */
+ private static class MemoryURLConnection extends URLConnection {
+ private byte[] bytes;
+ private InputStream in;
+
+ MemoryURLConnection(URL u, byte[] bytes) {
+ super(u);
+ this.bytes = bytes;
+ }
+
+ @Override
+ public void connect() throws IOException {
+ if (!connected) {
+ if (bytes == null) {
+ throw new FileNotFoundException(getURL().getPath());
+ }
+ in = new ByteArrayInputStream(bytes);
+ connected = true;
+ }
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ connect();
+ return in;
+ }
+
+ @Override
+ public long getContentLengthLong() {
+ return bytes.length;
+ }
+
+ @Override
+ public String getContentType() {
+ return "application/octet-stream";
+ }
+ }
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/launcher/GetResourceTest.java Thu Sep 06 16:15:32 2018 -0700
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. 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.
+ *
+ * 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8210009
+ * @summary Source Launcher classloader should support getResource and getResourceAsStream
+ * @modules jdk.compiler jdk.jdeps
+ * @library /tools/lib
+ * @build toolbox.JavaTask toolbox.ToolBox
+ * @run main GetResourceTest
+ */
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import toolbox.JavaTask;
+import toolbox.Task;
+import toolbox.ToolBox;
+
+/*
+ * The body of this test is in ${test.src}/src/CLTest.java,
+ * which is executed in single-file source-launcher mode,
+ * in order to test the classloader used to launch such programs.
+ */
+public class GetResourceTest {
+ public static void main(String... args) throws Exception {
+ GetResourceTest t = new GetResourceTest();
+ t.run();
+ }
+
+ void run() throws Exception {
+ ToolBox tb = new ToolBox();
+ Path file = Paths.get(tb.testSrc).resolve("src").resolve("CLTest.java");
+ new JavaTask(tb)
+ .vmOptions("--add-modules", "jdk.jdeps",
+ "--add-exports", "jdk.jdeps/com.sun.tools.classfile=ALL-UNNAMED")
+ .className(file.toString()) // implies source file mode
+ .run(Task.Expect.SUCCESS)
+ .writeAll();
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/launcher/src/CLTest.java Thu Sep 06 16:15:32 2018 -0700
@@ -0,0 +1,181 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. 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.
+ *
+ * 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * This class is intended to be run in the single source-file launcher
+ * mode defined by JEP 330. It checks the operation of the getResource*
+ * methods provided by the MemoryClassLoader used to run the compiled
+ * classes.
+ *
+ * The class uses the ClassFile library to validate the contents of
+ * the URLs and streams returned by the methods being tested.
+ *
+ * $ java \
+ * --add-modules jdk.jdeps \
+ * --add-exports jdk.jdeps/com.sun.tools.classfile=ALL-UNNAMED
+ * /path/to/CLTest.java
+ */
+package p.q;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import com.sun.tools.classfile.ClassFile;
+
+public class CLTest {
+ public static void main(String... args) throws Exception {
+ try {
+ new CLTest().run();
+ } catch (Throwable t) {
+ t.printStackTrace();
+ System.exit(1);
+ }
+ }
+
+ void run() throws Exception {
+ String[] names = {
+ "p/q/CLTest.class",
+ "p/q/CLTest$Inner.class",
+ "p/q/CLTest2.class",
+ "java/lang/Object.class",
+ "UNKNOWN.class",
+ "UNKNOWN"
+ };
+
+ for (String name : names) {
+ testGetResource(name);
+ testGetResources(name);
+ testGetResourceAsStream(name);
+ }
+
+ if (errors > 0) {
+ throw new Exception(errors + " errors found");
+ }
+ }
+
+ void testGetResource(String name) {
+ System.err.println("testGetResource: " + name);
+ try {
+ ClassLoader cl = getClass().getClassLoader();
+ URL u = cl.getResource(name);
+ if (u == null) {
+ if (!name.contains("UNKNOWN")) {
+ error("resource not found: " + name);
+ }
+ return;
+ }
+
+ checkURL(u);
+ checkClass(name, u);
+
+ } catch (Throwable t) {
+ t.printStackTrace(System.err);
+ error("unexpected exception: " + t);
+ }
+ }
+
+ void testGetResources(String name) {
+ System.err.println("testGetResources: " + name);
+ try {
+ ClassLoader cl = getClass().getClassLoader();
+ Enumeration<URL> e = cl.getResources(name);
+ List<URL> list = new ArrayList<>();
+ while (e.hasMoreElements()) {
+ list.add(e.nextElement());
+ }
+
+ switch (list.size()) {
+ case 0:
+ if (!name.contains("UNKNOWN")) {
+ error("resource not found: " + name);
+ }
+ break;
+
+ case 1:
+ checkClass(name, list.get(0));
+ break;
+
+ default:
+ error("unexpected resources found: " + list);
+ }
+
+ } catch (Throwable t) {
+ t.printStackTrace(System.err);
+ error("unexpected exception: " + t);
+ }
+ }
+
+ void testGetResourceAsStream(String name) {
+ System.err.println("testGetResourceAsStream: " + name);
+ try {
+ ClassLoader cl = getClass().getClassLoader();
+ try (InputStream in = cl.getResourceAsStream(name)) {
+ if (in == null) {
+ if (!name.contains("UNKNOWN")) {
+ error("resource not found: " + name);
+ }
+ return;
+ }
+
+ checkClass(name, in);
+ }
+ } catch (Throwable t) {
+ t.printStackTrace(System.err);
+ error("unexpected exception: " + t);
+ }
+ }
+
+ void checkClass(String name, URL u) throws Exception {
+ try (InputStream in = u.openConnection().getInputStream()) {
+ checkClass(name, in);
+ }
+ }
+
+ void checkClass(String name, InputStream in) throws Exception {
+ ClassFile cf = ClassFile.read(in);
+ System.err.println(" class " + cf.getName());
+ if (!name.equals(cf.getName() + ".class")) {
+ error("unexpected class found: " + cf.getName());
+ }
+ }
+
+ void checkURL(URL url) {
+ try {
+ // verify the URL is formatted strictly according to RFC2396
+ url.toURI();
+ } catch (URISyntaxException e) {
+ error("bad URL: " + url + "; " + e);
+ }
+ }
+
+ void error(String message) {
+ System.err.println("Error: " + message);
+ errors++;
+ }
+
+ int errors = 0;
+
+ class Inner { }
+}
+
+class CLTest2 { }