langtools/src/jdk.jshell/share/classes/jdk/jshell/OuterSnippetsClassWrap.java
author rfield
Wed, 08 Feb 2017 10:43:16 -0800
changeset 43758 868af3718a21
parent 37644 33cf53901cac
child 43770 a321bed02000
permissions -rw-r--r--
8173845: JShell API: not patch compatible 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.LinkedHashMap;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    29
import java.util.List;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    30
import javax.tools.Diagnostic;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    31
import javax.tools.JavaFileObject;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    32
import jdk.jshell.Wrap.CompoundWrap;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    33
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    34
/**
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    35
 * The outer wrap for a set of snippets wrapped in a generated class
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    36
 * @author Robert Field
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    37
 */
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    38
public class OuterSnippetsClassWrap extends OuterWrap {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    39
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    40
    private final String className;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    41
    private final LinkedHashMap<Wrap, Snippet> wrapToSnippet;
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
    OuterSnippetsClassWrap(String className, CompoundWrap w, List<Snippet> snippets, List<Wrap> wraps) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    44
        super(w);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    45
        assert snippets == null || snippets.size() == wraps.size();
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    46
        this.className = className;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    47
        wrapToSnippet = new LinkedHashMap<>();
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    48
        for (int i = 0; i < snippets.size(); ++i) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    49
            wrapToSnippet.put(wraps.get(i), snippets.get(i));
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    50
        }
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
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    53
    public Snippet wrapLineToSnippet(int wline) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    54
        Wrap wrap = ((CompoundWrap)w).wrapLineToWrap(wline);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    55
        return wrapToSnippet.get(wrap);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    56
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    57
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    58
    @Override
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    59
    Diag wrapDiag(Diagnostic<? extends JavaFileObject> d) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    60
        return new WrappedDiagnostic(d) {
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
            @Override
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    63
            Snippet snippetOrNull() {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    64
                Wrap wrap = ((CompoundWrap) w).wrapIndexToWrap(diag.getPosition());
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    65
                Snippet sn = wrapToSnippet.get(wrap);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    66
                if (sn != null) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    67
                    return sn;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    68
                } else {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    69
                    return super.snippetOrNull();
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    70
                }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    71
            }
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
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    75
    int ordinal(Snippet sn) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    76
        int i = 0;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    77
        for (Snippet si : wrapToSnippet.values()) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    78
            if (si == sn) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    79
                return i;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    80
            }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    81
            ++i;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    82
        }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    83
        return -1;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    84
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    85
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    86
    @Override
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    87
    public String className() {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    88
        return className;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    89
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    90
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    91
    @Override
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    92
    public String toString() {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
diff changeset
    93
        return "OSCW(" + className + ":" + w + ")";
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
}