changeset 4137:1faee09b8da1

8179531: JShell: fails to provide bytecode for dynamically created lambdas Summary: Adding support for getResource(s) to the JShell's ClassLoader Reviewed-by: psandoz, rfield
author jlahoda
date Tue, 09 May 2017 12:22:15 +0200
parents 5daed0e904ac
children 0e522ff8b9f5
files src/jdk.jshell/share/classes/jdk/jshell/execution/DefaultLoaderDelegate.java src/jdk.jshell/share/classes/jdk/jshell/execution/DirectExecutionControl.java src/jdk.jshell/share/classes/jdk/jshell/execution/JdiExecutionControl.java src/jdk.jshell/share/classes/jdk/jshell/execution/LoaderDelegate.java src/jdk.jshell/share/classes/jdk/jshell/execution/RemoteExecutionControl.java test/jdk/jshell/GetResourceTest.java
diffstat 6 files changed, 361 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jshell/share/classes/jdk/jshell/execution/DefaultLoaderDelegate.java	Thu May 04 17:55:07 2017 +0000
+++ b/src/jdk.jshell/share/classes/jdk/jshell/execution/DefaultLoaderDelegate.java	Tue May 09 12:22:15 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, 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
@@ -24,17 +24,35 @@
  */
 package jdk.jshell.execution;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
 import java.security.CodeSource;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
-import java.util.TreeMap;
+
 import jdk.jshell.spi.ExecutionControl.ClassBytecodes;
 import jdk.jshell.spi.ExecutionControl.ClassInstallException;
 import jdk.jshell.spi.ExecutionControl.EngineTerminationException;
 import jdk.jshell.spi.ExecutionControl.InternalException;
