src/jdk.jshell/share/classes/jdk/jshell/Eval.java
author rfield
Fri, 20 Oct 2017 19:08:25 -0700
changeset 47499 b3ea71b70f7b
parent 47350 d65c3b21081c
child 47975 5e86806f57f9
permissions -rw-r--r--
8187359: JShell: Give comprehensible error when user method name matches Object method Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
package jdk.jshell;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    26
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    27
import java.util.ArrayList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    28
import java.util.Collection;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    29
import java.util.Collections;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
import java.util.List;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
import java.util.Locale;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
import java.util.regex.Matcher;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
import java.util.regex.Pattern;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    34
import java.util.stream.Collectors;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
import javax.lang.model.element.Modifier;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import com.sun.source.tree.ArrayTypeTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
import com.sun.source.tree.AssignmentTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import com.sun.source.tree.ClassTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import com.sun.source.tree.ExpressionTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import com.sun.source.tree.IdentifierTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import com.sun.source.tree.MethodTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import com.sun.source.tree.ModifiersTree;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    43
import com.sun.source.tree.NewClassTree;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import com.sun.source.tree.Tree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import com.sun.source.tree.VariableTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import com.sun.tools.javac.tree.JCTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import com.sun.tools.javac.tree.Pretty;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
import java.io.StringWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import java.io.Writer;
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
    51
import java.util.Arrays;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import java.util.LinkedHashSet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import java.util.Set;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 42827
diff changeset
    54
import jdk.jshell.ExpressionToTypeInfo.ExpressionInfo;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
import jdk.jshell.Key.ErroneousKey;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
import jdk.jshell.Key.MethodKey;
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents: 36160
diff changeset
    57
import jdk.jshell.Key.TypeDeclKey;
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
    58
import jdk.jshell.Snippet.Kind;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
import jdk.jshell.Snippet.SubKind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
import jdk.jshell.TaskFactory.AnalyzeTask;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
import jdk.jshell.TaskFactory.BaseTask;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
import jdk.jshell.TaskFactory.CompileTask;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
import jdk.jshell.TaskFactory.ParseTask;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    64
import jdk.jshell.Wrap.CompoundWrap;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
import jdk.jshell.Wrap.Range;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
import jdk.jshell.Snippet.Status;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    67
import jdk.jshell.spi.ExecutionControl.ClassBytecodes;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    68
import jdk.jshell.spi.ExecutionControl.ClassInstallException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    69
import jdk.jshell.spi.ExecutionControl.EngineTerminationException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    70
import jdk.jshell.spi.ExecutionControl.InternalException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    71
import jdk.jshell.spi.ExecutionControl.NotImplementedException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    72
import jdk.jshell.spi.ExecutionControl.ResolutionException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    73
import jdk.jshell.spi.ExecutionControl.RunException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    74
import jdk.jshell.spi.ExecutionControl.UserException;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
import static java.util.stream.Collectors.toList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
import static java.util.stream.Collectors.toSet;
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
    77
import static java.util.Collections.singletonList;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
    79
import static jdk.jshell.Util.DOIT_METHOD_NAME;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
    80
import static jdk.jshell.Util.PREFIX_PATTERN;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
    81
