src/jdk.jshell/share/classes/jdk/jshell/Eval.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 55397 5eeee2cc94f5
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
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
/*
55397
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55156
diff changeset
     2
 * Copyright (c) 2014, 2019, 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;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
    39
import com.sun.source.tree.ExpressionStatementTree;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import com.sun.source.tree.ExpressionTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import com.sun.source.tree.IdentifierTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import com.sun.source.tree.MethodTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import com.sun.source.tree.ModifiersTree;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    44
import com.sun.source.tree.NewClassTree;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import com.sun.source.tree.Tree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import com.sun.source.tree.VariableTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import com.sun.tools.javac.tree.JCTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import com.sun.tools.javac.tree.Pretty;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import java.io.StringWriter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import java.io.Writer;
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
    52
import java.util.Arrays;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import java.util.LinkedHashSet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
import java.util.Set;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 42827
diff changeset
    55
import jdk.jshell.ExpressionToTypeInfo.ExpressionInfo;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
    56
import jdk.jshell.ExpressionToTypeInfo.ExpressionInfo.AnonymousDescription;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
    57
import jdk.jshell.ExpressionToTypeInfo.ExpressionInfo.AnonymousDescription.VariableDesc;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import jdk.jshell.Key.ErroneousKey;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
import jdk.jshell.Key.MethodKey;
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents: 36160
diff changeset
    60
import jdk.jshell.Key.TypeDeclKey;
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
    61
import jdk.jshell.Snippet.Kind;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
import jdk.jshell.Snippet.SubKind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
import jdk.jshell.TaskFactory.AnalyzeTask;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
import jdk.jshell.TaskFactory.BaseTask;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
import jdk.jshell.TaskFactory.ParseTask;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
    66
import jdk.jshell.Util.Pair;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    67
import jdk.jshell.Wrap.CompoundWrap;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
import jdk.jshell.Wrap.Range;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
import jdk.jshell.Snippet.Status;
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    70
import jdk.jshell.spi.ExecutionControl.ClassBytecodes;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    71
import jdk.jshell.spi.ExecutionControl.ClassInstallException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    72
import jdk.jshell.spi.ExecutionControl.EngineTerminationException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    73
import jdk.jshell.spi.ExecutionControl.InternalException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    74
import jdk.jshell.spi.ExecutionControl.NotImplementedException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    75
import jdk.jshell.spi.ExecutionControl.ResolutionException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    76
import jdk.jshell.spi.ExecutionControl.RunException;
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
    77
import jdk.jshell.spi.ExecutionControl.UserException;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
import static java.util.stream.Collectors.toList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
import static java.util.stream.Collectors.toSet;
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
    80
import static java.util.Collections.singletonList;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
    81
import com.sun.tools.javac.code.Symbol.TypeSymbol;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
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
    83
import static jdk.jshell.Util.DOIT_METHOD_NAME;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
    84
import static jdk.jshell.Util.PREFIX_PATTERN;
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
    85
import static jdk.jshell.Util.expunge;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
import static jdk.jshell.Snippet.SubKind.SINGLE_TYPE_IMPORT_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
import static jdk.jshell.Snippet.SubKind.SINGLE_STATIC_IMPORT_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
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
    89
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
    90
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
/**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
 * The Evaluation Engine. Source internal analysis, wrapping control,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
 * compilation, declaration. redefinition, replacement, and execution.
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
 * @author Robert Field
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    96
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    97
class Eval {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    98
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    99
    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}_\\$]+|\\*))");
55142
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
   100
    private static final Pattern DEFAULT_PREFIX = Pattern.compile("\\p{javaWhitespace}*(default)\\p{javaWhitespace}+");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   102
    // 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
   103
    private boolean preserveState = false;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   104
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
    private int varNumber = 0;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   107
    /* The number of anonymous innerclasses seen so far. Used to generate unique
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   108
     * names of these classes.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   109
     */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   110
    private int anonCount = 0;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   111
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   112
    private final JShell state;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   114
    // 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
   115
    private final Set<String> objectMethods = Arrays
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   116
            .stream(Object.class.getMethods())
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   117
            .map(m -> m.getName())
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   118
            .collect(toSet());
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   119
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   120
    Eval(JShell state) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   121
        this.state = state;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   122
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   123
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   124
    /**
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   125
     * Evaluates a snippet of source.
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   126
     *
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   127
     * @param userSource the source of the snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   128
     * @return the list of primary and update events
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   129
     * @throws IllegalStateException
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   130
     */
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
    List<SnippetEvent> eval(String userSource) throws IllegalStateException {
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   132
        List<SnippetEvent> allEvents = new ArrayList<>();
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   133
        for (Snippet snip : sourceToSnippets(userSource)) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   134
            if (snip.kind() == Kind.ERRONEOUS) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   135
                state.maps.installSnippet(snip);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   136
                allEvents.add(new SnippetEvent(
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   137
                        snip, Status.NONEXISTENT, Status.REJECTED,
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   138
                        false, null, null, null));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   139
            } else {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   140
                allEvents.addAll(declare(snip, snip.syntheticDiags()));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   141
            }
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
        return allEvents;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   144
    }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   145
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   146
    /**
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   147
     * 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
   148
     * have wrappers.
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   149
     *
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   150
     * @param userSource the source of the snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   151
     * @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
   152
     */
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   153
    List<Snippet> sourceToSnippetsWithWrappers(String userSource) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   154
        List<Snippet> snippets = sourceToSnippets(userSource);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   155
        for (Snippet snip : snippets) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   156
            if (snip.outerWrap() == null) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   157
                snip.setOuterWrap(
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   158
                        (snip.kind() == Kind.IMPORT)
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   159
                                ? state.outerMap.wrapImport(snip.guts(), snip)
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   160
                                : state.outerMap.wrapInTrialClass(snip.guts())
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   161
                );
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   162
            }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   163
        }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   164
        return snippets;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   165
    }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   166
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   167
    /**
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   168
     * 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
   169
     * 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
   170
     * or execute them.  Does not change any state.
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
     * @param userSource the source of the snippet
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   173
     * @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
   174
     */
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   175
    List<Snippet> toScratchSnippets(String userSource) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   176
        try {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   177
            preserveState = true;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   178
            return sourceToSnippets(userSource);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   179
        } finally {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   180
            preserveState = false;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   181
        }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   182
    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   183
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   184
    /**
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   185
     * 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
   186
     * 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
   187
     * or execute them.
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   188
     *
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   189
     * @param userSource the source of the snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   190
     * @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
   191
     */
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   192
    private List<Snippet> sourceToSnippets(String userSource) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
        String compileSource = Util.trimEnd(new MaskCommentsAndModifiers(userSource, false).cleared());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
        if (compileSource.length() == 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
            return Collections.emptyList();
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
        return state.taskFactory.parse(compileSource, pt -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   198
            List<? extends Tree> units = pt.units();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   199
            if (units.isEmpty()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   200
                return compileFailResult(pt, userSource, Kind.ERRONEOUS);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   201
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   202
            Tree unitTree = units.get(0);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   203
            if (pt.getDiagnostics().hasOtherThanNotStatementErrors()) {
55142
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
   204
                Matcher matcher = DEFAULT_PREFIX.matcher(compileSource);
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
   205
                DiagList dlist = matcher.lookingAt()
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
   206
                        ? new DiagList(new ModifierDiagnostic(true,
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
   207
                            state.messageFormat("jshell.diag.modifier.single.fatal", "'default'"),
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
   208
                            matcher.start(1), matcher.end(1)))
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
   209
                        : pt.getDiagnostics();
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
   210
                return compileFailResult(dlist, userSource, kindOfTree(unitTree));
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   211
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   212
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   213
            // Erase illegal/ignored modifiers
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   214
            String compileSourceInt = new MaskCommentsAndModifiers(compileSource, true).cleared();
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   215
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   216
            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
   217
            switch (unitTree.getKind()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   218
                case IMPORT:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   219
                    return processImport(userSource, compileSourceInt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   220
                case VARIABLE:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   221
                    return processVariables(userSource, units, compileSourceInt, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   222
                case EXPRESSION_STATEMENT:
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   223
                    return processExpression(userSource, unitTree, compileSourceInt, pt);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   224
                case CLASS:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   225
                    return processClass(userSource, unitTree, compileSourceInt, SubKind.CLASS_SUBKIND, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   226
                case ENUM:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   227
                    return processClass(userSource, unitTree, compileSourceInt, SubKind.ENUM_SUBKIND, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   228
                case ANNOTATION_TYPE:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   229
                    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
   230
                case INTERFACE:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   231
                    return processClass(userSource, unitTree, compileSourceInt, SubKind.INTERFACE_SUBKIND, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   232
                case METHOD:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   233
                    return processMethod(userSource, unitTree, compileSourceInt, pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   234
                default:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   235
                    return processStatement(userSource, compileSourceInt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   236
            }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   237
        });
33362
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
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   240
    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
   241
        Wrap guts = Wrap.simpleWrap(compileSource);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
        Matcher mat = IMPORT_PATTERN.matcher(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   243
        String fullname;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
        String name;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
        boolean isStatic;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   246
        if (mat.find()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
            isStatic = mat.group("static") != null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
            name = mat.group("name");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   249
            fullname = mat.group("fullname");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   250
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
            // bad import -- fake it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   252
            isStatic = compileSource.contains("static");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   253
            name = fullname = compileSource;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   254
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   255
        String fullkey = (isStatic ? "static-" : "") + fullname;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
        boolean isStar = name.equals("*");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
        String keyName = isStar
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   258
                ? fullname
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   259
                : name;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   260
        SubKind snippetKind = isStar
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
                ? (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
   262
                : (isStatic ? SINGLE_STATIC_IMPORT_SUBKIND : SINGLE_TYPE_IMPORT_SUBKIND);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
        Snippet snip = new ImportSnippet(state.keyMap.keyForImport(keyName, snippetKind),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
                userSource, guts, fullname, name, snippetKind, fullkey, isStatic, isStar);
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   265
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   268
    private static class EvalPretty extends Pretty {
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
        private final Writer out;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   271
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
        public EvalPretty(Writer writer, boolean bln) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   273
            super(writer, bln);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   274
            this.out = writer;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   275
        }
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
        /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   278
         * 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
   279
         * escapes.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   280
         */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   281
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   282
        public void print(Object o) throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   283
            out.write(o.toString());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   284
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   285
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   286
        static String prettyExpr(JCTree tree, boolean bln) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   287
            StringWriter out = new StringWriter();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   288
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   289
                new EvalPretty(out, bln).printExpr(tree);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   290
            } catch (IOException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   291
                throw new AssertionError(e);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   292
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   293
            return out.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   294
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   295
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   296
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   297
    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
   298
        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
   299
        TreeDissector dis = TreeDissector.createByFirstClass(pt);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   300
        for (Tree unitTree : units) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   301
            VariableTree vt = (VariableTree) unitTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   302
            String name = vt.getName().toString();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   303
            String typeName;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   304
            String fullTypeName;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   305
            String displayType;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   306
            boolean hasEnhancedType = false;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   307
            TreeDependencyScanner tds = new TreeDependencyScanner();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   308
            Wrap typeWrap;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   309
            Wrap anonDeclareWrap = null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   310
            Wrap winit = null;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   311
            boolean enhancedDesugaring = false;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   312
            Set<String> anonymousClasses = Collections.emptySet();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   313
            StringBuilder sbBrackets = new StringBuilder();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   314
            Tree baseType = vt.getType();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   315
            if (baseType != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   316
                tds.scan(baseType); // Not dependent on initializer
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   317
                fullTypeName = displayType = typeName = EvalPretty.prettyExpr((JCTree) vt.getType(), false);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   318
                while (baseType instanceof ArrayTypeTree) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   319
                    //TODO handle annotations too
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   320
                    baseType = ((ArrayTypeTree) baseType).getType();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   321
                    sbBrackets.append("[]");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   322
                }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   323
                Range rtype = dis.treeToRange(baseType);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   324
                typeWrap = Wrap.rangeWrap(compileSource, rtype);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   325
            } else {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   326
                DiagList dl = trialCompile(Wrap.methodWrap(compileSource));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   327
                if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   328
                    return compileFailResult(dl, userSource, kindOfTree(unitTree));
47318
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47268
diff changeset
   329
                }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   330
                Tree init = vt.getInitializer();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   331
                if (init != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   332
                    Range rinit = dis.treeToRange(init);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   333
                    String initCode = rinit.part(compileSource);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   334
                    ExpressionInfo ei =
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   335
                            ExpressionToTypeInfo.localVariableTypeForInitializer(initCode, state, false);
52775
d488477865c0 8213725: JShell NullPointerException due to class file with unexpected package
rfield
parents: 52282
diff changeset
   336
                    if (ei != null && ei.declareTypeName != null) {
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   337
                        typeName = ei.declareTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   338
                        fullTypeName = ei.fullTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   339
                        displayType = ei.displayTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   340
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   341
                        hasEnhancedType = !typeName.equals(fullTypeName);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   342
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   343
                        enhancedDesugaring = !ei.isPrimitiveType;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   344
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   345
                        Pair<Wrap, Wrap> anonymous2Member =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   346
                                anonymous2Member(ei, compileSource, rinit, dis, init);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   347
                        anonDeclareWrap = anonymous2Member.first;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   348
                        winit = anonymous2Member.second;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   349
                        anonymousClasses = ei.anonymousClasses.stream().map(ad -> ad.declareTypeName).collect(Collectors.toSet());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   350
                    } else {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   351
                        displayType = fullTypeName = typeName = "java.lang.Object";
47268
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
                    tds.scan(init);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   354
                } else {
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   355
                    displayType = fullTypeName = typeName = "java.lang.Object";
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   356
                }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   357
                typeWrap = Wrap.identityWrap(typeName);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   358
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   359
            Range runit = dis.treeToRange(vt);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   360
            runit = new Range(runit.begin, runit.end - 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   361
            ExpressionTree it = vt.getInitializer();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   362
            int nameMax = runit.end - 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   363
            SubKind subkind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   364
            if (it != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   365
                subkind = SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   366
                Range rinit = dis.treeToRange(it);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   367
                winit = winit == null ? Wrap.rangeWrap(compileSource, rinit) : winit;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   368
                nameMax = rinit.begin - 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   369
            } else {
52282
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   370
                String sinit;
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   371
                switch (typeName) {
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   372
                    case "byte":
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   373
                    case "short":
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   374
                    case "int":
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   375
                        sinit = "0";
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   376
                        break;
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   377
                    case "long":
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   378
                        sinit = "0L";
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   379
                        break;
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   380
                    case "float":
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   381
                        sinit = "0.0f";
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   382
                        break;
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   383
                    case "double":
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   384
                        sinit = "0.0d";
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   385
                        break;
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   386
                    case "boolean":
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   387
                        sinit = "false";
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   388
                        break;
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   389
                    case "char":
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   390
                        sinit = "'\\u0000'";
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   391
                        break;
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   392
                    default:
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   393
                        sinit = "null";
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   394
                        break;
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   395
                }
003c062e16ea 8211694: JShell: Redeclared variable should be reset
rfield
parents: 49515
diff changeset
   396
                winit = Wrap.simpleWrap(sinit);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   397
                subkind = SubKind.VAR_DECLARATION_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   398
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   399
            int nameStart = compileSource.lastIndexOf(name, nameMax);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   400
            if (nameStart < 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   401
                throw new AssertionError("Name '" + name + "' not found");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   402
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   403
            int nameEnd = nameStart + name.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   404
            Range rname = new Range(nameStart, nameEnd);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   405
            Wrap guts = Wrap.varWrap(compileSource, typeWrap, sbBrackets.toString(), rname,
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   406
                                     winit, enhancedDesugaring, anonDeclareWrap);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   407
            DiagList modDiag = modifierDiagnostics(vt.getModifiers(), dis, true);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   408
            Snippet snip = new VarSnippet(state.keyMap.keyForVariable(name), userSource, guts,
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   409
                    name, subkind, displayType, hasEnhancedType ? fullTypeName : null, anonymousClasses,
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   410
                    tds.declareReferences(), modDiag);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   411
            snippets.add(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   412
        }
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   413
        return snippets;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   414
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   415
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   416
    /**Convert anonymous classes in "init" to member classes, based
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   417
     * on the additional information from ExpressionInfo.anonymousClasses.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   418
     *
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   419
     * This means:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   420
     * -if the code in the anonymous class captures any variables from the
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   421
     *  enclosing context, create fields for them
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   422
     * -creating an explicit constructor that:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   423
     * --if the new class expression has a base/enclosing expression, make it an
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   424
     *   explicit constructor parameter "encl" and use "encl.super" when invoking
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   425
     *   the supertype constructor
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   426
     * --if the (used) supertype constructor has any parameters, declare them
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   427
     *   as explicit parameters of the constructor, and pass them to the super
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   428
     *   constructor
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   429
     * --if the code in the anonymous class captures any variables from the
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   430
     *   enclosing context, make them an explicit paramters of the constructor
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   431
     *   and assign to respective fields.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   432
     * --if there are any explicit fields with initializers in the anonymous class,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   433
     *   move the initializers at the end of the constructor (after the captured fields
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   434
     *   are assigned, so that the initializers of these fields can use them).
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   435
     * -from the captured variables fields, constructor, and existing members
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   436
     *  (with cleared field initializers), create an explicit class that extends or
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   437
     *  implements the supertype of the anonymous class.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   438
     *
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   439
     * This method returns two wraps: the first contains the class declarations for the
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   440
     * converted classes, the first one should be used instead of "init" in the variable
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   441
     * declaration.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   442
     */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   443
    private Pair<Wrap, Wrap> anonymous2Member(ExpressionInfo ei,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   444
                                              String compileSource,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   445
                                              Range rinit,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   446
                                              TreeDissector dis,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   447
                                              Tree init) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   448
        List<Wrap> anonymousDeclarations = new ArrayList<>();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   449
        List<Wrap> partitionedInit = new ArrayList<>();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   450
        int lastPos = rinit.begin;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   451
        com.sun.tools.javac.util.List<NewClassTree> toConvert =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   452
                ExpressionToTypeInfo.listAnonymousClassesToConvert(init);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   453
        com.sun.tools.javac.util.List<AnonymousDescription> descriptions =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   454
                ei.anonymousClasses;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   455
        while (toConvert.nonEmpty() && descriptions.nonEmpty()) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   456
            NewClassTree node = toConvert.head;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   457
            AnonymousDescription ad = descriptions.head;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   458
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   459
            toConvert = toConvert.tail;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   460
            descriptions = descriptions.tail;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   461
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   462
            List<Object> classBodyParts = new ArrayList<>();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   463
            //declarations of the captured variables:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   464
            for (VariableDesc vd : ad.capturedVariables) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   465
                classBodyParts.add(vd.type + " " + vd.name + ";\n");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   466
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   467
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   468
            List<Object> constructorParts = new ArrayList<>();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   469
            constructorParts.add(ad.declareTypeName + "(");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   470
            String sep = "";
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   471
            //add the parameter for the base/enclosing expression, if any:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   472
            if (ad.enclosingInstanceType != null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   473
                constructorParts.add(ad.enclosingInstanceType + " encl");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   474
                sep = ", ";
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   475
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   476
            int idx = 0;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   477
            //add parameters of the super constructor, if any:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   478
            for (String type : ad.parameterTypes) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   479
                constructorParts.add(sep);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   480
                constructorParts.add(type + " " + "arg" + idx++);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   481
                sep = ", ";
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   482
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   483
            //add parameters for the captured variables:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   484
            for (VariableDesc vd : ad.capturedVariables) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   485
                constructorParts.add(sep);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   486
                constructorParts.add(vd.type + " " + "cap$" + vd.name);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   487
                sep = ", ";
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   488
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   489
            //construct super constructor call:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   490
            if (ad.enclosingInstanceType != null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   491
                //if there's an enclosing instance, call super on it:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   492
                constructorParts.add(") { encl.super (");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   493
            } else {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   494
                constructorParts.add(") { super (");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   495
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   496
            sep = "";
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   497
            for (int i = 0; i < idx; i++) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   498
                constructorParts.add(sep);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   499
                constructorParts.add("arg" + i);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   500
                sep = ", ";
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   501
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   502
            constructorParts.add(");");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   503
            //initialize the captured variables:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   504
            for (VariableDesc vd : ad.capturedVariables) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   505
                constructorParts.add("this." + vd.name + " = " + "cap$" + vd.name + ";\n");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   506
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   507
            List<? extends Tree> members =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   508
                    node.getClassBody().getMembers();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   509
            for (Tree member : members) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   510
                if (member.getKind() == Tree.Kind.VARIABLE) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   511
                    VariableTree vt = (VariableTree) member;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   512
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   513
                    if (vt.getInitializer() != null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   514
                        //for variables with initializer, explicitly move the initializer
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   515
                        //to the constructor after the captured variables as assigned
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   516
                        //(the initializers would otherwise run too early):
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   517
                        Range wholeVar = dis.treeToRange(vt);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   518
                        int name = ((JCTree) vt).pos;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   519
                        classBodyParts.add(new CompoundWrap(Wrap.rangeWrap(compileSource,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   520
                                                                      new Range(wholeVar.begin, name)),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   521
                                                       vt.getName().toString(),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   522
                                                       ";\n"));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   523
                        constructorParts.add(Wrap.rangeWrap(compileSource,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   524
                                                            new Range(name, wholeVar.end)));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   525
                        continue;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   526
                    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   527
                }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   528
                classBodyParts.add(Wrap.rangeWrap(compileSource,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   529
                                             dis.treeToRange(member)));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   530
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   531
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   532
            constructorParts.add("}");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   533
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   534
            //construct the member class:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   535
            classBodyParts.add(new CompoundWrap(constructorParts.toArray()));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   536
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   537
            Wrap classBodyWrap = new CompoundWrap(classBodyParts.toArray());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   538
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   539
            anonymousDeclarations.add(new CompoundWrap("public static class ", ad.declareTypeName,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   540
                                         (ad.isClass ? " extends " : " implements "),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   541
                                         ad.superTypeName, " { ", classBodyWrap, "}"));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   542
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   543
            //change the new class expression to use the newly created member type:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   544
            Range argRange = dis.treeListToRange(node.getArguments());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   545
            Wrap argWrap;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   546
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   547
            if (argRange != null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   548
                argWrap = Wrap.rangeWrap(compileSource, argRange);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   549
            } else {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   550
                argWrap = Wrap.simpleWrap(" ");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   551
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   552
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   553
            if (ad.enclosingInstanceType != null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   554
                //if there's an enclosing expression, set it as the first parameter:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   555
                Range enclosingRanges =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   556
                        dis.treeToRange(node.getEnclosingExpression());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   557
                Wrap enclosingWrap = Wrap.rangeWrap(compileSource, enclosingRanges);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   558
                argWrap = argRange != null ? new CompoundWrap(enclosingWrap,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   559
                                                              Wrap.simpleWrap(","),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   560
                                                              argWrap)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   561
                                           : enclosingWrap;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   562
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   563
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   564
            Range current = dis.treeToRange(node);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   565
            String capturedArgs;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   566
            if (!ad.capturedVariables.isEmpty()) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   567
                capturedArgs = (ad.parameterTypes.isEmpty() ? "" : ", ") +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   568
                               ad.capturedVariables.stream()
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   569
                                                   .map(vd -> vd.name)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   570
                                                   .collect(Collectors.joining(","));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   571
            } else {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   572
                capturedArgs = "";
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   573
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   574
            if (lastPos < current.begin)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   575
                partitionedInit.add(Wrap.rangeWrap(compileSource,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   576
                                                   new Range(lastPos, current.begin)));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   577
            partitionedInit.add(new CompoundWrap("new " + ad.declareTypeName + "(",
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   578
                                                 argWrap,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   579
                                                 capturedArgs,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   580
                                                 ")"));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   581
            lastPos = current.end;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   582
        }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   583
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   584
        if (lastPos < rinit.end)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   585
            partitionedInit.add(Wrap.rangeWrap(compileSource, new Range(lastPos, rinit.end)));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   586
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   587
        return new Pair<>(new CompoundWrap(anonymousDeclarations.toArray()),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   588
                          new CompoundWrap(partitionedInit.toArray()));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   589
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   590
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   591
    private List<Snippet> processExpression(String userSource, Tree tree, String compileSource, ParseTask pt) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   592
        ExpressionStatementTree expr = (ExpressionStatementTree) tree;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   593
        String name = null;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 42827
diff changeset
   594
        ExpressionInfo ei = ExpressionToTypeInfo.expressionInfo(compileSource, state);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   595
        ExpressionTree assignVar;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   596
        Wrap guts;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   597
        Snippet snip;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   598
        if (ei != null && ei.isNonVoid) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   599
            String typeName = ei.typeName;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   600
            SubKind subkind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   601
            if (ei.tree instanceof IdentifierTree) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   602
                IdentifierTree id = (IdentifierTree) ei.tree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   603
                name = id.getName().toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   604
                subkind = SubKind.VAR_VALUE_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   605
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   606
            } else if (ei.tree instanceof AssignmentTree
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   607
                    && (assignVar = ((AssignmentTree) ei.tree).getVariable()) instanceof IdentifierTree) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   608
                name = assignVar.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   609
                subkind = SubKind.ASSIGNMENT_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   610
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   611
                subkind = SubKind.OTHER_EXPRESSION_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   612
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   613
            if (shouldGenTempVar(subkind)) {
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   614
                if (preserveState) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   615
                    name = "$$";
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   616
                } else {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   617
                    if (state.tempVariableNameGenerator != null) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   618
                        name = state.tempVariableNameGenerator.get();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   619
                    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   620
                    while (name == null || state.keyMap.doesVariableNameExist(name)) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   621
                        name = "$" + ++varNumber;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 43586
diff changeset
   622
                    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   623
                }
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   624
                ExpressionInfo varEI =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   625
                        ExpressionToTypeInfo.localVariableTypeForInitializer(compileSource, state, true);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   626
                String declareTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   627
                String fullTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   628
                String displayTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   629
                Set<String> anonymousClasses;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   630
                if (varEI != null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   631
                    declareTypeName = varEI.declareTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   632
                    fullTypeName = varEI.fullTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   633
                    displayTypeName = varEI.displayTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   634
55397
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55156
diff changeset
   635
                    TreeDissector dis = TreeDissector.createByFirstClass(pt);
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   636
                    Pair<Wrap, Wrap> anonymous2Member =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   637
                            anonymous2Member(varEI, compileSource, new Range(0, compileSource.length()), dis, expr.getExpression());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   638
                    guts = Wrap.tempVarWrap(anonymous2Member.second.wrapped(), declareTypeName, name, anonymous2Member.first);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   639
                    anonymousClasses = varEI.anonymousClasses.stream().map(ad -> ad.declareTypeName).collect(Collectors.toSet());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   640
                } else {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   641
                    declareTypeName = ei.accessibleTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   642
                    displayTypeName = fullTypeName = typeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   643
                    guts = Wrap.tempVarWrap(compileSource, declareTypeName, name, null);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   644
                    anonymousClasses = Collections.emptySet();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   645
                }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   646
                Collection<String> declareReferences = null; //TODO
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   647
                snip = new VarSnippet(state.keyMap.keyForVariable(name), userSource, guts,
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
   648
                        name, SubKind.TEMP_VAR_EXPRESSION_SUBKIND, displayTypeName, fullTypeName, anonymousClasses, declareReferences, null);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   649
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   650
                guts = Wrap.methodReturnWrap(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   651
                snip = new ExpressionSnippet(state.keyMap.keyForExpression(name, typeName), userSource, guts,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   652
                        name, subkind);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   653
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   654
        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   655
            guts = Wrap.methodWrap(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   656
            if (ei == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   657
                // 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
   658
                DiagList dl = trialCompile(guts);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   659
                if (dl.hasNotStatement()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   660
                    guts = Wrap.methodReturnWrap(compileSource);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   661
                    dl = trialCompile(guts);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   662
                }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   663
                if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   664
                    return compileFailResult(dl, userSource, Kind.EXPRESSION);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   665
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   666
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   667
            snip = new StatementSnippet(state.keyMap.keyForStatement(), userSource, guts);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   668
        }
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   669
        return singletonList(snip);
33362
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
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   672
    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
   673
        TreeDependencyScanner tds = new TreeDependencyScanner();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   674
        tds.scan(unitTree);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   675
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34752
diff changeset
   676
        TreeDissector dis = TreeDissector.createByFirstClass(pt);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   677
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   678
        ClassTree klassTree = (ClassTree) unitTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   679
        String name = klassTree.getSimpleName().toString();
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   680
        DiagList modDiag = modifierDiagnostics(klassTree.getModifiers(), dis, false);
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents: 36160
diff changeset
   681
        TypeDeclKey key = state.keyMap.keyForClass(name);
55397
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55156
diff changeset
   682
        // Corralling
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55156
diff changeset
   683
        Wrap corralled = new Corraller(dis, key.index(), compileSource).corralType(klassTree);
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   684
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   685
        Wrap guts = Wrap.classMemberWrap(compileSource);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   686
        Snippet snip = new TypeDeclSnippet(key, userSource, guts,
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   687
                name, snippetKind,
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   688
                corralled, tds.declareReferences(), tds.bodyReferences(), modDiag);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   689
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   690
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   691
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   692
    private List<Snippet> processStatement(String userSource, String compileSource) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   693
        Wrap guts = Wrap.methodWrap(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   694
        // Check for unreachable by trying
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   695
        DiagList dl = trialCompile(guts);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   696
        if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   697
            if (dl.hasUnreachableError()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   698
                guts = Wrap.methodUnreachableSemiWrap(compileSource);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   699
                dl = trialCompile(guts);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   700
                if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   701
                    if (dl.hasUnreachableError()) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   702
                        // Without ending semicolon
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   703
                        guts = Wrap.methodUnreachableWrap(compileSource);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   704
                        dl = trialCompile(guts);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   705
                    }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   706
                    if (dl.hasErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   707
                        return compileFailResult(dl, userSource, Kind.STATEMENT);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   708
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   709
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   710
            } else {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   711
                return compileFailResult(dl, userSource, Kind.STATEMENT);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   712
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   713
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   714
        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
   715
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   716
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   717
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   718
    private DiagList trialCompile(Wrap guts) {
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   719
        OuterWrap outer = state.outerMap.wrapInTrialClass(guts);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   720
        return state.taskFactory.analyze(outer, AnalyzeTask::getDiagnostics);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   721
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   722
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   723
    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
   724
        TreeDependencyScanner tds = new TreeDependencyScanner();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   725
        tds.scan(unitTree);
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   726
        final TreeDissector dis = TreeDissector.createByFirstClass(pt);
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   727
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   728
        final MethodTree mt = (MethodTree) unitTree;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   729
        final String name = mt.getName().toString();
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   730
        if (objectMethods.contains(name)) {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   731
            // 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
   732
            // fails, see 8187137.  Generate a descriptive error message
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   733
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   734
            // 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
   735
            long possibleStart = dis.getEndPosition(mt.getReturnType());
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   736
            Range possibleRange = new Range((int) possibleStart,
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   737
                    dis.getStartPosition(mt.getBody()));
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   738
            String possibleNameSection = possibleRange.part(compileSource);
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   739
            int offset = possibleNameSection.indexOf(name);
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   740
            long start = offset < 0
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   741
                    ? possibleStart // something wrong, punt
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   742
                    : possibleStart + offset;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   743
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
   744
            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
   745
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   746
        String parameterTypes
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   747
                = mt.getParameters()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   748
                .stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   749
                .map(param -> dis.treeToRange(param.getType()).part(compileSource))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   750
                .collect(Collectors.joining(","));
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   751
        Tree returnType = mt.getReturnType();
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   752
        DiagList modDiag = modifierDiagnostics(mt.getModifiers(), dis, true);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   753
        MethodKey key = state.keyMap.keyForMethod(name, parameterTypes);
55397
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55156
diff changeset
   754
        // Corralling
5eeee2cc94f5 8200701: jdk/jshell/ExceptionsTest.java fails on Windows, after JDK-8198801
rfield
parents: 55156
diff changeset
   755
        Wrap corralled = new Corraller(dis, key.index(), compileSource).corralMethod(mt);
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   756
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   757
        if (modDiag.hasErrors()) {
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   758
            return compileFailResult(modDiag, userSource, Kind.METHOD);
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   759
        }
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   760
        Wrap guts = Wrap.classMemberWrap(compileSource);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   761
        Range typeRange = dis.treeToRange(returnType);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   762
        String signature = "(" + parameterTypes + ")" + typeRange.part(compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   763
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   764
        Snippet snip = new MethodSnippet(key, userSource, guts,
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   765
                name, signature,
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   766
                corralled, tds.declareReferences(), tds.bodyReferences(), modDiag);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   767
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   768
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   769
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   770
    private Kind kindOfTree(Tree tree) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   771
        switch (tree.getKind()) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   772
            case IMPORT:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   773
                return Kind.IMPORT;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   774
            case VARIABLE:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   775
                return Kind.VAR;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   776
            case EXPRESSION_STATEMENT:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   777
                return Kind.EXPRESSION;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   778
            case CLASS:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   779
            case ENUM:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   780
            case ANNOTATION_TYPE:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   781
            case INTERFACE:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   782
                return Kind.TYPE_DECL;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   783
            case METHOD:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   784
                return Kind.METHOD;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   785
            default:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   786
                return Kind.STATEMENT;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   787
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   788
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   789
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   790
    /**
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   791
     * The snippet has failed, return with the rejected snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   792
     *
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   793
     * @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
   794
     * @param userSource the incoming bad user source
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   795
     * @return a rejected snippet
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   796
     */
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   797
    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
   798
        return compileFailResult(xt.getDiagnostics(), userSource, probableKind);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   799
    }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   800
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   801
    /**
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   802
     * The snippet has failed, return with the rejected snippet
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   803
     *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   804
     * @param diags the failure diagnostics
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   805
     * @param userSource the incoming bad user source
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   806
     * @return a rejected snippet
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   807
     */
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   808
    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
   809
        ErroneousKey key = state.keyMap.keyForErroneous();
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   810
        Snippet snip = new ErroneousSnippet(key, userSource, null,
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   811
                probableKind, SubKind.UNKNOWN_SUBKIND);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   812
        snip.setFailed(diags);
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   813
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   814
        // Install  wrapper for query by SourceCodeAnalysis.wrapper
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   815
        String compileSource = Util.trimEnd(new MaskCommentsAndModifiers(userSource, true).cleared());
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   816
        OuterWrap outer;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   817
        switch (probableKind) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   818
            case IMPORT:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   819
                outer = state.outerMap.wrapImport(Wrap.simpleWrap(compileSource), snip);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   820
                break;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   821
            case EXPRESSION:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   822
                outer = state.outerMap.wrapInTrialClass(Wrap.methodReturnWrap(compileSource));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   823
                break;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   824
            case VAR:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   825
            case TYPE_DECL:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   826
            case METHOD:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   827
                outer = state.outerMap.wrapInTrialClass(Wrap.classMemberWrap(compileSource));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   828
                break;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   829
            default:
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   830
                outer = state.outerMap.wrapInTrialClass(Wrap.methodWrap(compileSource));
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   831
                break;
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   832
        }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   833
        snip.setOuterWrap(outer);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   834
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   835
        return singletonList(snip);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   836
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   837
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   838
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   839
     * 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
   840
     *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   841
     * @param snippetKind
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   842
     * @return
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   843
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   844
    private boolean shouldGenTempVar(SubKind snippetKind) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   845
        return snippetKind == SubKind.OTHER_EXPRESSION_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   846
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   847
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   848
    List<SnippetEvent> drop(Snippet si) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   849
        Unit c = new Unit(state, si);
41514
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   850
        Set<Unit> outs;
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   851
        if (si instanceof PersistentSnippet) {
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   852
            Set<Unit> ins = c.dependents().collect(toSet());
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   853
            outs = compileAndLoad(ins);
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   854
        } else {
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   855
            outs = Collections.emptySet();
a75c2b869d8d 8167128: JShell: /drop of statement gives confusing output
rfield
parents: 40769
diff changeset
   856
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   857
        return events(c, outs, null, null);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   858
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   859
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   860
    private List<SnippetEvent> declare(Snippet si, DiagList generatedDiagnostics) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   861
        Unit c = new Unit(state, si, null, generatedDiagnostics);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   862
        Set<Unit> ins = new LinkedHashSet<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   863
        ins.add(c);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   864
        Set<Unit> outs = compileAndLoad(ins);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   865
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38535
diff changeset
   866
        if (!si.status().isDefined()
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   867
                && si.diagnostics().isEmpty()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   868
                && si.unresolved().isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   869
            // 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
   870
            si.setDiagnostics(outs.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   871
                    .flatMap(u -> u.snippet().diagnostics().stream())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   872
                    .collect(Collectors.toCollection(DiagList::new)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   873
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   874
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   875
        // If appropriate, execute the snippet
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   876
        String value = null;
37751
77e7bb904a13 8139837: JShell API: make a common JShellException
rfield
parents: 37644
diff changeset
   877
        JShellException exception = null;
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38535
diff changeset
   878
        if (si.status().isDefined()) {
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   879
            if (si.isExecutable()) {
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   880
                try {
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   881
                    value = state.executionControl().invoke(si.classFullName(), DOIT_METHOD_NAME);
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   882
                    value = si.subKind().hasValue()
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   883
                            ? expunge(value)
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   884
                            : "";
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   885
                } catch (ResolutionException ex) {
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   886
                    exception = asUnresolvedReferenceException(ex);
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   887
                } catch (UserException ex) {
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   888
                    exception = asEvalException(ex);
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   889
                } catch (RunException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   890
                    // StopException - no-op
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   891
                } catch (InternalException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   892
                    state.debug(ex, "invoke");
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   893
                } catch (EngineTerminationException ex) {
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   894
                    state.debug(ex, "termination");
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
   895
                    state.closeDown();
37389
9c137b83a8b8 8143955: JShell tool (UX): Output structure
rfield
parents: 37005
diff changeset
   896
                }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   897
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   898
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   899
        return events(c, outs, value, exception);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   900
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   901
49515
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   902
    // Convert an internal UserException to an API EvalException, translating
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   903
    // the stack to snippet form.  Convert any chained exceptions
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   904
    private EvalException asEvalException(UserException ue) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   905
        return new EvalException(ue.getMessage(),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   906
                ue.causeExceptionClass(),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   907
                translateExceptionStack(ue),
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   908
                asJShellException(ue.getCause()));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   909
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   910
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   911
    // Convert an internal ResolutionException to an API UnresolvedReferenceException,
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   912
    // translating the snippet id to snipper and the stack to snippet form
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   913
    private UnresolvedReferenceException asUnresolvedReferenceException(ResolutionException re) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   914
        DeclarationSnippet sn = (DeclarationSnippet) state.maps.getSnippetDeadOrAlive(re.id());
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   915
        return new UnresolvedReferenceException(sn, translateExceptionStack(re));
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   916
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   917
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   918
    // Convert an internal UserException/ResolutionException to an API
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   919
    // EvalException/UnresolvedReferenceException
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   920
    private JShellException asJShellException(Throwable e) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   921
        if (e == null) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   922
            return null;
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   923
        } else if (e instanceof UserException) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   924
            return asEvalException((UserException) e);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   925
        } else if (e instanceof ResolutionException) {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   926
            return asUnresolvedReferenceException((ResolutionException) e);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   927
        } else {
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   928
            throw new AssertionError(e);
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   929
        }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   930
    }
083318155ad1 8198801: JShell: user exception chained cause not retained
rfield
parents: 48610
diff changeset
   931
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   932
    private boolean interestingEvent(SnippetEvent e) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   933
        return e.isSignatureChange()
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   934
                    || e.causeSnippet() == null
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   935
                    || e.status() != e.previousStatus()
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   936
                    || e.exception() != null;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   937
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   938
37751
77e7bb904a13 8139837: JShell API: make a common JShellException
rfield
parents: 37644
diff changeset
   939
    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
   940
        List<SnippetEvent> events = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   941
        events.add(c.event(value, exception));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   942
        events.addAll(outs.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   943
                .filter(u -> u != c)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   944
                .map(u -> u.event(null, null))
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   945
                .filter(this::interestingEvent)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   946
                .collect(Collectors.toList()));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   947
        events.addAll(outs.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   948
                .flatMap(u -> u.secondaryEvents().stream())
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   949
                .filter(this::interestingEvent)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   950
                .collect(Collectors.toList()));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   951
        //System.err.printf("Events: %s\n", events);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   952
        return events;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   953
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   954
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   955
    private Set<OuterWrap> outerWrapSet(Collection<Unit> units) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   956
        return units.stream()
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   957
                .map(u -> u.snippet().outerWrap())
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   958
                .collect(toSet());
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   959
    }
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   960
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   961
    private Set<Unit> compileAndLoad(Set<Unit> ins) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   962
        if (ins.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   963
            return ins;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   964
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   965
        Set<Unit> replaced = new LinkedHashSet<>();
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   966
        // Loop until dependencies and errors are stable
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   967
        while (true) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   968
            state.debug(DBG_GEN, "compileAndLoad  %s\n", ins);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   969
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42272
diff changeset
   970
            ins.stream().forEach(Unit::initialize);
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
   971
            ins.stream().forEach(u -> u.setWrap(ins, ins));
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   972
            state.taskFactory.analyze(outerWrapSet(ins), at -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   973
                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
   974
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   975
                // corral any Snippets that need it
55156
1c291d1c272c 8199623: JShell: corralling not restored on drop
rfield
parents: 55142
diff changeset
   976
                if (ins.stream().filter(u -> u.corralIfNeeded(ins)).count() > 0) {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   977
                    // if any were corralled, re-analyze everything
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   978
                    state.taskFactory.analyze(outerWrapSet(ins), cat -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   979
                        ins.stream().forEach(u -> u.setCorralledDiagnostics(cat));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   980
                        ins.stream().forEach(u -> u.setStatus(cat));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   981
                        return null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   982
                    });
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   983
                } else {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   984
                    ins.stream().forEach(u -> u.setStatus(at));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   985
                }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   986
                return null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
   987
            });
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   988
            // compile and load the legit snippets
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   989
            boolean success;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   990
            while (true) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   991
                List<Unit> legit = ins.stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42272
diff changeset
   992
                        .filter(Unit::isDefined)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   993
                        .collect(toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   994
                state.debug(DBG_GEN, "compileAndLoad ins = %s -- legit = %s\n",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   995
                        ins, legit);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   996
                if (legit.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   997
                    // no class files can be generated
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   998
                    success = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   999
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1000
                    // re-wrap with legit imports
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1001
                    legit.stream().forEach(u -> u.setWrap(ins, legit));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1002
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1003
                    // generate class files for those capable
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1004
                    Result res = state.taskFactory.compile(outerWrapSet(legit), ct -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1005
                        if (!ct.compile()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1006
                            // oy! compile failed because of recursive new unresolved
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1007
                            if (legit.stream()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1008
                                    .filter(u -> u.smashingErrorDiagnostics(ct))
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1009
                                    .count() > 0) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1010
                                // try again, with the erroreous removed
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1011
                                return Result.CONTINUE;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1012
                            } else {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1013
                                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
  1014
                                        legit);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1015
                            }
33362
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
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1018
                        // load all new classes
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1019
                        load(legit.stream()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1020
                                .flatMap(u -> u.classesToLoad(ct.classList(u.snippet().outerWrap())))
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1021
                                .collect(toSet()));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1022
                        // attempt to redefine the remaining classes
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1023
                        List<Unit> toReplace = legit.stream()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1024
                                .filter(u -> !u.doRedefines())
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1025
                                .collect(toList());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1026
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1027
                        // prevent alternating redefine/replace cyclic dependency
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1028
                        // loop by replacing all that have been replaced
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1029
                        if (!toReplace.isEmpty()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1030
                            replaced.addAll(toReplace);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1031
                            replaced.stream().forEach(Unit::markForReplacement);
48208
0a8db756a7e7 8189248: Jshell: error with mutually dependent snippets, when one must be replaced
jlahoda
parents: 47975
diff changeset
  1032
                            //ensure correct classnames are set in the snippets:
0a8db756a7e7 8189248: Jshell: error with mutually dependent snippets, when one must be replaced
jlahoda
parents: 47975
diff changeset
  1033
                            replaced.stream().forEach(u -> u.setWrap(ins, legit));
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1034
                        }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1035
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1036
                        return toReplace.isEmpty() ? Result.SUCESS : Result.FAILURE;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1037
                    });
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1038
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1039
                    switch (res) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1040
                        case CONTINUE: continue;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1041
                        case SUCESS: success = true; break;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1042
                        default:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1043
                        case FAILURE: success = false; break;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1044
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1045
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1046
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1047
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1048
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1049
            // add any new dependencies to the working set
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1050
            List<Unit> newDependencies = ins.stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42272
diff changeset
  1051
                    .flatMap(Unit::effectedDependents)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1052
                    .collect(toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1053
            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
  1054
                    ins, newDependencies, success);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1055
            if (!ins.addAll(newDependencies) && success) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1056
                // all classes that could not be directly loaded (because they
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1057
                // are new) have been redefined, and no new dependnencies were
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1058
                // identified
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42272
diff changeset
  1059
                ins.stream().forEach(Unit::finish);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1060
                return ins;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1061
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1062
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1063
    }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1064
    //where:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1065
        enum Result {SUCESS, FAILURE, CONTINUE}
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1066
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
  1067
    /**
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
  1068
     * 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
  1069
     * @param classbytecodes names of the classes to load.
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 37751
diff changeset
  1070
     */
42272
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41858
diff changeset
  1071
    private void load(Collection<ClassBytecodes> classbytecodes) {
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41858
diff changeset
  1072
        if (!classbytecodes.isEmpty()) {
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41858
diff changeset
  1073
            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
  1074
            try {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
  1075
                state.executionControl().load(cbcs);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
  1076
                state.classTracker.markLoaded(cbcs);
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
  1077
            } catch (ClassInstallException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
  1078
                state.classTracker.markLoaded(cbcs, ex.installed());
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
  1079
            } catch (NotImplementedException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
  1080
                state.debug(ex, "Seriously?!? load not implemented");
42272
82e273c4f2b3 8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents: 41858
diff changeset
  1081
                state.closeDown();
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
  1082
            } catch (EngineTerminationException ex) {
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
  1083
                state.closeDown();
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39370
diff changeset
  1084
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1085
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1086
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1087
40769
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
  1088
    private StackTraceElement[] translateExceptionStack(Exception ex) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1089
        StackTraceElement[] raw = ex.getStackTrace();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1090
        int last = raw.length;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1091
        do {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1092
            if (last == 0) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1093
                last = raw.length - 1;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1094
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1095
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1096
        } while (!isWrap(raw[--last]));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1097
        StackTraceElement[] elems = new StackTraceElement[last + 1];
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1098
        for (int i = 0; i <= last; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1099
            StackTraceElement r = raw[i];
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
  1100
            OuterSnippetsClassWrap outer = state.outerMap.getOuter(r.getClassName());
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
  1101
            if (outer != null) {
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
  1102
                String klass = expunge(r.getClassName());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1103
                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
  1104
                int wln = r.getLineNumber() - 1;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
  1105
                int line = outer.wrapLineToSnippetLine(wln) + 1;
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
  1106
                Snippet sn = outer.wrapLineToSnippet(wln);
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
  1107
                String file = "#" + sn.id();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1108
                elems[i] = new StackTraceElement(klass, method, file, line);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1109
            } else if (r.getFileName().equals("<none>")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1110
                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
  1111
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1112
                elems[i] = r;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1113
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1114
        }
40769
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
  1115
        return elems;
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
  1116
    }
e57f1a5c9346 8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents: 39807
diff changeset
  1117
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1118
    private boolean isWrap(StackTraceElement ste) {
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 37389
diff changeset
  1119
        return PREFIX_PATTERN.matcher(ste.getClassName()).find();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1120
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1121
47499
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1122
    /**
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1123
     * 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
  1124
     * @param name the method name
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1125
     * @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
  1126
     * @return the generated diagnostic
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1127
     */
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1128
    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
  1129
        return new Diag() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1130
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1131
            public boolean isError() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1132
                return true;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1133
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1134
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1135
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1136
            public long getPosition() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1137
                return nameStart;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1138
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1139
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1140
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1141
            public long getStartPosition() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1142
                return nameStart;
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1143
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1144
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1145
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1146
            public long getEndPosition() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1147
                return nameStart + name.length();
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1148
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1149
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1150
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1151
            public String getCode() {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1152
                return "jdk.eval.error.object.method";
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1153
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1154
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1155
            @Override
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1156
            public String getMessage(Locale locale) {
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1157
                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
  1158
                        String.join(" ", objectMethods));
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1159
            }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1160
        };
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1161
    }
b3ea71b70f7b 8187359: JShell: Give comprehensible error when user method name matches Object method
rfield
parents: 47350
diff changeset
  1162
55142
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1163
    private class ModifierDiagnostic extends Diag {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1164
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1165
            final boolean fatal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1166
            final String message;
55142
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1167
            final long start;
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1168
            final long end;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1169
55142
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1170
            ModifierDiagnostic(boolean fatal,
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1171
                    final String message,
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1172
                    long start,
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1173
                    long end) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1174
                this.fatal = fatal;
55142
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1175
                this.message = message;
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1176
                this.start = start;
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1177
                this.end = end;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1178
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1179
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1180
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1181
            public boolean isError() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1182
                return fatal;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1183
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1184
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1185
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1186
            public long getPosition() {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1187
                return start;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1188
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1189
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1190
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1191
            public long getStartPosition() {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1192
                return start;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1193
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1194
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1195
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1196
            public long getEndPosition() {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47318
diff changeset
  1197
                return end;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1198
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1199
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1200
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1201
            public String getCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1202
                return fatal
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1203
                        ? "jdk.eval.error.illegal.modifiers"
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1204
                        : "jdk.eval.warn.illegal.modifiers";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1205
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1206
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1207
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1208
            public String getMessage(Locale locale) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1209
                return message;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1210
            }
55142
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1211
    }
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1212
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1213
    private DiagList modifierDiagnostics(ModifiersTree modtree,
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1214
                                         final TreeDissector dis, boolean isAbstractProhibited) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1215
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1216
        List<Modifier> list = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1217
        boolean fatal = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1218
        for (Modifier mod : modtree.getFlags()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1219
            switch (mod) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1220
                case SYNCHRONIZED:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1221
                case NATIVE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1222
                    list.add(mod);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1223
                    fatal = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1224
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1225
                case ABSTRACT:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1226
                    if (isAbstractProhibited) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1227
                        list.add(mod);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1228
                        fatal = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1229
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1230
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1231
                case PUBLIC:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1232
                case PROTECTED:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1233
                case PRIVATE:
41858
5843b57ce3a6 8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents: 41514
diff changeset
  1234
                    // 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
  1235
                    break;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1236
                case STATIC:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1237
                case FINAL:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1238
                    list.add(mod);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1239
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1240
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1241
        }
55142
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1242
        if (list.isEmpty()) {
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1243
            return new DiagList();
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1244
        } else {
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1245
            StringBuilder sb = new StringBuilder();
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1246
            for (Modifier mod : list) {
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1247
                sb.append("'");
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1248
                sb.append(mod.toString());
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1249
                sb.append("' ");
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1250
            }
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1251
            String key = (list.size() > 1)
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1252
                    ? fatal
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1253
                    ? "jshell.diag.modifier.plural.fatal"
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1254
                    : "jshell.diag.modifier.plural.ignore"
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1255
                    : fatal
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1256
                    ? "jshell.diag.modifier.single.fatal"
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1257
                    : "jshell.diag.modifier.single.ignore";
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1258
            String message = state.messageFormat(key, sb.toString().trim());
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1259
            return new DiagList(new ModifierDiagnostic(fatal, message,
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1260
                    dis.getStartPosition(modtree), dis.getEndPosition(modtree)));
e2dbcc6ed36d 8080353: JShell: Better error message on attempting to add default method
rfield
parents: 52775
diff changeset
  1261
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1262
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1263
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
  1264
    String computeDeclareName(TypeSymbol ts) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
  1265
        return Util.JSHELL_ANONYMOUS + "$" + Long.toUnsignedString(anonCount++);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 48208
diff changeset
  1266
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1267
}