-import jdk.jshell.spi.ExecutionControl.NotImplementedException;
 
 /**
  * The standard implementation of {@link LoaderDelegate} using
@@ -45,27 +63,141 @@
 class DefaultLoaderDelegate implements LoaderDelegate {
 
     private final RemoteClassLoader loader;
-    private final Map<String, Class<?>> klasses = new TreeMap<>();
+    private final Map<String, Class<?>> klasses = new HashMap<>();
 
-    class RemoteClassLoader extends URLClassLoader {
+    private static class RemoteClassLoader extends URLClassLoader {
 
-        private final Map<String, byte[]> classObjects = new TreeMap<>();
+        private final Map<String, ClassFile> classFiles = new HashMap<>();
 
         RemoteClassLoader() {
             super(new URL[0]);
         }
 
-        void delare(String name, byte[] bytes) {
-            classObjects.put(name, bytes);
+        private class ResourceURLStreamHandler extends URLStreamHandler {
+
+            private final String name;
+
+            ResourceURLStreamHandler(String name) {
+                this.name = name;
+            }
+
+            @Override
+            protected URLConnection openConnection(URL u) throws IOException {
+                return new URLConnection(u) {
+                    private InputStream in;
+                    private Map<String, List<String>> fields;
+                    private List<String> fieldNames;
+
+                    @Override
+                    public void connect() {
+                        if (connected) {
+                            return;
+                        }
+                        connected = true;
+                        ClassFile file = classFiles.get(name);
+                        in = new ByteArrayInputStream(file.data);
+                        fields = new LinkedHashMap<>();
+                        fields.put("content-length", List.of(Integer.toString(file.data.length)));
+                        Instant instant = new Date(file.timestamp).toInstant();
+                        ZonedDateTime time = ZonedDateTime.ofInstant(instant, ZoneId.of("GMT"));
+                        String timeStamp = DateTimeFormatter.RFC_1123_DATE_TIME.format(time);
+                        fields.put("date", List.of(timeStamp));
+                        fields.put("last-modified", List.of(timeStamp));
+                        fieldNames = new ArrayList<>(fields.keySet());
+                    }
+
+                    @Override
+                    public InputStream getInputStream() throws IOException {
+                        connect();
+                        return in;
+                    }
+
+                    @Override
+                    public String getHeaderField(String name) {
+                        connect();
+                        return fields.getOrDefault(name, List.of())
+                                     .stream()
+                                     .findFirst()
+                                     .orElse(null);
+                    }
+
+                    @Override
+                    public Map<String, List<String>> getHeaderFields() {
+                        connect();
+                        return fields;
+                    }
+
+                    @Override
+                    public String getHeaderFieldKey(int n) {
+                        return n < fieldNames.size() ? fieldNames.get(n) : null;
+                    }
+
+                    @Override
+                    public String getHeaderField(int n) {
+                        String name = getHeaderFieldKey(n);
+
+                        return name != null ? getHeaderField(name) : null;
+                    }
+
+                };
+            }
+        }
+
+        void declare(String name, byte[] bytes) {
+            classFiles.put(toResourceString(name), new ClassFile(bytes, System.currentTimeMillis()));
         }
 
         @Override
         protected Class<?> findClass(String name) throws ClassNotFoundException {
-            byte[] b = classObjects.get(name);
-            if (b == null) {
+            ClassFile file = classFiles.get(toResourceString(name));
+            if (file == null) {
                 return super.findClass(name);
             }
-            return super.defineClass(name, b, 0, b.length, (CodeSource) null);
+            return super.defineClass(name, file.data, 0, file.data.length, (CodeSource) null);
+        }
+
+        @Override
+        public URL findResource(String name) {
+            URL u = doFindResource(name);
+            return u != null ? u : super.findResource(name);
+        }
+
+        @Override
+        public Enumeration<URL> findResources(String name) throws IOException {
+            URL u = doFindResource(name);
+            Enumeration<URL> sup = super.findResources(name);
+
+            if (u == null) {
+                return sup;
+            }
+
+            List<URL> result = new ArrayList<>();
+
+            while (sup.hasMoreElements()) {
+                result.add(sup.nextElement());
+            }
+
+            result.add(u);
+
+            return Collections.enumeration(result);
+        }
+
+        private URL doFindResource(String name) {
+            if (classFiles.containsKey(name)) {
+                try {
+                    return new URL(null,
+                                   new URI("jshell", null, "/" + name, null).toString(),
+                                   new ResourceURLStreamHandler(name));
+                } catch (MalformedURLException | URISyntaxException ex) {
+                    throw new InternalError(ex);
+                }
+            }
+
+            return null;
+        }
+
+        private String toResourceString(String className) {
+            return className.replace('.', '/') + ".class";
         }
 
         @Override
@@ -73,6 +205,16 @@
             super.addURL(url);
         }
 
+        private static class ClassFile {
+            public final byte[] data;
+            public final long timestamp;
+
+            ClassFile(byte[] data, long timestamp) {
+                this.data = data;
+                this.timestamp = timestamp;
+            }
+
+        }
     }
 
     public DefaultLoaderDelegate() {
@@ -86,7 +228,7 @@
         boolean[] loaded = new boolean[cbcs.length];
         try {
             for (ClassBytecodes cbc : cbcs) {
-                loader.delare(cbc.name(), cbc.bytecodes());
+                loader.declare(cbc.name(), cbc.bytecodes());
             }
             for (int i = 0; i < cbcs.length; ++i) {
                 ClassBytecodes cbc = cbcs[i];
@@ -101,6 +243,12 @@
         }
     }
 
+    @Override
+    public void classesRedefined(ClassBytecodes[] cbcs) {
+        for (ClassBytecodes cbc : cbcs) {
+            loader.declare(cbc.name(), cbc.bytecodes());
+        }
+    }
 
     @Override
     public void addToClasspath(String cp)
--- a/src/jdk.jshell/share/classes/jdk/jshell/execution/DirectExecutionControl.java	Thu May 04 17:55:07 2017 +0000
+++ b/src/jdk.jshell/share/classes/jdk/jshell/execution/DirectExecutionControl.java	Tue May 09 12:22:15 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, 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
@@ -91,6 +91,17 @@
         throw new NotImplementedException("redefine not supported");
     }
 
+    /**Notify that classes have been redefined.
+     *
+     * @param cbcs the class name and bytecodes to redefine
+     * @throws NotImplementedException if not implemented
+     * @throws EngineTerminationException the execution engine has terminated
+     */
+    protected void classesRedefined(ClassBytecodes[] cbcs)
+            throws NotImplementedException, EngineTerminationException {
+        loaderDelegate.classesRedefined(cbcs);
+    }
+
     @Override
     public String invoke(String className, String methodName)
             throws RunException, InternalException, EngineTerminationException {
--- a/src/jdk.jshell/share/classes/jdk/jshell/execution/JdiExecutionControl.java	Thu May 04 17:55:07 2017 +0000
+++ b/src/jdk.jshell/share/classes/jdk/jshell/execution/JdiExecutionControl.java	Tue May 09 12:22:15 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2017, 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
@@ -93,6 +93,12 @@
         } catch (Exception ex) {
             throw new ClassInstallException("redefine: " + ex.getMessage(), new boolean[cbcs.length]);
         }
