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