src/jdk.jshell/share/classes/jdk/jshell/ExpressionToTypeInfo.java
author rfield
Fri, 24 Nov 2017 16:55:18 -0800
changeset 47975 5e86806f57f9
parent 47350 d65c3b21081c
child 48610 a587f95313f1
permissions -rw-r--r--
8190939: JShell: gives a compiler error evaluating an expression of inaccessible type Reviewed-by: jlahoda
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
/*
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
     2
 * Copyright (c) 2016, 2017, 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
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    28
import com.sun.source.tree.ReturnTree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    29
import com.sun.source.tree.ClassTree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    30
import com.sun.source.tree.CompilationUnitTree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    31
import com.sun.source.tree.ConditionalExpressionTree;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    32
import com.sun.source.tree.ExpressionStatementTree;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    33
import com.sun.source.tree.ExpressionTree;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    34
import com.sun.source.tree.MethodInvocationTree;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    35
import com.sun.source.tree.MethodTree;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    36
import com.sun.source.tree.NewClassTree;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    37
import com.sun.source.tree.Tree;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    38
import com.sun.source.tree.Tree.Kind;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    39
import com.sun.source.tree.VariableTree;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    40
import com.sun.source.util.TreePath;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    41
import com.sun.source.util.TreePathScanner;
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
    42
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
    43
import com.sun.tools.javac.code.Symbol;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    44
import com.sun.tools.javac.code.Symtab;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    45
import com.sun.tools.javac.code.Type;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    46
import com.sun.tools.javac.code.Types;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    47
import com.sun.tools.javac.util.List;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    48
import jdk.jshell.TaskFactory.AnalyzeTask;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    49
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    50
/**
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    51
 * Compute information about an expression string, particularly its type name.
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    52
 */
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    53
class ExpressionToTypeInfo {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    54
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    55
    private static final String OBJECT_TYPE_NAME = "Object";
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    56
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    57
    final AnalyzeTask at;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    58
    final CompilationUnitTree cu;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    59
    final JShell state;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    60
    final Symtab syms;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    61
    final Types types;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    62
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    63
    private ExpressionToTypeInfo(AnalyzeTask at, CompilationUnitTree cu, JShell state) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    64
        this.at = at;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    65
        this.cu = cu;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    66
        this.state = state;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    67
        this.syms = Symtab.instance(at.context);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    68
        this.types = Types.instance(at.context);
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
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    71
    public static class ExpressionInfo {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    72
        ExpressionTree tree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    73
        String typeName;
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
    74
        String accessibleTypeName;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    75
        String fullTypeName;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    76
        List<String> parameterTypes;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    77
        String enclosingInstanceType;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    78
        boolean isClass;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    79
        boolean isNonVoid;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    80
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    81
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    82
    // return mechanism and other general structure from TreePath.getPath()
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    83
    private static class Result extends Error {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    84
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    85
        static final long serialVersionUID = -5942088234594905629L;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    86
        final TreePath expressionPath;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    87
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    88
        Result(TreePath path) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    89
            this.expressionPath = path;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    90
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    91
    }
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
    private static class PathFinder extends TreePathScanner<TreePath, Boolean> {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    94
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    95
        // Optimize out imports etc
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    96
        @Override
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    97
        public TreePath visitCompilationUnit(CompilationUnitTree node, Boolean isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    98
            return scan(node.getTypeDecls(), isTargetContext);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
    99
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   100
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   101
        // Only care about members
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   102
        @Override
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   103
        public TreePath visitClass(ClassTree node, Boolean isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   104
            return scan(node.getMembers(), isTargetContext);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   105
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   106
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   107
        // Only want the doit method where the code is
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   108
        @Override
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   109
        public TreePath visitMethod(MethodTree node, Boolean isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   110
            if (Util.isDoIt(node.getName())) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   111
                return scan(node.getBody(), true);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   112
            } else {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   113
                return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   114
            }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   115
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   116
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   117
        @Override
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   118
        public TreePath visitReturn(ReturnTree node, Boolean isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   119
            ExpressionTree tree = node.getExpression();
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   120
            TreePath tp = new TreePath(getCurrentPath(), tree);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   121
            if (isTargetContext) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   122
                throw new Result(tp);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   123
            } else {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   124
                return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   125
            }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   126
        }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   127
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   128
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   129
        public TreePath visitVariable(VariableTree node, Boolean isTargetContext) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   130
            if (isTargetContext) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   131
                throw new Result(getCurrentPath());
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   132
            } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   133
                return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   134
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   135
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   136
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   137
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   138
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   139
    private Type pathToType(TreePath tp) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   140
        return (Type) at.trees().getTypeMirror(tp);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   141
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   142
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   143
    private Type pathToType(TreePath tp, Tree tree) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   144
        if (tree instanceof ConditionalExpressionTree) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   145
            // Conditionals always wind up as Object -- this corrects
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   146
            ConditionalExpressionTree cet = (ConditionalExpressionTree) tree;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   147
            Type tmt = pathToType(new TreePath(tp, cet.getTrueExpression()));
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   148
            Type tmf = pathToType(new TreePath(tp, cet.getFalseExpression()));
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   149
            if (!tmt.isPrimitive() && !tmf.isPrimitive()) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   150
                Type lub = types.lub(tmt, tmf);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   151
                // System.err.printf("cond ? %s : %s  --  lub = %s\n",
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   152
                //             varTypeName(tmt), varTypeName(tmf), varTypeName(lub));
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   153
                return lub;
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
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   156
        return pathToType(tp);
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
     * Entry method: get expression info
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   161
     * @param code the expression as a string
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   162
     * @param state a JShell instance
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   163
     * @return type information
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   164
     */
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   165
    public static ExpressionInfo expressionInfo(String code, JShell state) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   166
        if (code == null || code.isEmpty()) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   167
            return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   168
        }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   169
        OuterWrap codeWrap = state.outerMap.wrapInTrialClass(Wrap.methodReturnWrap(code));
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   170
        try {
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   171
            return state.taskFactory.analyze(codeWrap, at -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   172
                CompilationUnitTree cu = at.firstCuTree();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   173
                if (at.hasErrors() || cu == null) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   174
                    return null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   175
                }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   176
                return new ExpressionToTypeInfo(at, cu, state).typeOfExpression();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   177
            });
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   178
        } catch (Exception ex) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   179
            return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   180
        }
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
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   183
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   184
     * 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
   185
     * has been inferred automatically from the given initializer.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   186
     * @param code the initializer as a string
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   187
     * @param state a JShell instance
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   188
     * @return type information
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   189
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   190
    public static ExpressionInfo localVariableTypeForInitializer(String code, JShell state) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   191
        if (code == null || code.isEmpty()) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   192
            return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   193
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   194
        try {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   195
            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
   196
            return state.taskFactory.analyze(codeWrap, at -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   197
                CompilationUnitTree cu = at.firstCuTree();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   198
                if (at.hasErrors() || cu == null) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   199
                    return null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   200
                }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   201
                return new ExpressionToTypeInfo(at, cu, state).typeOfExpression();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   202
            });
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   203
        } catch (Exception ex) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   204
            return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   205
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   206
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   207
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   208
    private ExpressionInfo typeOfExpression() {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   209
        return treeToInfo(findExpressionPath());
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   210
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   211
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   212
    private TreePath findExpressionPath() {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   213
        try {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   214
            new PathFinder().scan(new TreePath(cu), false);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   215
        } catch (Result result) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   216
            return result.expressionPath;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   217
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   218
        return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   219
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   220
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   221
    /**
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   222
     * 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
   223
     * 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
   224
     * accessible
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   225
     *
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   226
     * @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
   227
     * @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
   228
     */
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   229
    private boolean isAccessible(Type type) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   230
        Symbol.TypeSymbol tsym = type.asElement();
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   231
        return ((tsym.flags() & Flags.PUBLIC) != 0 ||
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   232
                ((tsym.flags() & Flags.PRIVATE) == 0 &&
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   233
                Util.isInJShellClass(tsym.flatName().toString()))) &&
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   234
                 type.getTypeArguments().stream()
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   235
                        .allMatch(this::isAccessible);
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   236
    }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   237
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   238
    /**
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   239
     * Return the superclass.
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   240
     *
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   241
     * @param type the type
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   242
     * @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
   243
     */
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   244
    private Type supertype(Type type) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   245
        Type sup = types.supertype(type);
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   246
        if (sup == Type.noType || sup == null) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   247
            return syms.objectType;
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   248
        }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   249
        return sup;
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   250
    }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   251
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   252
    /**
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   253
     * Find an accessible supertype.
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   254
     *
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   255
     * @param type the type
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   256
     * @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
   257
     * interface which is
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   258
     */
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   259
    private Type findAccessibleSupertype(Type type) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   260
        // 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
   261
        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
   262
            if (isAccessible(sup)) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   263
                return sup;
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   264
            }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   265
        }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   266
        // Failing superclasses, look through superclasses for accessible interfaces
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   267
        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
   268
            for (Type itf : types.interfaces(sup)) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   269
                if (isAccessible(itf)) {
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   270
                    return itf;
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   271
                }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   272
            }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   273
        }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   274
        // Punt, return Object which is the supertype of everything
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   275
        return syms.objectType;
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   276
    }
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   277
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   278
    private ExpressionInfo treeToInfo(TreePath tp) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   279
        if (tp != null) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   280
            Tree tree = tp.getLeaf();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   281
            boolean isExpression = tree instanceof ExpressionTree;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   282
            if (isExpression || tree.getKind() == Kind.VARIABLE) {
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   283
                ExpressionInfo ei = new ExpressionInfo();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   284
                if (isExpression)
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   285
                    ei.tree = (ExpressionTree) tree;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   286
                Type type = pathToType(tp, tree);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   287
                if (type != null) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   288
                    switch (type.getKind()) {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   289
                        case VOID:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   290
                        case NONE:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   291
                        case ERROR:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   292
                        case OTHER:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   293
                            break;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   294
                        case NULL:
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   295
                            ei.isNonVoid = true;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   296
                            ei.typeName = OBJECT_TYPE_NAME;
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   297
                            ei.accessibleTypeName = OBJECT_TYPE_NAME;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   298
                            break;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   299
                        default: {
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   300
                            ei.isNonVoid = true;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   301
                            ei.typeName = varTypeName(type, false);
47975
5e86806f57f9 8190939: JShell: gives a compiler error evaluating an expression of inaccessible type
rfield
parents: 47350
diff changeset
   302
                            ei.accessibleTypeName = varTypeName(findAccessibleSupertype(type), false);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   303
                            ei.fullTypeName = varTypeName(type, true);
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   304
                            break;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   305
                        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   306
                    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   307
                }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   308
                if (tree.getKind() == Tree.Kind.VARIABLE) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   309
                    Tree init = ((VariableTree) tree).getInitializer();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   310
                    if (init.getKind() == Tree.Kind.NEW_CLASS &&
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   311
                        ((NewClassTree) init).getClassBody() != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   312
                        NewClassTree nct = (NewClassTree) init;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   313
                        ClassTree clazz = nct.getClassBody();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   314
                        MethodTree constructor = (MethodTree) clazz.getMembers().get(0);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   315
                        ExpressionStatementTree superCallStatement =
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   316
                                (ExpressionStatementTree) constructor.getBody().getStatements().get(0);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   317
                        MethodInvocationTree superCall =
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   318
                                (MethodInvocationTree) superCallStatement.getExpression();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   319
                        TreePath superCallPath =
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   320
                                at.trees().getPath(tp.getCompilationUnit(), superCall.getMethodSelect());
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   321
                        Type constrType = pathToType(superCallPath);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   322
                        ei.parameterTypes = constrType.getParameterTypes()
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   323
                                                      .stream()
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   324
                                                      .map(t -> varTypeName(t, false))
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   325
                                                      .collect(List.collector());
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   326
                        if (nct.getEnclosingExpression() != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   327
                            TreePath enclPath = new TreePath(tp, nct.getEnclosingExpression());
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   328
                            ei.enclosingInstanceType = varTypeName(pathToType(enclPath), false);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   329
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   330
                        ei.isClass = at.task.getTypes().directSupertypes(type).size() == 1;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   331
                    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   332
                }
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   333
                return ei;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   334
            }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   335
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   336
        return null;
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   337
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   338
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   339
    private String varTypeName(Type type, boolean printIntersectionTypes) {
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   340
        try {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   341
            TypePrinter tp = new TypePrinter(at.messages(),
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   342
                    state.maps::fullClassNameAndPackageToClass, printIntersectionTypes);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   343
            List<Type> captures = types.captures(type);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   344
            String res = tp.toString(types.upward(type, captures));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   345
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   346
            if (res == null)
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   347
                res = OBJECT_TYPE_NAME;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   348
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   349
            return res;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   350
        } catch (Exception ex) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   351
            return OBJECT_TYPE_NAME;
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   352
        }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   353
    }
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   354
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
diff changeset
   355
}