langtools/src/jdk.jshell/share/classes/jdk/jshell/Corraller.java
author rfield
Fri, 01 Apr 2016 09:20:33 -0700
changeset 36780 6bf2bef08a91
parent 36499 9d823cc0fe98
child 38535 4a25025e0b0d
permissions -rw-r--r--
8152925: JShell: enable corralling of any type declaration, including enum Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
     1
/*
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
     4
 *
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    10
 *
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    15
 * accompanied this code).
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    16
 *
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    20
 *
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    23
 * questions.
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    24
 */
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    25
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    26
package jdk.jshell;
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    27
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    28
import java.io.IOException;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    29
import java.io.StringWriter;
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    30
import com.sun.source.tree.ClassTree;
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    31
import com.sun.source.tree.MethodTree;
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    32
import com.sun.source.tree.Tree;
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    33
import com.sun.tools.javac.code.Flags;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    34
import com.sun.tools.javac.tree.JCTree;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    35
import com.sun.tools.javac.tree.JCTree.JCBlock;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    36
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    37
import com.sun.tools.javac.tree.JCTree.JCExpression;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    38
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    39
import com.sun.tools.javac.tree.JCTree.JCNewClass;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    40
import com.sun.tools.javac.tree.JCTree.JCStatement;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    41
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    42
import com.sun.tools.javac.tree.Pretty;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    43
import com.sun.tools.javac.tree.TreeMaker;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    44
import com.sun.tools.javac.util.Context;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    45
import com.sun.tools.javac.util.List;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    46
import com.sun.tools.javac.util.ListBuffer;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    47
import com.sun.tools.javac.util.Names;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    48
import static com.sun.tools.javac.code.Flags.STATIC;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    49
import static com.sun.tools.javac.code.Flags.INTERFACE;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    50
import static com.sun.tools.javac.code.Flags.ENUM;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    51
import static com.sun.tools.javac.code.Flags.PUBLIC;
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    52
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    53
/**
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    54
 * Produce a corralled version of the Wrap for a snippet.
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    55
 * Incoming tree is mutated.
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    56
 *
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    57
 * @author Robert Field
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    58
 */
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    59
class Corraller extends Pretty {
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    60
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    61
    private final StringWriter out;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    62
    private final int keyIndex;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    63
    private final TreeMaker make;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    64
    private final Names names;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    65
    private JCBlock resolutionExceptionBlock;
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    66
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    67
    public Corraller(int keyIndex, Context context) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    68
        this(new StringWriter(), keyIndex, context);
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    69
    }
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    70
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    71
    private Corraller(StringWriter out, int keyIndex, Context context) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    72
        super(out, false);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    73
        this.out = out;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    74
        this.keyIndex = keyIndex;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    75
        this.make = TreeMaker.instance(context);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    76
        this.names = Names.instance(context);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    77
    }
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    78
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    79
    public Wrap corralType(ClassTree ct) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    80
        ((JCClassDecl) ct).mods.flags |= Flags.STATIC | Flags.PUBLIC;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    81
        return corral(ct);
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    82
    }
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    83
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    84
    public Wrap corralMethod(MethodTree mt) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    85
        ((JCMethodDecl) mt).mods.flags |= Flags.STATIC | Flags.PUBLIC;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    86
        return corral(mt);
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    87
    }
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    88
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    89
    private Wrap corral(Tree tree) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    90
        try {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    91
            printStat((JCTree) tree);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    92
        } catch (IOException e) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    93
            throw new AssertionError(e);
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    94
        }
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    95
        return Wrap.simpleWrap(out.toString());
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    96
    }
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
    97
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    98
    @Override
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
    99
    public void visitBlock(JCBlock tree) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   100
        // Top-level executable blocks (usually method bodies) are corralled
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   101
        super.visitBlock((tree.flags & STATIC) != 0
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   102
                ? tree
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   103
                : resolutionExceptionBlock());
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   104
    }
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   105
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   106
    @Override
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   107
    public void visitVarDef(JCVariableDecl tree) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   108
        // No field inits in corralled classes
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   109
        tree.init = null;
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   110
        super.visitVarDef(tree);
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
   111
    }
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
   112
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   113
    @Override
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   114
    public void visitClassDef(JCClassDecl tree) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   115
        if ((tree.mods.flags & (INTERFACE | ENUM)) == 0 &&
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   116
                !tree.getMembers().stream()
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   117
                .anyMatch(t -> t.getKind() == Tree.Kind.METHOD &&
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   118
                ((MethodTree) t).getName() == tree.name.table.names.init)) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   119
            // Generate a default constructor, since
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   120
            // this is a regular class and there are no constructors
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   121
            ListBuffer<JCTree> ndefs = new ListBuffer<>();
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   122
            ndefs.addAll(tree.defs);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   123
            ndefs.add(make.MethodDef(make.Modifiers(PUBLIC),
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   124
                    tree.name.table.names.init,
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   125
                    null, List.nil(), List.nil(), List.nil(),
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   126
                    resolutionExceptionBlock(), null));
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   127
            tree.defs = ndefs.toList();
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
   128
        }
36780
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   129
        super.visitClassDef(tree);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   130
    }
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   131
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   132
    private JCBlock resolutionExceptionBlock() {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   133
        if (resolutionExceptionBlock == null) {
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   134
            JCExpression expClass
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   135
                    = make.Select(make.Select(make.Select(make.Select(
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   136
                            make.Ident(names.fromString("jdk")),
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   137
                            names.fromString("internal")),
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   138
                            names.fromString("jshell")),
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   139
                            names.fromString("remote")),
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   140
                            names.fromString("RemoteResolutionException")
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   141
                    );
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   142
            JCNewClass exp = make.NewClass(null,
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   143
                    null, expClass, List.of(make.Literal(keyIndex)), null);
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   144
            resolutionExceptionBlock = make.Block(0L, List.<JCStatement>of(
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   145
                    make.Throw(exp)));
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   146
        }
6bf2bef08a91 8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents: 36499
diff changeset
   147
        return resolutionExceptionBlock;
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
   148
    }
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents:
diff changeset
   149
}