+        // forward the redefine to remote-end to register the redefined bytecode
+        try {
+            super.redefine(cbcs);
+        } catch (NotImplementedException ex) {
+            // this remote doesn't care about registering bytecode, so we don't either
+        }
     }
 
     /**
--- a/src/jdk.jshell/share/classes/jdk/jshell/execution/LoaderDelegate.java	Thu May 04 17:55:07 2017 +0000
+++ b/src/jdk.jshell/share/classes/jdk/jshell/execution/LoaderDelegate.java	Tue May 09 12:22:15 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, 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
@@ -52,6 +52,13 @@
             throws ClassInstallException, NotImplementedException, EngineTerminationException;
 
     /**
+     * Notify that classes have been redefined.
+     *
+     * @param cbcs the class names and bytecodes that have been redefined
+     */
+    public void classesRedefined(ClassBytecodes[] cbcs);
+
+    /**
      * Adds the path to the execution class path.
      *
      * @param path the path to add
--- a/src/jdk.jshell/share/classes/jdk/jshell/execution/RemoteExecutionControl.java	Thu May 04 17:55:07 2017 +0000
+++ b/src/jdk.jshell/share/classes/jdk/jshell/execution/RemoteExecutionControl.java	Tue May 09 12:22:15 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2017, 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
@@ -96,6 +96,15 @@
     public RemoteExecutionControl() {
     }
 
+    /**
+     * Redefine processing on the remote end is only to register the redefined classes
+     */
+    @Override
+    public void redefine(ClassBytecodes[] cbcs)
+            throws ClassInstallException, NotImplementedException, EngineTerminationException {
+        classesRedefined(cbcs);
+    }
+
     @Override
     public void stop() throws EngineTerminationException, InternalException {
         // handled by JDI
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jshell/GetResourceTest.java	Tue May 09 12:22:15 2017 +0200
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2015, 2017, 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 8179531
+ * @summary Check that ClassLoader.getResource works as expected in the JShell agent.
+ * @modules jdk.jshell
+ * @build KullaTesting TestingInputStream
+ * @run testng GetResourceTest
+ */
+
+import jdk.jshell.Snippet;
+import static jdk.jshell.Snippet.Status.OVERWRITTEN;
+import static jdk.jshell.Snippet.Status.VALID;
+import org.testng.annotations.Test;
+
+
+@Test
+public class GetResourceTest extends KullaTesting {
+
+    public void checkGetResource() {
+        assertEval("import java.util.Arrays;");
+        assertEval("boolean match(byte[] data, byte[] snippet) {\n" +
+                   "    for (int i = 0; i < data.length - snippet.length; i++) {\n" +
+                   "        if (Arrays.equals(Arrays.copyOfRange(data, i, i + snippet.length), snippet)) {\n" +
+                   "            return true;\n" +
+                   "        }\n" +
+                   "    }\n" +
+                   "    return false;\n" +
+                   "}");
+        assertEval("boolean test() throws Exception {\n" +
+                   "    Class c = new Object() {}.getClass().getEnclosingClass();\n" +
+                   "    byte[] data = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\").openStream().readAllBytes();\n" +
+                   "    return match(data, \"check text\".getBytes(\"UTF-8\"));\n" +
+                   "}");
+        assertEval("test()", "true");
+    }
+
+    public void checkRedefine() {
+        assertEval("import java.util.Arrays;");
+        assertEval("boolean match(byte[] data, byte[] snippet) {\n" +
+                   "    for (int i = 0; i < data.length - snippet.length; i++) {\n" +
+                   "        if (Arrays.equals(Arrays.copyOfRange(data, i, i + snippet.length), snippet)) {\n" +
+                   "            return true;\n" +
+                   "        }\n" +
+                   "    }\n" +
+                   "    return false;\n" +
+                   "}");
+        Snippet testMethod =
+                methodKey(assertEval("boolean test() throws Exception {\n" +
+                                     "    return false;\n" +
+                                     "}"));
+        assertEval("boolean test() throws Exception {\n" +
+                   "    Class c = new Object() {}.getClass().getEnclosingClass();\n" +
+                   "    byte[] data = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\").openStream().readAllBytes();\n" +
+                   "    return match(data, \"updated variant\".getBytes(\"UTF-8\"));\n" +
+                   "}",
+                   IGNORE_VALUE,
+                   null,
+                   DiagCheck.DIAG_OK,
+                   DiagCheck.DIAG_OK,
+                   ste(MAIN_SNIPPET, VALID, VALID, false, null),
+                   ste(testMethod, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
+        assertEval("test()", "true");
+    }
+
+    public void checkResourceSize() {
+        assertEval("import java.net.*;");
+        assertEval("boolean test() throws Exception {\n" +
+                   "    Class c = new Object() {}.getClass().getEnclosingClass();" +
+                   "    URL url = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\");\n" +
+                   "    URLConnection connection = url.openConnection();\n" +
+                   "    connection.connect();\n" +
+                   "    return connection.getContentLength() == connection.getInputStream().readAllBytes().length;\n" +
+                   "}");
+        assertEval("test()", "true");
+    }
+
+    public void checkTimestampCheck() {
+        assertEval("import java.net.*;");
+        assertEval("import java.time.*;");
+        assertEval("import java.time.format.*;");
+        assertEval("long[] times(Class c) throws Exception {\n" +
+                   "    URL url = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\");\n" +
+                   "    URLConnection connection = url.openConnection();\n" +
+                   "    connection.connect();\n" +
+                   "    return new long[] {connection.getDate(),\n" +
+                   "                       connection.getLastModified()," +
+                   "                       Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(connection.getHeaderField(\"last-modified\"))).toEpochMilli()};\n" +
+                   "}");
+        Snippet testMethod =
+                methodKey(assertEval("long[] test() throws Exception {\n" +
+                                     "    int i = 0;\n" +
+                                     "    return times(new Object() {}.getClass().getEnclosingClass());\n" +
+                                     "}"));
+        assertEval("long[] orig = test();");
+        long s = System.currentTimeMillis();
+        while ((System.currentTimeMillis() - s) < 1000) { //ensure time change:
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException ex) {}
+        }
+        assertEval("long[] test() throws Exception {\n" +
+                   "    int i = 1;\n" +
+                   "    return times(new Object() {}.getClass().getEnclosingClass());\n" +
+                   "}",
+                   IGNORE_VALUE,
+                   null,
+                   DiagCheck.DIAG_OK,
+                   DiagCheck.DIAG_OK,
+                   ste(MAIN_SNIPPET, VALID, VALID, false, null),
+                   ste(testMethod, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
+        assertEval("long[] nue = test();");
+        assertEval("orig[0] < nue[0]", "true");
+        assertEval("orig[1] < nue[1]", "true");
+        assertEval("orig[0] == orig[2]", "true");
+        assertEval("nue[0] == nue[2]", "true");
+    }
+
+    public void checkFieldAccess() {
+        assertEval("import java.net.*;");
+        assertEval("Class c = new Object() {}.getClass().getEnclosingClass();");
+        assertEval("URL url = c.getClassLoader().getResource(c.getName().replace('.', '/') + \".class\");");
+        assertEval("URLConnection connection = url.openConnection();");
+        assertEval("connection.connect();");
+        assertEval("connection.getHeaderFieldKey(0)", "\"content-length\"");
+        assertEval("connection.getHeaderFieldKey(1)", "\"date\"");
+        assertEval("connection.getHeaderFieldKey(2)", "\"last-modified\"");
+        assertEval("connection.getHeaderFieldKey(3)", "null");
+        assertEval("connection.getHeaderField(0) != null", "true");
+        assertEval("connection.getHeaderField(1) != null", "true");
+        assertEval("connection.getHeaderField(2) != null", "true");
+        assertEval("connection.getHeaderField(3) == null", "true");
+    }
+
+    public void checkGetResources() {
+        assertEval("import java.net.*;");
+        assertEval("Class c = new Object() {}.getClass().getEnclosingClass();");
+        assertEval("c.getClassLoader().getResources(c.getName().replace('.', '/') + \".class\").hasMoreElements()", "true");
+    }
+
+}
+