langtools/src/jdk.jshell/share/classes/jdk/jshell/execution/DefaultLoaderDelegate.java
author jlahoda
Tue, 09 May 2017 12:22:15 +0200
changeset 45044 7c50549b7744
parent 43132 6a5c69926e60
permissions -rw-r--r--
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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     1
/*
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     4
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    10
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    15
 * accompanied this code).
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    16
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    20
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    23
 * questions.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    24
 */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    25
package jdk.jshell.execution;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    26
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    27
import java.io.ByteArrayInputStream;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    28
import java.io.File;
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    29
import java.io.IOException;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    30
import java.io.InputStream;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    31
import java.net.MalformedURLException;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    32
import java.net.URI;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    33
import java.net.URISyntaxException;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    34
import java.net.URL;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    35
import java.net.URLClassLoader;
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    36
import java.net.URLConnection;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    37
import java.net.URLStreamHandler;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    38
import java.security.CodeSource;
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    39
import java.time.Instant;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    40
import java.time.ZoneId;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    41
import java.time.ZonedDateTime;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    42
import java.time.format.DateTimeFormatter;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    43
import java.util.ArrayList;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    44
import java.util.Collections;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    45
import java.util.Date;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    46
import java.util.Enumeration;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    47
import java.util.HashMap;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    48
import java.util.LinkedHashMap;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    49
import java.util.List;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    50
import java.util.Map;
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    51
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    52
import jdk.jshell.spi.ExecutionControl.ClassBytecodes;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    53
import jdk.jshell.spi.ExecutionControl.ClassInstallException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    54
import jdk.jshell.spi.ExecutionControl.EngineTerminationException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    55
import jdk.jshell.spi.ExecutionControl.InternalException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    56
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    57
/**
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    58
 * The standard implementation of {@link LoaderDelegate} using
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    59
 * a {@link URLClassLoader}.
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    60
 *
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    61
 * @author Robert Field
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    62
 */
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    63
class DefaultLoaderDelegate implements LoaderDelegate {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    64
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    65
    private final RemoteClassLoader loader;
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    66
    private final Map<String, Class<?>> klasses = new HashMap<>();
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    67
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    68
    private static class RemoteClassLoader extends URLClassLoader {
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    69
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    70
        private final Map<String, ClassFile> classFiles = new HashMap<>();
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    71
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    72
        RemoteClassLoader() {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    73
            super(new URL[0]);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    74
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
    75
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    76
        private class ResourceURLStreamHandler extends URLStreamHandler {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    77
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    78
            private final String name;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    79
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    80
            ResourceURLStreamHandler(String name) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    81
                this.name = name;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    82
            }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    83
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    84
            @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    85
            protected URLConnection openConnection(URL u) throws IOException {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    86
                return new URLConnection(u) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    87
                    private InputStream in;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    88
                    private Map<String, List<String>> fields;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    89
                    private List<String> fieldNames;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    90
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    91
                    @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    92
                    public void connect() {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    93
                        if (connected) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    94
                            return;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    95
                        }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    96
                        connected = true;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    97
                        ClassFile file = classFiles.get(name);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    98
                        in = new ByteArrayInputStream(file.data);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
    99
                        fields = new LinkedHashMap<>();
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   100
                        fields.put("content-length", List.of(Integer.toString(file.data.length)));
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   101
                        Instant instant = new Date(file.timestamp).toInstant();
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   102
                        ZonedDateTime time = ZonedDateTime.ofInstant(instant, ZoneId.of("GMT"));
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   103
                        String timeStamp = DateTimeFormatter.RFC_1123_DATE_TIME.format(time);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   104
                        fields.put("date", List.of(timeStamp));
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   105
                        fields.put("last-modified", List.of(timeStamp));
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   106
                        fieldNames = new ArrayList<>(fields.keySet());
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   107
                    }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   108
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   109
                    @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   110
                    public InputStream getInputStream() throws IOException {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   111
                        connect();
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   112
                        return in;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   113
                    }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   114
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   115
                    @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   116
                    public String getHeaderField(String name) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   117
                        connect();
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   118
                        return fields.getOrDefault(name, List.of())
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   119
                                     .stream()
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   120
                                     .findFirst()
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   121
                                     .orElse(null);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   122
                    }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   123
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   124
                    @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   125
                    public Map<String, List<String>> getHeaderFields() {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   126
                        connect();
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   127
                        return fields;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   128
                    }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   129
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   130
                    @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   131
                    public String getHeaderFieldKey(int n) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   132
                        return n < fieldNames.size() ? fieldNames.get(n) : null;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   133
                    }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   134
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   135
                    @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   136
                    public String getHeaderField(int n) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   137
                        String name = getHeaderFieldKey(n);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   138
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   139
                        return name != null ? getHeaderField(name) : null;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   140
                    }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   141
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   142
                };
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   143
            }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   144
        }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   145
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   146
        void declare(String name, byte[] bytes) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   147
            classFiles.put(toResourceString(name), new ClassFile(bytes, System.currentTimeMillis()));
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   148
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   149
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   150
        @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   151
        protected Class<?> findClass(String name) throws ClassNotFoundException {
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   152
            ClassFile file = classFiles.get(toResourceString(name));
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   153
            if (file == null) {
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   154
                return super.findClass(name);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   155
            }
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   156
            return super.defineClass(name, file.data, 0, file.data.length, (CodeSource) null);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   157
        }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   158
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   159
        @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   160
        public URL findResource(String name) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   161
            URL u = doFindResource(name);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   162
            return u != null ? u : super.findResource(name);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   163
        }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   164
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   165
        @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   166
        public Enumeration<URL> findResources(String name) throws IOException {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   167
            URL u = doFindResource(name);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   168
            Enumeration<URL> sup = super.findResources(name);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   169
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   170
            if (u == null) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   171
                return sup;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   172
            }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   173
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   174
            List<URL> result = new ArrayList<>();
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   175
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   176
            while (sup.hasMoreElements()) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   177
                result.add(sup.nextElement());
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   178
            }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   179
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   180
            result.add(u);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   181
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   182
            return Collections.enumeration(result);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   183
        }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   184
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   185
        private URL doFindResource(String name) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   186
            if (classFiles.containsKey(name)) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   187
                try {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   188
                    return new URL(null,
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   189
                                   new URI("jshell", null, "/" + name, null).toString(),
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   190
                                   new ResourceURLStreamHandler(name));
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   191
                } catch (MalformedURLException | URISyntaxException ex) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   192
                    throw new InternalError(ex);
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   193
                }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   194
            }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   195
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   196
            return null;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   197
        }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   198
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   199
        private String toResourceString(String className) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   200
            return className.replace('.', '/') + ".class";
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   201
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   202
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   203
        @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   204
        public void addURL(URL url) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   205
            super.addURL(url);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   206
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   207
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   208
        private static class ClassFile {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   209
            public final byte[] data;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   210
            public final long timestamp;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   211
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   212
            ClassFile(byte[] data, long timestamp) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   213
                this.data = data;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   214
                this.timestamp = timestamp;
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   215
            }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   216
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   217
        }
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   218
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   219
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   220
    public DefaultLoaderDelegate() {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   221
        this.loader = new RemoteClassLoader();
40516
9e0e107c39dd 8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents: 39807
diff changeset
   222
        Thread.currentThread().setContextClassLoader(loader);
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   223
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   224
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   225
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   226
    public void load(ClassBytecodes[] cbcs)
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   227
            throws ClassInstallException, EngineTerminationException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   228
        boolean[] loaded = new boolean[cbcs.length];
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   229
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   230
            for (ClassBytecodes cbc : cbcs) {
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   231
                loader.declare(cbc.name(), cbc.bytecodes());
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   232
            }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   233
            for (int i = 0; i < cbcs.length; ++i) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   234
                ClassBytecodes cbc = cbcs[i];
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   235
                Class<?> klass = loader.loadClass(cbc.name());
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   236
                klasses.put(cbc.name(), klass);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   237
                loaded[i] = true;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   238
                // Get class loaded to the point of, at least, preparation
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   239
                klass.getDeclaredMethods();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   240
            }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   241
        } catch (Throwable ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   242
            throw new ClassInstallException("load: " + ex.getMessage(), loaded);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   243
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   244
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   245
45044
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   246
    @Override
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   247
    public void classesRedefined(ClassBytecodes[] cbcs) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   248
        for (ClassBytecodes cbc : cbcs) {
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   249
            loader.declare(cbc.name(), cbc.bytecodes());
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   250
        }
7c50549b7744 8179531: JShell: fails to provide bytecode for dynamically created lambdas
jlahoda
parents: 43132
diff changeset
   251
    }
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   252
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   253
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   254
    public void addToClasspath(String cp)
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   255
            throws EngineTerminationException, InternalException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   256
        try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   257
            for (String path : cp.split(File.pathSeparator)) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   258
                loader.addURL(new File(path).toURI().toURL());
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   259
            }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   260
        } catch (Exception ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   261
            throw new InternalException(ex.toString());
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   262
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   263
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   264
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   265
    @Override
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   266
    public Class<?> findClass(String name) throws ClassNotFoundException {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   267
        Class<?> klass = klasses.get(name);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   268
        if (klass == null) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   269
            throw new ClassNotFoundException(name + " not found");
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   270
        } else {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   271
            return klass;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   272
        }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   273
    }
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   274
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
diff changeset
   275
}