langtools/src/jdk.jshell/share/classes/jdk/jshell/OuterWrapMap.java
author rfield
Sat, 21 May 2016 22:32:08 -0700
changeset 38535 4a25025e0b0d
parent 37644 33cf53901cac
child 43770 a321bed02000
permissions -rw-r--r--
8156101: JShell SPI: Provide a pluggable execution control SPI Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
     1
/*
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
     4
 *
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    10
 *
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    15
 * accompanied this code).
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    16
 *
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    20
 *
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    23
 * questions.
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    24
 */
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    25
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    26
package jdk.jshell;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    27
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    28
import java.util.ArrayList;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    29
import java.util.Collection;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    30
import java.util.Collections;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    31
import java.util.HashMap;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    32
import java.util.List;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    33
import java.util.Map;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    34
import java.util.Set;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    35
import java.util.regex.Matcher;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    36
import java.util.stream.Collectors;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    37
import jdk.jshell.Wrap.CompoundWrap;
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37644
diff changeset
    38
import static jdk.jshell.Util.PREFIX_PATTERN;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37644
diff changeset
    39
import static jdk.jshell.Util.REPL_CLASS_PREFIX;
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    40
import static jdk.jshell.Util.REPL_DOESNOTMATTER_CLASS_NAME;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    41
import static jdk.jshell.Util.asLetters;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    42
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    43
/**
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    44
 *
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    45
 * @author Robert Field
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    46
 */
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    47
public class OuterWrapMap {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    48
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    49
    private final JShell state;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    50
    private final Map<String,OuterSnippetsClassWrap> classOuters = new HashMap<>();
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    51
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    52
    OuterWrapMap(JShell state) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    53
        this.state = state;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    54
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    55
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    56
    OuterSnippetsClassWrap getOuter(String className) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    57
        Matcher matcher = PREFIX_PATTERN.matcher(className);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    58
        String cn;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    59
        if (matcher.find() && (cn = matcher.group("class")) != null) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    60
            return classOuters.get(cn);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    61
        }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    62
        return null;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    63
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    64
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    65
    private CompoundWrap wrappedInClass(String className, String imports, List<Wrap> wraps) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    66
        List<Object> elems = new ArrayList<>(wraps.size() + 2);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    67
        elems.add(imports +
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    68
                "class " + className + " {\n");
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    69
        elems.addAll(wraps);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    70
        elems.add("}\n");
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    71
        return new CompoundWrap(elems.toArray());
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    72
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    73
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    74
    OuterWrap wrapInClass(Set<Key> except, Collection<Snippet> plus,
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    75
            List<Snippet> snippets, List<Wrap> wraps) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    76
        String imports = state.maps.packageAndImportsExcept(except, plus);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    77
        // className is unique to the set of snippets and their version (seq)
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    78
        String className = REPL_CLASS_PREFIX + snippets.stream()
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    79
                .sorted((sn1, sn2) -> sn1.key().index() - sn2.key().index())
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    80
                .map(sn -> "" + sn.key().index() + asLetters(sn.sequenceNumber()))
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    81
                .collect(Collectors.joining("_"));
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    82
        CompoundWrap w = wrappedInClass(className, imports, wraps);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    83
        OuterSnippetsClassWrap now = new OuterSnippetsClassWrap(className, w, snippets, wraps);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    84
        classOuters.put(className, now);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    85
        return now;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    86
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    87
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    88
    OuterWrap wrapInTrialClass(Wrap wrap) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    89
        String imports = state.maps.packageAndImportsExcept(null, null);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    90
        CompoundWrap w = wrappedInClass(REPL_DOESNOTMATTER_CLASS_NAME, imports,
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    91
                Collections.singletonList(wrap));
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    92
        return new OuterWrap(w);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    93
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    94
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    95
    OuterWrap wrapImport(Wrap guts, Snippet sn) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    96
        return new OuterImportSnippetWrap(guts, sn);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    97
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    98
}