src/jdk.jshell/share/classes/jdk/jshell/ExpressionToTypeInfo.java
author darcy
Tue, 01 Oct 2019 20:07:30 -0700
changeset 58436 91d236715160
parent 48610 a587f95313f1
permissions -rw-r--r--
8231545: Suppress warnings on non-serializable instance fields in jdk.jshell module Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
     1
/*
58436
91d236715160 8231545: Suppress warnings on non-serializable instance fields in jdk.jshell module
darcy
parents: 48610
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
     4
 *
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    10
 *
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    15
 * accompanied this code).
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    16
 *
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    20
 *
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    23
 * questions.
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    24
 */
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    25
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    26
package jdk.jshell;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    27
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    28
import java.util.EnumSet;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    29
import java.util.HashMap;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    30
import java.util.HashSet;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    31
import java.util.Map;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    32
import java.util.Set;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    33
import java.util.function.Function;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    34
import javax.lang.model.element.Element;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    35
import javax.lang.model.element.ElementKind;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    36
import javax.lang.model.element.VariableElement;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    37
import com.sun.source.tree.ReturnTree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    38
import com.sun.source.tree.ClassTree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    39
import com.sun.source.tree.CompilationUnitTree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    40
import com.sun.source.tree.ConditionalExpressionTree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    41
import com.sun.source.tree.ExpressionTree;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    42
import com.sun.source.tree.IdentifierTree;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    43
import com.sun.source.tree.MethodInvocationTree;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    44
import com.sun.source.tree.MethodTree;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    45
import com.sun.source.tree.NewClassTree;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    46
import com.sun.source.tree.Tree;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    47
import com.sun.source.tree.Tree.Kind;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    48
import com.sun.source.tree.VariableTree;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    49
import com.sun.source.util.TreePath;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    50
import com.sun.source.util.TreePathScanner;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    51
import com.sun.source.util.TreeScanner;
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
    52
import com.sun.tools.javac.code.Flags;
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
    53