import static jdk.jshell.Util.expunge;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
import static jdk.jshell.Snippet.SubKind.SINGLE_TYPE_IMPORT_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
import static jdk.jshell.Snippet.SubKind.SINGLE_STATIC_IMPORT_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
import static jdk.jshell.Snippet.SubKind.TYPE_IMPORT_ON_DEMAND_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
import static jdk.jshell.Snippet.SubKind.STATIC_IMPORT_ON_DEMAND_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
/**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
 * The Evaluation Engine. Source internal analysis, wrapping control,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
 * compilation, declaration. redefinition, replacement, and execution.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
 * @author Robert Field
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
class Eval {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
    private static final Pattern IMPORT_PATTERN = Pattern.compile("import\\p{javaWhitespace}+(?<static>static\\p{javaWhitespace}+)?(?<fullname>[\\p{L}\\p{N}_\\$\\.]+\\.(?<name>[\\p{L}\\p{N}_\\$]+|\\*))");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    96
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
    97
    // for uses that should not change state -- non-evaluations
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
    98
    private boolean preserveState = false;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
    99
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   100
    private int varNumber = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
    private final JShell state;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   104
    // The set of names of methods on Object
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   105
    private final Set<String> objectMethods = Arrays
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   106
            .stream(Object.class.getMethods())
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   107
            .map(m -> m.getName())
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   108
            .collect(toSet());
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   109
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   110
    Eval(JShell state) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   111
        this.state = state;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   112
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   114
    /**
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   115
     * Evaluates a snippet of source.
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   116
     *
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   117
     * @param userSource the source of the snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   118
     * @return the list of primary and update events
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   119
     * @throws IllegalStateException
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   120
     */
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   121
    List<SnippetEvent> eval(String userSource) throws IllegalStateException {
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   122
        List<SnippetEvent> allEvents = new ArrayList<>();
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   123
        for (Snippet snip : sourceToSnippets(userSource)) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   124
            if (snip.kind() == Kind.ERRONEOUS) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   125
                state.maps.installSnippet(snip);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   126
                allEvents.add(new SnippetEvent(
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   127
                        snip, Status.NONEXISTENT, Status.REJECTED,
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   128
                        false, null, null, null));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   129
            } else {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   130
                allEvents.addAll(declare(snip, snip.syntheticDiags()));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   131
            }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   132
        }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   133
        return allEvents;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   134
    }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   135
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   136
    /**
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   137
     * Converts the user source of a snippet into a Snippet list -- Snippet will
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   138
     * have wrappers.
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   139
     *
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   140
     * @param userSource the source of the snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   141
     * @return usually a singleton list of Snippet, but may be empty or multiple
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   142
     */
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   143
    List<Snippet> sourceToSnippetsWithWrappers(String userSource) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   144
        List<Snippet> snippets = sourceToSnippets(userSource);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   145
        for (Snippet snip : snippets) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   146
            if (snip.outerWrap() == null) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   147
                snip.setOuterWrap(
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   148
                        (snip.kind() == Kind.IMPORT)
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   149
                                ? state.outerMap.wrapImport(snip.guts(), snip)
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   150
                                : state.outerMap.wrapInTrialClass(snip.guts())
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   151
                );
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   152
            }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   153
        }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   154
        return snippets;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   155
    }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   156
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   157
    /**
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   158
     * Converts the user source of a snippet into a Snippet object (or list of
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   159
     * objects in the case of: int x, y, z;).  Does not install the Snippets
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   160
     * or execute them.  Does not change any state.
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   161
     *
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   162
     * @param userSource the source of the snippet
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   163
     * @return usually a singleton list of Snippet, but may be empty or multiple
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   164
     */
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   165
    List<Snippet> toScratchSnippets(String userSource) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   166
        try {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   167
            preserveState = true;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   168
            return sourceToSnippets(userSource);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   169
        } finally {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   170
            preserveState = false;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   171
        }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   172
    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   173
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   174
    /**
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   175
     * Converts the user source of a snippet into a Snippet object (or list of
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   176
     * objects in the case of: int x, y, z;).  Does not install the Snippets
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   177
     * or execute them.
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   178
     *
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   179
     * @param userSource the source of the snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   180
     * @return usually a singleton list of Snippet, but may be empty or multiple
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   181
     */
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   182
    private List<Snippet> sourceToSnippets(String userSource) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
        String compileSource = Util.trimEnd(new MaskCommentsAndModifiers(userSource, false).cleared());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        if (compileSource.length() == 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
            return Collections.emptyList();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
        }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   187
        return state.taskFactory.parse(compileSource, pt -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   188
            List<? extends Tree> units = pt.units();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   189
            if (units.isEmpty()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   190
                return compileFailResult(pt, userSource, Kind.ERRONEOUS);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   191
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   192
            Tree unitTree = units.get(0);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   193
            if (pt.getDiagnostics().hasOtherThanNotStatementErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   194
                return compileFailResult(pt, userSource, kindOfTree(unitTree));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   195
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   197
            // Erase illegal/ignored modifiers
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   198
            String compileSourceInt = new MaskCommentsAndModifiers(compileSource, true).cleared();
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   199
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   200
            state.debug(DBG_GEN, "Kind: %s -- %s\n", unitTree.getKind(), unitTree);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   201
            switch (unitTree.getKind()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   202
                case IMPORT:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   203
                    return processImport(userSource, compileSourceInt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   204
                case VARIABLE:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   205
                    return processVariables(userSource, units, compileSourceInt, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   206
                case EXPRESSION_STATEMENT:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   207
                    return processExpression(userSource, compileSourceInt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   208
                case CLASS:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   209
                    return processClass(userSource, unitTree, compileSourceInt, SubKind.CLASS_SUBKIND, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   210
                case ENUM:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   211
                    return processClass(userSource, unitTree, compileSourceInt, SubKind.ENUM_SUBKIND, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   212
                case ANNOTATION_TYPE:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   213
                    return processClass(userSource, unitTree, compileSourceInt, SubKind.ANNOTATION_TYPE_SUBKIND, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   214
                case INTERFACE:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   215
                    return processClass(userSource, unitTree, compileSourceInt, SubKind.INTERFACE_SUBKIND, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   216
                case METHOD:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   217
                    return processMethod(userSource, unitTree, compileSourceInt, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   218
                default:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   219
                    return processStatement(userSource, compileSourceInt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   220
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   221
        });
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   222
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   223
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   224
    private List<Snippet> processImport(String userSource, String compileSource) {
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   225
        Wrap guts = Wrap.simpleWrap(compileSource);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   226
        Matcher mat = IMPORT_PATTERN.matcher(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   227
        String fullname;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   228
        String name;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
        boolean isStatic;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
        if (mat.find()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   231
            isStatic = mat.group("static") != null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   232
            name = mat.group("name");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   233
            fullname = mat.group("fullname");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   234
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   235
            // bad import -- fake it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   236
            isStatic = compileSource.contains("static");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   237
            name = fullname = compileSource;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   238
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   239
        String fullkey = (isStatic ? "static-" : "") + fullname;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   240
        boolean isStar = name.equals("*");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   241
        String keyName = isStar
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
                ? fullname
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   243
                : name;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
        SubKind snippetKind = isStar
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
                ? (isStatic ? STATIC_IMPORT_ON_DEMAND_SUBKIND : TYPE_IMPORT_ON_DEMAND_SUBKIND)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   246
                : (isStatic ? SINGLE_STATIC_IMPORT_SUBKIND : SINGLE_TYPE_IMPORT_SUBKIND);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
        Snippet snip = new ImportSnippet(state.keyMap.keyForImport(keyName, snippetKind),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
                userSource, guts, fullname, name, snippetKind, fullkey, isStatic, isStar);
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   249
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   250
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   252
    private static class EvalPretty extends Pretty {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   253
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   254
        private final Writer out;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   255
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
        public EvalPretty(Writer writer, boolean bln) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
            super(writer, bln);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   258
            this.out = writer;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   259
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   260
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
        /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   262
         * Print string, DO NOT replacing all non-ascii character with unicode
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
         * escapes.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
         */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
        public void print(Object o) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
            out.write(o.toString());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   268
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   269
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   270
        static String prettyExpr(JCTree tree, boolean bln) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   271
            StringWriter out = new StringWriter();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   273
                new EvalPretty(out, bln).printExpr(tree);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   274
            } catch (IOException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   275
                throw new AssertionError(e);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   276
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   277
            return out.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   278
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   279
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   280
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   281
    private List<Snippet> processVariables(String userSource, List<? extends Tree> units, String compileSource, ParseTask pt) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   282
        List<Snippet> snippets = new ArrayList<>();
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34752
diff changeset
   283
        TreeDissector dis = TreeDissector.createByFirstClass(pt);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   284
        for (Tree unitTree : units) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   285
            VariableTree vt = (VariableTree) unitTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   286
            String name = vt.getName().toString();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   287
            String typeName;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   288
            String fullTypeName;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   289
            TreeDependencyScanner tds = new TreeDependencyScanner();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   290
            Wrap typeWrap;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   291
            Wrap anonDeclareWrap = null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   292
            Wrap winit = null;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   293
            StringBuilder sbBrackets = new StringBuilder();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   294
            Tree baseType = vt.getType();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   295
            if (baseType != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   296
                tds.scan(baseType); // Not dependent on initializer
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   297
                fullTypeName = typeName = EvalPretty.prettyExpr((JCTree) vt.getType(), false);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   298
                while (baseType instanceof ArrayTypeTree) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   299
                    //TODO handle annotations too
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   300
                    baseType = ((ArrayTypeTree) baseType).getType();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   301
                    sbBrackets.append("[]");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   302
                }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   303
                Range rtype = dis.treeToRange(baseType);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   304
                typeWrap = Wrap.rangeWrap(compileSource, rtype);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   305
            } else {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   306
                DiagList dl = trialCompile(Wrap.methodWrap(compileSource));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   307
                if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   308
                    return compileFailResult(dl, userSource, kindOfTree(unitTree));
47318
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47268
diff changeset
   309
                }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   310
                Tree init = vt.getInitializer();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   311
                if (init != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   312
                    Range rinit = dis.treeToRange(init);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   313
                    String initCode = rinit.part(compileSource);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   314
                    ExpressionInfo ei =
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   315
                            ExpressionToTypeInfo.localVariableTypeForInitializer(initCode, state);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   316
                    typeName = ei == null ? "java.lang.Object" : ei.typeName;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   317
                    fullTypeName = ei == null ? "java.lang.Object" : ei.fullTypeName;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   318
                    if (ei != null && init.getKind() == Tree.Kind.NEW_CLASS &&
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   319
                        ((NewClassTree) init).getClassBody() != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   320
                        NewClassTree nct = (NewClassTree) init;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   321
                        StringBuilder constructor = new StringBuilder();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   322
                        constructor.append(fullTypeName).append("(");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   323
                        String sep = "";
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   324
                        if (ei.enclosingInstanceType != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   325
                            constructor.append(ei.enclosingInstanceType);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   326
                            constructor.append(" encl");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   327
                            sep = ", ";
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   328
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   329
                        int idx = 0;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   330
                        for (String type : ei.parameterTypes) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   331
                            constructor.append(sep);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   332
                            constructor.append(type);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   333
                            constructor.append(" ");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   334
                            constructor.append("arg" + idx++);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   335
                            sep = ", ";
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   336
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   337
                        if (ei.enclosingInstanceType != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   338
                            constructor.append(") { encl.super (");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   339
                        } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   340
                            constructor.append(") { super (");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   341
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   342
                        sep = "";
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   343
                        for (int i = 0; i < idx; i++) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   344
                            constructor.append(sep);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   345
                            constructor.append("arg" + i++);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   346
                            sep = ", ";
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   347
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   348
                        constructor.append("); }");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   349
                        List<? extends Tree> members = nct.getClassBody().getMembers();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   350
                        Range bodyRange = dis.treeListToRange(members);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   351
                        Wrap bodyWrap;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   352
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   353
                        if (bodyRange != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   354
                            bodyWrap = Wrap.rangeWrap(compileSource, bodyRange);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   355
                        } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   356
                            bodyWrap = Wrap.simpleWrap(" ");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   357
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   358
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   359
                        Range argRange = dis.treeListToRange(nct.getArguments());
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   360
                        Wrap argWrap;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   361
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   362
                        if (argRange != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   363
                            argWrap = Wrap.rangeWrap(compileSource, argRange);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   364
                        } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   365
                            argWrap = Wrap.simpleWrap(" ");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   366
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   367
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   368
                        if (ei.enclosingInstanceType != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   369
                            Range enclosingRanges =
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   370
                                    dis.treeToRange(nct.getEnclosingExpression());
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   371
                            Wrap enclosingWrap = Wrap.rangeWrap(compileSource, enclosingRanges);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   372
                            argWrap = argRange != null ? new CompoundWrap(enclosingWrap,
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   373
                                                                          Wrap.simpleWrap(","),
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   374
                                                                          argWrap)
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   375
                                                       : enclosingWrap;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   376
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   377
                        Wrap hwrap = Wrap.simpleWrap("public static class " + fullTypeName +
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   378
                                                     (ei.isClass ? " extends " : " implements ") +
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   379
                                                     typeName + " { " + constructor);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   380
                        anonDeclareWrap = new CompoundWrap(hwrap, bodyWrap, Wrap.simpleWrap("}"));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   381
                        winit = new CompoundWrap("new " + fullTypeName + "(", argWrap, ")");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   382
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   383
                        String superType = typeName;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   384
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   385
                        typeName = fullTypeName;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   386
                        fullTypeName = ei.isClass ? "<anonymous class extending " + superType + ">"
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   387
                                                  : "<anonymous class implementing " + superType + ">";
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   388
                    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   389
                    tds.scan(init);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   390
                } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   391
                    fullTypeName = typeName = "java.lang.Object";
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   392
                }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   393
                typeWrap = Wrap.identityWrap(typeName);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   394
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   395
            Range runit = dis.treeToRange(vt);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   396
            runit = new Range(runit.begin, runit.end - 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   397
            ExpressionTree it = vt.getInitializer();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   398
            int nameMax = runit.end - 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   399
            SubKind subkind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   400
            if (it != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   401
                subkind = SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   402
                Range rinit = dis.treeToRange(it);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   403
                winit = winit == null ? Wrap.rangeWrap(compileSource, rinit) : winit;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   404
                nameMax = rinit.begin - 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   405
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   406
                subkind = SubKind.VAR_DECLARATION_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   407
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   408
            int nameStart = compileSource.lastIndexOf(name, nameMax);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   409
            if (nameStart < 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   410
                throw new AssertionError("Name '" + name + "' not found");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   411
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   412
            int nameEnd = nameStart + name.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   413
            Range rname = new Range(nameStart, nameEnd);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   414
            Wrap guts = Wrap.varWrap(compileSource, typeWrap, sbBrackets.toString(), rname,
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   415
                                     winit, anonDeclareWrap);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   416
                        DiagList modDiag = modifierDiagnostics(vt.getModifiers(), dis, true);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   417
            Snippet snip = new VarSnippet(state.keyMap.keyForVariable(name), userSource, guts,
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   418
                    name, subkind, fullTypeName,
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   419
                    tds.declareReferences(), modDiag);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   420
            snippets.add(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   421
        }
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   422
        return snippets;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   423
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   424
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   425
    private List<Snippet> processExpression(String userSource, String compileSource) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   426
        String name = null;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 42827
diff changeset
   427
        ExpressionInfo ei = ExpressionToTypeInfo.expressionInfo(compileSource, state);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   428
        ExpressionTree assignVar;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   429
        Wrap guts;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   430
        Snippet snip;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   431
        if (ei != null && ei.isNonVoid) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   432
            String typeName = ei.typeName;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   433
            SubKind subkind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   434
            if (ei.tree instanceof IdentifierTree) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   435
                IdentifierTree id = (IdentifierTree) ei.tree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   436
                name = id.getName().toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   437
                subkind = SubKind.VAR_VALUE_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   438
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   439
            } else if (ei.tree instanceof AssignmentTree
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   440
                    && (assignVar = ((AssignmentTree) ei.tree).getVariable()) instanceof IdentifierTree) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   441
                name = assignVar.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   442
                subkind = SubKind.ASSIGNMENT_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   443
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   444
                subkind = SubKind.OTHER_EXPRESSION_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   445
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   446
            if (shouldGenTempVar(subkind)) {
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   447
                if (preserveState) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   448
                    name = "$$";
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   449
                } else {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   450
                    if (state.tempVariableNameGenerator != null) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   451
                        name = state.tempVariableNameGenerator.get();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   452
                    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   453
                    while (name == null || state.keyMap.doesVariableNameExist(name)) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   454
                        name = "$" + ++varNumber;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   455
                    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   456
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   457
                guts = Wrap.tempVarWrap(compileSource, typeName, name);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   458
                Collection<String> declareReferences = null; //TODO
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   459
                snip = new VarSnippet(state.keyMap.keyForVariable(name), userSource, guts,
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   460
                        name, SubKind.TEMP_VAR_EXPRESSION_SUBKIND, typeName, declareReferences, null);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   461
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   462
                guts = Wrap.methodReturnWrap(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   463
                snip = new ExpressionSnippet(state.keyMap.keyForExpression(name, typeName), userSource, guts,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   464
                        name, subkind);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   465
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   466
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   467
            guts = Wrap.methodWrap(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   468
            if (ei == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   469
                // We got no type info, check for not a statement by trying
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   470
                DiagList dl = trialCompile(guts);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   471
                if (dl.hasNotStatement()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   472
                    guts = Wrap.methodReturnWrap(compileSource);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   473
                    dl = trialCompile(guts);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   474
                }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   475
                if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   476
                    return compileFailResult(dl, userSource, Kind.EXPRESSION);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   477
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   478
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   479
            snip = new StatementSnippet(state.keyMap.keyForStatement(), userSource, guts);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   480
        }
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   481
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   482
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   483
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   484
    private List<Snippet> processClass(String userSource, Tree unitTree, String compileSource, SubKind snippetKind, ParseTask pt) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   485
        TreeDependencyScanner tds = new TreeDependencyScanner();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   486
        tds.scan(unitTree);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   487
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34752
diff changeset
   488
        TreeDissector dis = TreeDissector.createByFirstClass(pt);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   489
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   490
        ClassTree klassTree = (ClassTree) unitTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   491
        String name = klassTree.getSimpleName().toString();
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   492
        DiagList modDiag = modifierDiagnostics(klassTree.getModifiers(), dis, false);
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents: 36160
diff changeset
   493
        TypeDeclKey key = state.keyMap.keyForClass(name);
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   494
        // Corralling mutates.  Must be last use of pt, unitTree, klassTree
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   495
        Wrap corralled = new Corraller(key.index(), pt.getContext()).corralType(klassTree);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   496
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   497
        Wrap guts = Wrap.classMemberWrap(compileSource);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   498
        Snippet snip = new TypeDeclSnippet(key, userSource, guts,
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   499
                name, snippetKind,
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   500
                corralled, tds.declareReferences(), tds.bodyReferences(), modDiag);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   501
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   502
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   503
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   504
    private List<Snippet> processStatement(String userSource, String compileSource) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   505
        Wrap guts = Wrap.methodWrap(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   506
        // Check for unreachable by trying
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   507
        DiagList dl = trialCompile(guts);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   508
        if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   509
            if (dl.hasUnreachableError()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   510
                guts = Wrap.methodUnreachableSemiWrap(compileSource);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   511
                dl = trialCompile(guts);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   512
                if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   513
                    if (dl.hasUnreachableError()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   514
                        // Without ending semicolon
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   515
                        guts = Wrap.methodUnreachableWrap(compileSource);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   516
                        dl = trialCompile(guts);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   517
                    }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   518
                    if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   519
                        return compileFailResult(dl, userSource, Kind.STATEMENT);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   520
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   521
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   522
            } else {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   523
                return compileFailResult(dl, userSource, Kind.STATEMENT);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   524
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   525
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   526
        Snippet snip = new StatementSnippet(state.keyMap.keyForStatement(), userSource, guts);
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   527
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   528
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   529
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   530
    private DiagList trialCompile(Wrap guts) {
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   531
        OuterWrap outer = state.outerMap.wrapInTrialClass(guts);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   532
        return state.taskFactory.analyze(outer, AnalyzeTask::getDiagnostics);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   533
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   534
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   535
    private List<Snippet> processMethod(String userSource, Tree unitTree, String compileSource, ParseTask pt) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   536
        TreeDependencyScanner tds = new TreeDependencyScanner();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   537
        tds.scan(unitTree);
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   538
        final TreeDissector dis = TreeDissector.createByFirstClass(pt);
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   539
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   540
        final MethodTree mt = (MethodTree) unitTree;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   541
        final String name = mt.getName().toString();
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   542
        if (objectMethods.contains(name)) {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   543
            // The name matches a method on Object, short of an overhaul, this
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   544
            // fails, see 8187137.  Generate a descriptive error message
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   545
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   546
            // The error position will be the position of the name, find it
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   547
            long possibleStart = dis.getEndPosition(mt.getReturnType());
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   548
            Range possibleRange = new Range((int) possibleStart,
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   549
                    dis.getStartPosition(mt.getBody()));
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   550
            String possibleNameSection = possibleRange.part(compileSource);
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   551
            int offset = possibleNameSection.indexOf(name);
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   552
            long start = offset < 0
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   553
                    ? possibleStart // something wrong, punt
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   554
                    : possibleStart + offset;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   555
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   556
            return compileFailResult(new DiagList(objectMethodNameDiag(name, start)), userSource, Kind.METHOD);
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   557
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   558
        String parameterTypes
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   559
                = mt.getParameters()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   560
                .stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   561
                .map(param -> dis.treeToRange(param.getType()).part(compileSource))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   562
                .collect(Collectors.joining(","));
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   563
        Tree returnType = mt.getReturnType();
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   564
        DiagList modDiag = modifierDiagnostics(mt.getModifiers(), dis, true);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   565
        MethodKey key = state.keyMap.keyForMethod(name, parameterTypes);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   566
        // Corralling mutates.  Must be last use of pt, unitTree, mt
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   567
        Wrap corralled = new Corraller(key.index(), pt.getContext()).corralMethod(mt);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   568
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   569
        if (modDiag.hasErrors()) {
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   570
            return compileFailResult(modDiag, userSource, Kind.METHOD);
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   571
        }
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   572
        Wrap guts = Wrap.classMemberWrap(compileSource);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   573
        Range typeRange = dis.treeToRange(returnType);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   574
        String signature = "(" + parameterTypes + ")" + typeRange.part(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   575
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   576
        Snippet snip = new MethodSnippet(key, userSource, guts,
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   577
                name, signature,
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   578
                corralled, tds.declareReferences(), tds.bodyReferences(), modDiag);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   579
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   580
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   581
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   582
    private Kind kindOfTree(Tree tree) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   583
        switch (tree.getKind()) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   584
            case IMPORT:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   585
                return Kind.IMPORT;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   586
            case VARIABLE:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   587
                return Kind.VAR;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   588
            case EXPRESSION_STATEMENT:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   589
                return Kind.EXPRESSION;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   590
            case CLASS:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   591
            case ENUM:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   592
            case ANNOTATION_TYPE:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   593
            case INTERFACE:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   594
                return Kind.TYPE_DECL;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   595
            case METHOD:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   596
                return Kind.METHOD;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   597
            default:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   598
                return Kind.STATEMENT;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   599
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   600
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   601
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   602
    /**
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   603
     * The snippet has failed, return with the rejected snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   604
     *
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   605
     * @param xt the task from which to extract the failure diagnostics
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   606
     * @param userSource the incoming bad user source
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   607
     * @return a rejected snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   608
     */
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   609
    private List<Snippet> compileFailResult(BaseTask xt, String userSource, Kind probableKind) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   610
        return compileFailResult(xt.getDiagnostics(), userSource, probableKind);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   611
    }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   612
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   613
    /**
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   614
     * The snippet has failed, return with the rejected snippet
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   615
     *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   616
     * @param diags the failure diagnostics
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   617
     * @param userSource the incoming bad user source
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   618
     * @return a rejected snippet
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   619
     */
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   620
    private List<Snippet> compileFailResult(DiagList diags, String userSource, Kind probableKind) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   621
        ErroneousKey key = state.keyMap.keyForErroneous();
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   622
        Snippet snip = new ErroneousSnippet(key, userSource, null,
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   623
                probableKind, SubKind.UNKNOWN_SUBKIND);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   624
        snip.setFailed(diags);
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   625
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   626
        // Install  wrapper for query by SourceCodeAnalysis.wrapper
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   627
        String compileSource = Util.trimEnd(new MaskCommentsAndModifiers(userSource, true).cleared());
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   628
        OuterWrap outer;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   629
        switch (probableKind) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   630
            case IMPORT:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   631
                outer = state.outerMap.wrapImport(Wrap.simpleWrap(compileSource), snip);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   632
                break;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   633
            case EXPRESSION:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   634
                outer = state.outerMap.wrapInTrialClass(Wrap.methodReturnWrap(compileSource));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   635
                break;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   636
            case VAR:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   637
            case TYPE_DECL:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   638
            case METHOD:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   639
                outer = state.outerMap.wrapInTrialClass(Wrap.classMemberWrap(compileSource));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   640
                break;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   641
            default:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   642
                outer = state.outerMap.wrapInTrialClass(Wrap.methodWrap(compileSource));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   643
                break;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   644
        }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   645
        snip.setOuterWrap(outer);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   646
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   647
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   648
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   649
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   650
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   651
     * Should a temp var wrap the expression. TODO make this user configurable.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   652
     *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   653
     * @param snippetKind
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   654
     * @return
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   655
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   656
    private boolean shouldGenTempVar(SubKind snippetKind) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   657
        return snippetKind == SubKind.OTHER_EXPRESSION_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   658
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   659
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   660
    List<SnippetEvent> drop(Snippet si) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   661
        Unit c = new Unit(state, si);
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   662
        Set<Unit> outs;
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   663
        if (si instanceof PersistentSnippet) {
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   664
            Set<Unit> ins = c.dependents().collect(toSet());
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   665
            outs = compileAndLoad(ins);
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   666
        } else {
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   667
            outs = Collections.emptySet();
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   668
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   669
        return events(c, outs, null, null);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   670
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   671
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   672
    private List<SnippetEvent> declare(Snippet si, DiagList generatedDiagnostics) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   673
        Unit c = new Unit(state, si, null, generatedDiagnostics);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   674
        Set<Unit> ins = new LinkedHashSet<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   675
        ins.add(c);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   676
        Set<Unit> outs = compileAndLoad(ins);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   677
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38535
diff changeset
   678
        if (!si.status().isDefined()
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   679
                && si.diagnostics().isEmpty()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   680
                && si.unresolved().isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   681
            // did not succeed, but no record of it, extract from others
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   682
            si.setDiagnostics(outs.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   683
                    .flatMap(u -> u.snippet().diagnostics().stream())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   684
                    .collect(Collectors.toCollection(DiagList::new)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   685
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   686
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   687
        // If appropriate, execute the snippet
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   688
        String value = null;
37751
77e7bb904a13 8139837: JShell API: make a common JShellException
rfield
parents: 37644
diff changeset
   689
        JShellException exception = null;
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38535
diff changeset
   690
        if (si.status().isDefined()) {
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   691
            if (si.isExecutable()) {
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   692
                try {
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   693
                    value = state.executionControl().invoke(si.classFullName(), DOIT_METHOD_NAME);
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   694
                    value = si.subKind().hasValue()
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   695
                            ? expunge(value)
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   696
                            : "";
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   697
                } catch (ResolutionException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   698
                    DeclarationSnippet sn = (DeclarationSnippet) state.maps.getSnippetDeadOrAlive(ex.id());
40769
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
   699
                    exception = new UnresolvedReferenceException(sn, translateExceptionStack(ex));
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   700
                } catch (UserException ex) {
46926
25ac5da6502e 8185108: JShell: NullPointerException when throwing exception with null message under local ExecutionControl
rfield
parents: 46185
diff changeset
   701
                    exception = new EvalException(ex.getMessage(),
40769
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
   702
                            ex.causeExceptionClass(),
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
   703
                            translateExceptionStack(ex));
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   704
                } catch (RunException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   705
                    // StopException - no-op
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   706
                } catch (InternalException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   707
                    state.debug(ex, "invoke");
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   708
                } catch (EngineTerminationException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   709
                    state.closeDown();
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   710
                }
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   711
            } else if (si.subKind() == SubKind.VAR_DECLARATION_SUBKIND) {
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   712
                switch (((VarSnippet) si).typeName()) {
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   713
                    case "byte":
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   714
                    case "short":
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   715
                    case "int":
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   716
                    case "long":
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   717
                        value = "0";
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   718
                        break;
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   719
                    case "float":
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   720
                    case "double":
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   721
                        value = "0.0";
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   722
                        break;
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   723
                    case "boolean":
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   724
                        value = "false";
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   725
                        break;
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   726
                    case "char":
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   727
                        value = "''";
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   728
                        break;
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   729
                    default:
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   730
                        value = "null";
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   731
                        break;
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   732
                }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   733
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   734
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   735
        return events(c, outs, value, exception);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   736
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   737
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   738
    private boolean interestingEvent(SnippetEvent e) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   739
        return e.isSignatureChange()
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   740
                    || e.causeSnippet() == null
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   741
                    || e.status() != e.previousStatus()
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   742
                    || e.exception() != null;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   743
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   744
37751
77e7bb904a13 8139837: JShell API: make a common JShellException
rfield
parents: 37644
diff changeset
   745
    private List<SnippetEvent> events(Unit c, Collection<Unit> outs, String value, JShellException exception) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   746
        List<SnippetEvent> events = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   747
        events.add(c.event(value, exception));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   748
        events.addAll(outs.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   749
                .filter(u -> u != c)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   750
                .map(u -> u.event(null, null))
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   751
                .filter(this::interestingEvent)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   752
                .collect(Collectors.toList()));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   753
        events.addAll(outs.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   754
                .flatMap(u -> u.secondaryEvents().stream())
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   755
                .filter(this::interestingEvent)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   756
                .collect(Collectors.toList()));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   757
        //System.err.printf("Events: %s\n", events);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   758
        return events;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   759
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   760
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   761
    private Set<OuterWrap> outerWrapSet(Collection<Unit> units) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   762
        return units.stream()
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   763
                .map(u -> u.snippet().outerWrap())
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   764
                .collect(toSet());
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   765
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   766
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   767
    private Set<Unit> compileAndLoad(Set<Unit> ins) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   768
        if (ins.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   769
            return ins;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   770
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   771
        Set<Unit> replaced = new LinkedHashSet<>();
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   772
        // Loop until dependencies and errors are stable
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   773
        while (true) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   774
            state.debug(DBG_GEN, "compileAndLoad  %s\n", ins);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   775
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42272
diff changeset
   776
            ins.stream().forEach(Unit::initialize);
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   777
            ins.stream().forEach(u -> u.setWrap(ins, ins));
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   778
            state.taskFactory.analyze(outerWrapSet(ins), at -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   779
                ins.stream().forEach(u -> u.setDiagnostics(at));
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34752
diff changeset
   780
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   781
                // corral any Snippets that need it
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   782
                if (ins.stream().anyMatch(u -> u.corralIfNeeded(ins))) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   783
                    // if any were corralled, re-analyze everything
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   784
                    state.taskFactory.analyze(outerWrapSet(ins), cat -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   785
                        ins.stream().forEach(u -> u.setCorralledDiagnostics(cat));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   786
                        ins.stream().forEach(u -> u.setStatus(cat));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   787
                        return null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   788
                    });
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   789
                } else {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   790
                    ins.stream().forEach(u -> u.setStatus(at));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   791
                }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   792
                return null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   793
            });
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   794
            // compile and load the legit snippets
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   795
            boolean success;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   796
            while (true) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   797
                List<Unit> legit = ins.stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42272
diff changeset
   798
                        .filter(Unit::isDefined)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   799
                        .collect(toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   800
                state.debug(DBG_GEN, "compileAndLoad ins = %s -- legit = %s\n",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   801
                        ins, legit);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   802
                if (legit.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   803
                    // no class files can be generated
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   804
                    success = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   805
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   806
                    // re-wrap with legit imports
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   807
                    legit.stream().forEach(u -> u.setWrap(ins, legit));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   808
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   809
                    // generate class files for those capable
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   810
                    Result res = state.taskFactory.compile(outerWrapSet(legit), ct -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   811
                        if (!ct.compile()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   812
                            // oy! compile failed because of recursive new unresolved
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   813
                            if (legit.stream()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   814
                                    .filter(u -> u.smashingErrorDiagnostics(ct))
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   815
                                    .count() > 0) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   816
                                // try again, with the erroreous removed
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   817
                                return Result.CONTINUE;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   818
                            } else {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   819
                                state.debug(DBG_GEN, "Should never happen error-less failure - %s\n",
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   820
                                        legit);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   821
                            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   822
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   823
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   824
                        // load all new classes
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   825
                        load(legit.stream()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   826
                                .flatMap(u -> u.classesToLoad(ct.classList(u.snippet().outerWrap())))
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   827
                                .collect(toSet()));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   828
                        // attempt to redefine the remaining classes
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   829
                        List<Unit> toReplace = legit.stream()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   830
                                .filter(u -> !u.doRedefines())
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   831
                                .collect(toList());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   832
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   833
                        // prevent alternating redefine/replace cyclic dependency
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   834
                        // loop by replacing all that have been replaced
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   835
                        if (!toReplace.isEmpty()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   836
                            replaced.addAll(toReplace);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   837
                            replaced.stream().forEach(Unit::markForReplacement);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   838
                        }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   839
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   840
                        return toReplace.isEmpty() ? Result.SUCESS : Result.FAILURE;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   841
                    });
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   842
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   843
                    switch (res) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   844
                        case CONTINUE: continue;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   845
                        case SUCESS: success = true; break;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   846
                        default:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   847
                        case FAILURE: success = false; break;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   848
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   849
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   850
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   851
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   852
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   853
            // add any new dependencies to the working set
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   854
            List<Unit> newDependencies = ins.stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42272
diff changeset
   855
                    .flatMap(Unit::effectedDependents)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   856
                    .collect(toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   857
            state.debug(DBG_GEN, "compileAndLoad %s -- deps: %s  success: %s\n",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   858
                    ins, newDependencies, success);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   859
            if (!ins.addAll(newDependencies) && success) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   860
                // all classes that could not be directly loaded (because they
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   861
                // are new) have been redefined, and no new dependnencies were
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   862
                // identified
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42272
diff changeset
   863
                ins.stream().forEach(Unit::finish);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   864
                return ins;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   865
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   866
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   867
    }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   868
    //where:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   869
        enum Result {SUCESS, FAILURE, CONTINUE}
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   870
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
   871
    /**
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
   872
     * If there are classes to load, loads by calling the execution engine.
42272
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41858
diff changeset
   873
     * @param classbytecodes names of the classes to load.
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
   874
     */
42272
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41858
diff changeset
   875
    private void load(Collection<ClassBytecodes> classbytecodes) {
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41858
diff changeset
   876
        if (!classbytecodes.isEmpty()) {
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41858
diff changeset
   877
            ClassBytecodes[] cbcs = classbytecodes.toArray(new ClassBytecodes[classbytecodes.size()]);
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   878
            try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   879
                state.executionControl().load(cbcs);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   880
                state.classTracker.markLoaded(cbcs);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   881
            } catch (ClassInstallException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   882
                state.classTracker.markLoaded(cbcs, ex.installed());
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   883
            } catch (NotImplementedException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   884
                state.debug(ex, "Seriously?!? load not implemented");
42272
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41858
diff changeset
   885
                state.closeDown();
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   886
            } catch (EngineTerminationException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   887
                state.closeDown();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   888
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   889
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   890
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   891
40769
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
   892
    private StackTraceElement[] translateExceptionStack(Exception ex) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   893
        StackTraceElement[] raw = ex.getStackTrace();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   894
        int last = raw.length;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   895
        do {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   896
            if (last == 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   897
                last = raw.length - 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   898
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   899
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   900
        } while (!isWrap(raw[--last]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   901
        StackTraceElement[] elems = new StackTraceElement[last + 1];
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   902
        for (int i = 0; i <= last; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   903
            StackTraceElement r = raw[i];
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   904
            OuterSnippetsClassWrap outer = state.outerMap.getOuter(r.getClassName());
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   905
            if (outer != null) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   906
                String klass = expunge(r.getClassName());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   907
                String method = r.getMethodName().equals(DOIT_METHOD_NAME) ? "" : r.getMethodName();
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   908
                int wln = r.getLineNumber() - 1;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   909
                int line = outer.wrapLineToSnippetLine(wln) + 1;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   910
                Snippet sn = outer.wrapLineToSnippet(wln);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   911
                String file = "#" + sn.id();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   912
                elems[i] = new StackTraceElement(klass, method, file, line);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   913
            } else if (r.getFileName().equals("<none>")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   914
                elems[i] = new StackTraceElement(r.getClassName(), r.getMethodName(), null, r.getLineNumber());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   915
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   916
                elems[i] = r;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   917
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   918
        }
40769
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
   919
        return elems;
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
   920
    }
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
   921
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   922
    private boolean isWrap(StackTraceElement ste) {
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   923
        return PREFIX_PATTERN.matcher(ste.getClassName()).find();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   924
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   925
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   926
    /**
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   927
     * Construct a diagnostic for a method name matching an Object method name
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   928
     * @param name the method name
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   929
     * @param nameStart the position within the source of the method name
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   930
     * @return the generated diagnostic
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   931
     */
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   932
    private Diag objectMethodNameDiag(String name, long nameStart) {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   933
        return new Diag() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   934
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   935
            public boolean isError() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   936
                return true;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   937
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   938
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   939
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   940
            public long getPosition() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   941
                return nameStart;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   942
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   943
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   944
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   945
            public long getStartPosition() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   946
                return nameStart;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   947
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   948
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   949
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   950
            public long getEndPosition() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   951
                return nameStart + name.length();
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   952
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   953
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   954
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   955
            public String getCode() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   956
                return "jdk.eval.error.object.method";
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   957
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   958
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   959
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   960
            public String getMessage(Locale locale) {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   961
                return state.messageFormat("jshell.diag.object.method.fatal",
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   962
                        String.join(" ", objectMethods));
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   963
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   964
        };
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   965
    }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   966
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   967
    private DiagList modifierDiagnostics(ModifiersTree modtree,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   968
            final TreeDissector dis, boolean isAbstractProhibited) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   969
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   970
        class ModifierDiagnostic extends Diag {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   971
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   972
            final boolean fatal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   973
            final String message;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   974
            long start;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   975
            long end;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   976
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   977
            ModifierDiagnostic(List<Modifier> list, boolean fatal) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   978
                this.fatal = fatal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   979
                StringBuilder sb = new StringBuilder();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   980
                for (Modifier mod : list) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   981
                    sb.append("'");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   982
                    sb.append(mod.toString());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   983
                    sb.append("' ");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   984
                }
36990
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36780
diff changeset
   985
                String key = (list.size() > 1)
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36780
diff changeset
   986
                        ? fatal
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36780
diff changeset
   987
                            ? "jshell.diag.modifier.plural.fatal"
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36780
diff changeset
   988
                            : "jshell.diag.modifier.plural.ignore"
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36780
diff changeset
   989
                        : fatal
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36780
diff changeset
   990
                            ? "jshell.diag.modifier.single.fatal"
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36780
diff changeset
   991
                            : "jshell.diag.modifier.single.ignore";
ec0b843a7af5 8147515: JShell: Internationalize
rfield
parents: 36780
diff changeset
   992
                this.message = state.messageFormat(key, sb.toString());
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   993
                start = dis.getStartPosition(modtree);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   994
                end = dis.getEndPosition(modtree);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   995
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   996
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   997
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   998
            public boolean isError() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   999
                return fatal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1000
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1001
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1002
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1003
            public long getPosition() {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1004
                return start;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1005
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1006
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1007
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1008
            public long getStartPosition() {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1009
                return start;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1010
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1011
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1012
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1013
            public long getEndPosition() {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1014
                return end;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1015
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1016
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1017
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1018
            public String getCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1019
                return fatal
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1020
                        ? "jdk.eval.error.illegal.modifiers"
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1021
                        : "jdk.eval.warn.illegal.modifiers";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1022
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1023
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1024
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1025
            public String getMessage(Locale locale) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1026
                return message;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1027
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1028
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1029
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1030
        List<Modifier> list = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1031
        boolean fatal = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1032
        for (Modifier mod : modtree.getFlags()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1033
            switch (mod) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1034
                case SYNCHRONIZED:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1035
                case NATIVE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1036
                    list.add(mod);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1037
                    fatal = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1038
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1039
                case ABSTRACT:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1040
                    if (isAbstractProhibited) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1041
                        list.add(mod);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1042
                        fatal = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1043
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1044
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1045
                case PUBLIC:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1046
                case PROTECTED:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1047
                case PRIVATE:
41858
5843b57ce3a6 8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents: 41514
diff changeset
  1048
                    // quietly ignore, user cannot see effects one way or the other
5843b57ce3a6 8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents: 41514
diff changeset
  1049
                    break;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1050
                case STATIC:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1051
                case FINAL:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1052
                    list.add(mod);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1053
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1054
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1055
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1056
        return list.isEmpty()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1057
                ? new DiagList()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1058
                : new DiagList(new ModifierDiagnostic(list, fatal));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1059
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1060
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1061
}