import com.sun.tools.javac.code.Symbol;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    54
import com.sun.tools.javac.code.Symbol.TypeSymbol;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    55
import com.sun.tools.javac.code.Symtab;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    56
import com.sun.tools.javac.code.Type;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    57
import com.sun.tools.javac.code.Types;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    58
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    59
import com.sun.tools.javac.tree.TreeInfo;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    60
import com.sun.tools.javac.util.List;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    61
import com.sun.tools.javac.util.ListBuffer;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    62
import jdk.jshell.ExpressionToTypeInfo.ExpressionInfo.AnonymousDescription;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    63
import jdk.jshell.ExpressionToTypeInfo.ExpressionInfo.AnonymousDescription.VariableDesc;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    64
import jdk.jshell.TaskFactory.AnalyzeTask;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    65
import jdk.jshell.TypePrinter.AnonymousTypeKind;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    66
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    67
/**
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    68
 * Compute information about an expression string, particularly its type name.
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    69
 */
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    70
class ExpressionToTypeInfo {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    71
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    72
    private static final String OBJECT_TYPE_NAME = "Object";
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    73
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    74
    final AnalyzeTask at;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    75
    final CompilationUnitTree cu;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    76
    final JShell state;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    77
    final boolean computeEnhancedInfo;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    78
    final boolean enhancedTypesAccessible;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    79
    final Symtab syms;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    80
    final Types types;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    81
    final Map<TypeSymbol, String> anon2Name = new HashMap<>();
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    82
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    83
    private ExpressionToTypeInfo(AnalyzeTask at, CompilationUnitTree cu, JShell state,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    84
                                 boolean computeEnhancedInfo, boolean enhancedTypesAccessible) {
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    85
        this.at = at;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    86
        this.cu = cu;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    87
        this.state = state;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    88
        this.computeEnhancedInfo = computeEnhancedInfo;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    89
        this.enhancedTypesAccessible = enhancedTypesAccessible;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    90
        this.syms = Symtab.instance(at.context);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    91
        this.types = Types.instance(at.context);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    92
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    93
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    94
    public static class ExpressionInfo {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    95
        ExpressionTree tree;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    96
        boolean isPrimitiveType;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    97
        String typeName;
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
    98
        String accessibleTypeName;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
    99
        /* In result of localVariableTypeForInitializer, the type that should be used
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   100
         * as a declaration type of the field. This does not include intersection types,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   101
         * but does contain references to anonymous types converted to member types.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   102
         */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   103
        String declareTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   104
        /* In result of localVariableTypeForInitializer, the apparent/infered type of
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   105
         * the variable. This includes intersection types, and references to anonymous
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   106
         * types converted to member types.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   107
         */
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   108
        String fullTypeName;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   109
        /* In result of localVariableTypeForInitializer, the human readable type of
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   110
         * the variable. This includes intersection types, and human readable descriptions
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   111
         * of anonymous types.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   112
         */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   113
        String displayTypeName;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   114
        boolean isNonVoid;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   115
        /* In result of localVariableTypeForInitializer, description of important anonymous
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   116
         * classes.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   117
         */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   118
        List<AnonymousDescription> anonymousClasses = List.nil();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   119
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   120
        /* A description of an anonymous class. */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   121
        static class AnonymousDescription {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   122
            /* Parameter types of the invoked super constructor.*/
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   123
            List<String> parameterTypes;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   124
            /* Type of the base/enclosing expression, if any.*/
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   125
            String enclosingInstanceType;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   126
            /* The denotable name of the supertype.*/
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   127
            String superTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   128
            /* The human-readable name of this class.*/
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   129
            String declareTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   130
            /* If the supertype of this anonymous is a class. */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   131
            boolean isClass;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   132
            /* Variables captured by this anonymous class*/
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   133
            List<VariableDesc> capturedVariables;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   134
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   135
            static class VariableDesc {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   136
                String type;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   137
                String name;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   138
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   139
                public VariableDesc(String type, String name) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   140
                    this.type = type;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   141
                    this.name = name;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   142
                }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   143
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   144
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   145
        }
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   146
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   147
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   148
    // return mechanism and other general structure from TreePath.getPath()
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   149
    private static class Result extends Error {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   150
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   151
        static final long serialVersionUID = -5942088234594905629L;
58436
91d236715160 8231545: Suppress warnings on non-serializable instance fields in jdk.jshell module
darcy
parents: 48610
diff changeset
   152
        @SuppressWarnings("serial") // Not statically typed as Serializable
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   153
        final TreePath expressionPath;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   154
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   155
        Result(TreePath path) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   156
            this.expressionPath = path;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   157
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   158
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   159
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   160
    private static class PathFinder extends TreePathScanner<TreePath, Boolean> {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   161
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   162
        // Optimize out imports etc
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   163
        @Override
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   164
        public TreePath visitCompilationUnit(CompilationUnitTree node, Boolean isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   165
            return scan(node.getTypeDecls(), isTargetContext);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   166
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   167
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   168
        // Only care about members
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   169
        @Override
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   170
        public TreePath visitClass(ClassTree node, Boolean isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   171
            return scan(node.getMembers(), isTargetContext);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   172
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   173
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   174
        // Only want the doit method where the code is
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   175
        @Override
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   176
        public TreePath visitMethod(MethodTree node, Boolean isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   177
            if (Util.isDoIt(node.getName())) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   178
                return scan(node.getBody(), true);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   179
            } else {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   180
                return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   181
            }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   182
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   183
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   184
        @Override
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   185
        public TreePath visitReturn(ReturnTree node, Boolean isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   186
            ExpressionTree tree = node.getExpression();
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   187
            TreePath tp = new TreePath(getCurrentPath(), tree);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   188
            if (isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   189
                throw new Result(tp);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   190
            } else {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   191
                return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   192
            }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   193
        }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   194
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   195
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   196
        public TreePath visitVariable(VariableTree node, Boolean isTargetContext) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   197
            if (isTargetContext) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   198
                throw new Result(getCurrentPath());
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   199
            } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   200
                return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   201
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   202
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   203
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   204
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   205
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   206
    private Type pathToType(TreePath tp) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   207
        return (Type) at.trees().getTypeMirror(tp);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   208
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   209
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   210
    private Type pathToType(TreePath tp, Tree tree) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   211
        if (tree instanceof ConditionalExpressionTree) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   212
            // Conditionals always wind up as Object -- this corrects
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   213
            ConditionalExpressionTree cet = (ConditionalExpressionTree) tree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   214
            Type tmt = pathToType(new TreePath(tp, cet.getTrueExpression()));
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   215
            Type tmf = pathToType(new TreePath(tp, cet.getFalseExpression()));
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   216
            if (!tmt.isPrimitive() && !tmf.isPrimitive()) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   217
                Type lub = types.lub(tmt, tmf);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   218
                // System.err.printf("cond ? %s : %s  --  lub = %s\n",
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   219
                //             varTypeName(tmt), varTypeName(tmf), varTypeName(lub));
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   220
                return lub;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   221
            }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   222
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   223
        return pathToType(tp);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   224
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   225
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   226
    /**
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   227
     * Entry method: get expression info
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   228
     * @param code the expression as a string
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   229
     * @param state a JShell instance
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   230
     * @return type information
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   231
     */
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   232
    public static ExpressionInfo expressionInfo(String code, JShell state) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   233
        if (code == null || code.isEmpty()) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   234
            return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   235
        }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   236
        OuterWrap codeWrap = state.outerMap.wrapInTrialClass(Wrap.methodReturnWrap(code));
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   237
        try {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   238
            return state.taskFactory.analyze(codeWrap, at -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   239
                CompilationUnitTree cu = at.firstCuTree();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   240
                if (at.hasErrors() || cu == null) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   241
                    return null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   242
                }
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   243
                return new ExpressionToTypeInfo(at, cu, state, false, false).typeOfExpression();
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   244
            });
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   245
        } catch (Exception ex) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   246
            return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   247
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   248
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   249
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   250
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   251
     * Entry method: get expression info corresponding to a local variable declaration if its type
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   252
     * has been inferred automatically from the given initializer.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   253
     * @param code the initializer as a string
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   254
     * @param state a JShell instance
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   255
     * @return type information
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   256
     */
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   257
    public static ExpressionInfo localVariableTypeForInitializer(String code, JShell state, boolean onlyAccessible) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   258
        if (code == null || code.isEmpty()) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   259
            return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   260
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   261
        try {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   262
            OuterWrap codeWrap = state.outerMap.wrapInTrialClass(Wrap.methodWrap("var $$$ = " + code));
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   263
            return state.taskFactory.analyze(codeWrap, at -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   264
                CompilationUnitTree cu = at.firstCuTree();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   265
                if (at.hasErrors() || cu == null) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   266
                    return null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   267
                }
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   268
                return new ExpressionToTypeInfo(at, cu, state, true, onlyAccessible)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   269
                        .typeOfExpression();
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   270
            });
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   271
        } catch (Exception ex) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   272
            return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   273
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   274
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   275
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   276
    /**List (in a stable order) all NewClassTree instances under {@code from} that should be
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   277
     * converted to member classes
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   278
     *
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   279
     * @param from tree to inspect
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   280
     * @return NewClassTree instances that should be converted to member classes
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   281
     */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   282
    public static List<NewClassTree> listAnonymousClassesToConvert(Tree from) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   283
        ListBuffer<NewClassTree> classes = new ListBuffer<>();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   284
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   285
        new TreeScanner<Void, Void>() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   286
            @Override
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   287
            public Void visitNewClass(NewClassTree node, Void p) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   288
                if (node.getClassBody() != null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   289
                    classes.append(node);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   290
                    return null;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   291
                }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   292
                return super.visitNewClass(node, p);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   293
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   294
        }.scan(from, null);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   295
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   296
        return classes.toList();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   297
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   298
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   299
    private ExpressionInfo typeOfExpression() {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   300
        return treeToInfo(findExpressionPath());
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   301
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   302
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   303
    private TreePath findExpressionPath() {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   304
        try {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   305
            new PathFinder().scan(new TreePath(cu), false);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   306
        } catch (Result result) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   307
            return result.expressionPath;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   308
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   309
        return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   310
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   311
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   312
    /**
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   313
     * A type is accessible if it is public or if it is package-private and is a
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   314
     * type defined in JShell.  Additionally, all its type arguments must be
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   315
     * accessible
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   316
     *
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   317
     * @param type the type to check for accessibility
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   318
     * @return true if the type name can be referenced
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   319
     */
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   320
    private boolean isAccessible(Type type) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   321
        Symbol.TypeSymbol tsym = type.asElement();
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   322
        return ((tsym.flags() & Flags.PUBLIC) != 0 ||
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   323
                ((tsym.flags() & Flags.PRIVATE) == 0 &&
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   324
                Util.isInJShellClass(tsym.flatName().toString()))) &&
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   325
                 type.getTypeArguments().stream()
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   326
                        .allMatch(this::isAccessible);
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   327
    }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   328
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   329
    /**
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   330
     * Return the superclass.
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   331
     *
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   332
     * @param type the type
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   333
     * @return the superclass, or Object on error
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   334
     */
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   335
    private Type supertype(Type type) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   336
        Type sup = types.supertype(type);
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   337
        if (sup == Type.noType || sup == null) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   338
            return syms.objectType;
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   339
        }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   340
        return sup;
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   341
    }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   342
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   343
    /**
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   344
     * Find an accessible supertype.
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   345
     *
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   346
     * @param type the type
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   347
     * @return the type, if it is accessible, otherwise a superclass or
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   348
     * interface which is
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   349
     */
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   350
    private List<Type> findAccessibleSupertypes(Type type) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   351
        List<Type> accessible = List.nil();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   352
        Type accessibleSuper = syms.objectType;
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   353
        // Iterate up the superclasses, see if any are accessible
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   354
        for (Type sup = type; !types.isSameType(sup, syms.objectType); sup = supertype(sup)) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   355
            if (isAccessible(sup)) {
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   356
                accessible = accessible.prepend(sup);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   357
                accessibleSuper = sup;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   358
                break;
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   359
            }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   360
        }
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   361
        // then look through superclasses for accessible interfaces
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   362
        for (Type sup = type; !types.isSameType(sup, accessibleSuper); sup = supertype(sup)) {
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   363
            for (Type itf : types.interfaces(sup)) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   364
                if (isAccessible(itf)) {
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   365
                    accessible = accessible.prepend(itf);
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   366
                }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   367
            }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   368
        }
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   369
        if (accessible.isEmpty()) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   370
            // Punt, use Object which is the supertype of everything
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   371
            accessible = accessible.prepend(syms.objectType);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   372
        }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   373
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   374
        return accessible.reverse();
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   375
    }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   376
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   377
    private ExpressionInfo treeToInfo(TreePath tp) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   378
        if (tp != null) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   379
            Tree tree = tp.getLeaf();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   380
            boolean isExpression = tree instanceof ExpressionTree;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   381
            if (isExpression || tree.getKind() == Kind.VARIABLE) {
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   382
                ExpressionInfo ei = new ExpressionInfo();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   383
                if (isExpression)
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   384
                    ei.tree = (ExpressionTree) tree;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   385
                Type type = pathToType(tp, tree);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   386
                if (type != null) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   387
                    switch (type.getKind()) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   388
                        case VOID:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   389
                        case NONE:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   390
                        case ERROR:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   391
                        case OTHER:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   392
                            break;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   393
                        case NULL:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   394
                            ei.isNonVoid = true;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   395
                            ei.typeName = OBJECT_TYPE_NAME;
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   396
                            ei.accessibleTypeName = OBJECT_TYPE_NAME;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   397
                            break;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   398
                        default: {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   399
                            ei.isNonVoid = true;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   400
                            ei.isPrimitiveType = type.isPrimitive();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   401
                            ei.typeName = varTypeName(type, false, AnonymousTypeKind.SUPER);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   402
                            List<Type> accessibleTypes = findAccessibleSupertypes(type);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   403
                            ei.accessibleTypeName =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   404
                                    varTypeName(accessibleTypes.head, false, AnonymousTypeKind.SUPER);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   405
                            if (computeEnhancedInfo) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   406
                                Type accessibleType = accessibleTypes.size() == 1 ? accessibleTypes.head
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   407
                                            : types.makeIntersectionType(accessibleTypes);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   408
                                ei.declareTypeName =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   409
                                        varTypeName(accessibleType, false, AnonymousTypeKind.DECLARE);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   410
                                ei.fullTypeName =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   411
                                        varTypeName(enhancedTypesAccessible ? accessibleType : type,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   412
                                                    true, AnonymousTypeKind.DECLARE);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   413
                                ei.displayTypeName =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   414
                                        varTypeName(type, true, AnonymousTypeKind.DISPLAY);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   415
                            }
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   416
                            break;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   417
                        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   418
                    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   419
                }
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   420
                if (tree.getKind() == Tree.Kind.VARIABLE && computeEnhancedInfo) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   421
                    Tree init = ((VariableTree) tree).getInitializer();
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   422
                    for (NewClassTree node : listAnonymousClassesToConvert(init)) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   423
                        Set<VariableElement> captured = capturedVariables(at,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   424
                                                                          tp.getCompilationUnit(),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   425
                                                                          node);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   426
                        JCClassDecl clazz = (JCClassDecl) node.getClassBody();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   427
                        MethodInvocationTree superCall =
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   428
                                clazz.getMembers()
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   429
                                     .stream()
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   430
                                     .map(TreeInfo::firstConstructorCall)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   431
                                     .findAny()
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   432
                                     .get();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   433
                        TreePath superCallPath
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   434
                                = at.trees().
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   435
                                        getPath(tp.getCompilationUnit(), superCall.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   436
                                                getMethodSelect());
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   437
                        Type constrType = pathToType(superCallPath);
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   438
                        AnonymousDescription desc = new AnonymousDescription();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   439
                        desc.parameterTypes = constrType.getParameterTypes().
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   440
                                stream().
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   441
                                map(t -> varTypeName(t, false, AnonymousTypeKind.DECLARE)).
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   442
                                collect(List.collector());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   443
                        if (node.getEnclosingExpression() != null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   444
                            TreePath enclPath = new TreePath(tp,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   445
                                                             node.getEnclosingExpression());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   446
                            desc.enclosingInstanceType = varTypeName(pathToType(enclPath),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   447
                                                                     false,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   448
                                                                     AnonymousTypeKind.DECLARE);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   449
                        }
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   450
                        TreePath currentPath = at.trees()
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   451
                                                 .getPath(tp.getCompilationUnit(),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   452
                                                          node);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   453
                        Type nodeType = pathToType(currentPath, node);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   454
                        desc.superTypeName = varTypeName(nodeType,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   455
                                                         false,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   456
                                                         AnonymousTypeKind.SUPER);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   457
                        desc.declareTypeName = varTypeName(nodeType,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   458
                                                           true, AnonymousTypeKind.DECLARE);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   459
                        desc.capturedVariables =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   460
                                captured.stream()
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   461
                                        .map(ve -> new VariableDesc(varTypeName((Type) ve.asType(),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   462
                                                                                false,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   463
                                                                                AnonymousTypeKind.DECLARE),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   464
                                                                    ve.getSimpleName().toString()))
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   465
                                        .collect(List.collector());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   466
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   467
                        desc.isClass = at.task.getTypes().directSupertypes(nodeType).size() == 1;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   468
                        ei.anonymousClasses = ei.anonymousClasses.prepend(desc);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   469
                    }
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   470
                    ei.anonymousClasses = ei.anonymousClasses.reverse();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   471
                }
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   472
                return ei;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   473
            }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   474
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   475
        return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   476
    }
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   477
    //where:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   478
        private static Set<VariableElement> capturedVariables(AnalyzeTask at,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   479
                                                              CompilationUnitTree topLevel,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   480
                                                              Tree tree) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   481
            Set<VariableElement> capturedVars = new HashSet<>();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   482
            new TreeScanner<Void, Void>() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   483
                Set<VariableElement> declaredLocalVars = new HashSet<>();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   484
                @Override
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   485
                public Void visitVariable(VariableTree node, Void p) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   486
                    TreePath currentPath = at.trees()
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   487
                                             .getPath(topLevel, node);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   488
                    declaredLocalVars.add((VariableElement) at.trees().getElement(currentPath));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   489
                    return super.visitVariable(node, p);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   490
                }
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   491
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   492
                @Override
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   493
                public Void visitIdentifier(IdentifierTree node, Void p) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   494
                    TreePath currentPath = at.trees()
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   495
                                             .getPath(topLevel, node);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   496
                    Element el = at.trees().getElement(currentPath);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   497
                    if (el != null &&
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   498
                        LOCAL_VARIABLES.contains(el.getKind()) &&
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   499
                        !declaredLocalVars.contains(el)) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   500
                        capturedVars.add((VariableElement) el);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   501
                    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   502
                    return super.visitIdentifier(node, p);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   503
                }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   504
            }.scan(tree, null);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   505
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   506
            return capturedVars;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   507
        }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   508
        private static final Set<ElementKind> LOCAL_VARIABLES =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   509
                EnumSet.of(ElementKind.EXCEPTION_PARAMETER, ElementKind.LOCAL_VARIABLE,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   510
                           ElementKind.PARAMETER, ElementKind.RESOURCE_VARIABLE);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   511
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   512
    private String varTypeName(Type type, boolean printIntersectionTypes, AnonymousTypeKind anonymousTypesKind) {
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   513
        try {
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   514
            Function<TypeSymbol, String> anonymousClass2DeclareName =
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   515
                    cs -> anon2Name.computeIfAbsent(cs, state.eval::computeDeclareName);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   516
            TypePrinter tp = new TypePrinter(at.messages(),
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   517
                    state.maps::fullClassNameAndPackageToClass, anonymousClass2DeclareName,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47975
diff changeset
   518
                    printIntersectionTypes, anonymousTypesKind);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   519
            List<Type> captures = types.captures(type);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   520
            String res = tp.toString(types.upward(type, captures));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   521
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   522
            if (res == null)
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   523
                res = OBJECT_TYPE_NAME;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   524
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   525
            return res;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   526
        } catch (Exception ex) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   527
            return OBJECT_TYPE_NAME;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   528
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   529
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   530
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   531
}