src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java
author rfield
Fri, 16 Feb 2018 16:18:55 -0800
changeset 49092 6dc5e0cdb44c
parent 48610 a587f95313f1
child 49518 d0ff431a596e
permissions -rw-r--r--
8196133: JShell crashes when attempting to use bad source file in class path Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
     2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    26
package jdk.jshell;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    27
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    28
import com.sun.source.tree.CompilationUnitTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    29
import com.sun.source.tree.Tree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
import com.sun.source.util.Trees;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
import com.sun.tools.javac.api.JavacTaskImpl;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
import com.sun.tools.javac.util.Context;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
import java.util.ArrayList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    34
import java.util.List;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
import javax.tools.Diagnostic;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import javax.tools.DiagnosticCollector;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
import javax.tools.JavaCompiler;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import javax.tools.JavaFileManager;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import javax.tools.JavaFileObject;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import javax.tools.ToolProvider;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import static jdk.jshell.Util.*;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import com.sun.source.tree.ImportTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import com.sun.tools.javac.code.Types;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import com.sun.tools.javac.util.JavacMessages;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import jdk.jshell.MemoryFileManager.OutputMemoryJavaFileObject;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import java.util.Collections;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import java.util.Locale;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import static javax.tools.StandardLocation.CLASS_OUTPUT;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import java.io.File;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import java.util.Collection;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import java.util.HashMap;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import java.util.LinkedHashMap;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
import java.util.Map;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
import java.util.stream.Collectors;
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
    56
import static java.util.stream.Collectors.toList;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import java.util.stream.Stream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import javax.lang.model.util.Elements;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
import javax.tools.FileObject;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject;
38526
6f5838874afc 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37939
diff changeset
    61
import java.lang.Runtime.Version;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    62
import java.nio.CharBuffer;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    63
import java.util.function.BiFunction;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    64
import com.sun.source.tree.ClassTree;
43586
cc7a4eb79b29 8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents: 43134
diff changeset
    65
import com.sun.source.tree.Tree.Kind;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    66
import com.sun.source.util.TaskEvent;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    67
import com.sun.source.util.TaskListener;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    68
import com.sun.tools.javac.api.JavacTaskPool;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    69
import com.sun.tools.javac.code.ClassFinder;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    70
import com.sun.tools.javac.code.Kinds;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    71
import com.sun.tools.javac.code.Symbol;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    72
import com.sun.tools.javac.code.Symbol.ClassSymbol;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    73
import com.sun.tools.javac.code.Symbol.PackageSymbol;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    74
import com.sun.tools.javac.code.Symbol.TypeSymbol;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    75
import com.sun.tools.javac.code.Symbol.VarSymbol;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    76
import com.sun.tools.javac.code.Symtab;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    77
import com.sun.tools.javac.code.Type;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    78
import com.sun.tools.javac.comp.Attr;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    79
import com.sun.tools.javac.comp.AttrContext;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    80
import com.sun.tools.javac.comp.Enter;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    81
import com.sun.tools.javac.comp.Env;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    82
import com.sun.tools.javac.comp.Resolve;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    83
import com.sun.tools.javac.parser.Parser;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    84
import com.sun.tools.javac.parser.ParserFactory;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    85
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    86
import com.sun.tools.javac.tree.JCTree.JCExpression;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    87
import com.sun.tools.javac.tree.JCTree.JCTypeCast;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    88
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    89
import com.sun.tools.javac.tree.JCTree.Tag;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
    90
import com.sun.tools.javac.util.Context.Factory;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    91
import com.sun.tools.javac.util.Log;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    92
import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
    93
import com.sun.tools.javac.util.Names;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
    94
import jdk.jshell.Snippet.Status;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    96
/**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    97
 * The primary interface to the compiler API.  Parsing, analysis, and
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    98
 * compilation to class files (in memory).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    99
 * @author Robert Field
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   100
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
class TaskFactory {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
    private final JavaCompiler compiler;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   104
    private final MemoryFileManager fileManager;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
    private final JShell state;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
    private String classpath = System.getProperty("java.class.path");
36497
9c4840131512 8150632: jdk.jshell.TaskFactory should use jdk.Version to check for java.specification.version
simonis
parents: 35000
diff changeset
   107
    private final static Version INITIAL_SUPPORTED_VER = Version.parse("9");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   108
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   109
    TaskFactory(JShell state) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   110
        this.state = state;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   111
        this.compiler = ToolProvider.getSystemJavaCompiler();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   112
        if (compiler == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
            throw new UnsupportedOperationException("Compiler not available, must be run with full JDK 9.");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   114
        }
36497
9c4840131512 8150632: jdk.jshell.TaskFactory should use jdk.Version to check for java.specification.version
simonis
parents: 35000
diff changeset
   115
        Version current = Version.parse(System.getProperty("java.specification.version"));
39809
74b7aea686d8 8161236: Runtime.Version.{compareTo, equals}IgnoreOpt should be renamed
iris
parents: 39807
diff changeset
   116
        if (INITIAL_SUPPORTED_VER.compareToIgnoreOptional(current) > 0)  {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
            throw new UnsupportedOperationException("Wrong compiler, must be run with full JDK 9.");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   118
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   119
        this.fileManager = new MemoryFileManager(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   120
                compiler.getStandardFileManager(null, null, null), state);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   121
        initTaskPool();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   122
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   123
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   124
    void addToClasspath(String path) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
        classpath = classpath + File.pathSeparator + path;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   126
        List<String> args = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   127
        args.add(classpath);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   128
        fileManager().handleOption("-classpath", args.iterator());
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   129
        initTaskPool();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   130
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   132
    MemoryFileManager fileManager() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
        return fileManager;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   134
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   135
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   136
    public <Z> Z parse(String source,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   137
                       boolean forceExpression,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   138
                       Worker<ParseTask, Z> worker) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   139
        StringSourceHandler sh = new StringSourceHandler();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   140
        return runTask(Stream.of(source),
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   141
                       sh,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   142
                       List.of("-XDallowStringFolding=false", "-proc:none",
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   143
                               "-XDneedsReplParserFactory=" + forceExpression),
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   144
                       (jti, diagnostics) -> new ParseTask(sh, jti, diagnostics, forceExpression),
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   145
                       worker);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   146
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   147
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   148
    public <Z> Z analyze(OuterWrap wrap,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   149
                         Worker<AnalyzeTask, Z> worker) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   150
        return analyze(Collections.singletonList(wrap), worker);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   151
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   152
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   153
    public <Z> Z analyze(OuterWrap wrap,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   154
                         List<String> extraArgs,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   155
                         Worker<AnalyzeTask, Z> worker) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   156
        return analyze(Collections.singletonList(wrap), extraArgs, worker);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   157
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   158
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   159
    public <Z> Z analyze(Collection<OuterWrap> wraps,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   160
                         Worker<AnalyzeTask, Z> worker) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   161
        return analyze(wraps, Collections.emptyList(), worker);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   162
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   163
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   164
    public <Z> Z analyze(Collection<OuterWrap> wraps,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   165
                         List<String> extraArgs,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   166
                         Worker<AnalyzeTask, Z> worker) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   167
        WrapSourceHandler sh = new WrapSourceHandler();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   168
        List<String> allOptions = new ArrayList<>();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   169
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   170
        allOptions.add("--should-stop:at=FLOW");
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   171
        allOptions.add("-Xlint:unchecked");
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   172
        allOptions.add("-proc:none");
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   173
        allOptions.addAll(extraArgs);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   174
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   175
        return runTask(wraps.stream(),
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   176
                       sh,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   177
                       allOptions,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   178
                       (jti, diagnostics) -> new AnalyzeTask(sh, jti, diagnostics),
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   179
                       worker);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   180
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   181
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   182
    public <Z> Z compile(Collection<OuterWrap> wraps,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   183
                         Worker<CompileTask, Z> worker) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   184
        WrapSourceHandler sh = new WrapSourceHandler();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   185
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   186
        return runTask(wraps.stream(),
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   187
                       sh,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   188
                       List.of("-Xlint:unchecked", "-proc:none", "-parameters"),
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   189
                       (jti, diagnostics) -> new CompileTask(sh, jti, diagnostics),
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   190
                       worker);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   191
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   192
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   193
    private <S, T extends BaseTask, Z> Z runTask(Stream<S> inputs,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   194
                                                 SourceHandler<S> sh,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   195
                                                 List<String> options,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   196
                                                 BiFunction<JavacTaskImpl, DiagnosticCollector<JavaFileObject>, T> creator,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   197
                                                 Worker<T, Z> worker) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   198
            List<String> allOptions = new ArrayList<>(options.size() + state.extraCompilerOptions.size());
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   199
            allOptions.addAll(options);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   200
            allOptions.addAll(state.extraCompilerOptions);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   201
            Iterable<? extends JavaFileObject> compilationUnits = inputs
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   202
                            .map(in -> sh.sourceToFileObject(fileManager, in))
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   203
                            .collect(Collectors.toList());
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   204
            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   205
            return javacTaskPool.getTask(null, fileManager, diagnostics, allOptions, null,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   206
                                         compilationUnits, task -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   207
                 JavacTaskImpl jti = (JavacTaskImpl) task;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   208
                 Context context = jti.getContext();
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   209
                 DisableAccessibilityResolve.preRegister(context);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   210
                 jti.addTaskListener(new TaskListenerImpl(context, state));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   211
                 try {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   212
                     return worker.withTask(creator.apply(jti, diagnostics));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   213
                 } finally {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   214
                     //additional cleanup: purge the REPL package:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   215
                     Symtab syms = Symtab.instance(context);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   216
                     Names names = Names.instance(context);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   217
                     PackageSymbol repl = syms.getPackage(syms.unnamedModule, names.fromString(Util.REPL_PACKAGE));
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   218
                     if (repl != null) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   219
                         for (ClassSymbol clazz : syms.getAllClasses()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   220
                             if (clazz.packge() == repl) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   221
                                 syms.removeClass(syms.unnamedModule, clazz.flatName());
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   222
                             }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   223
                         }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   224
                         repl.members_field = null;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   225
                         repl.completer = ClassFinder.instance(context).getCompleter();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   226
                     }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   227
                 }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   228
            });
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   229
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   230
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   231
    interface Worker<T extends BaseTask, Z> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   232
        public Z withTask(T task);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   233
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   234
43586
cc7a4eb79b29 8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents: 43134
diff changeset
   235
    // Parse a snippet and return our parse task handler
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   236
    <Z> Z parse(final String source, Worker<ParseTask, Z> worker) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   237
        return parse(source, false, pt -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   238
            if (!pt.units().isEmpty()
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   239
                    && pt.units().get(0).getKind() == Kind.EXPRESSION_STATEMENT
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   240
                    && pt.getDiagnostics().hasOtherThanNotStatementErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   241
                // It failed, it may be an expression being incorrectly
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   242
                // parsed as having a leading type variable, example:   a < b
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   243
                // Try forcing interpretation as an expression
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   244
                return parse(source, true, ept -> {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   245
                    if (!ept.getDiagnostics().hasOtherThanNotStatementErrors()) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   246
                        return worker.withTask(ept);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   247
                    } else {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   248
                        return worker.withTask(pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   249
                    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   250
                });
43586
cc7a4eb79b29 8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents: 43134
diff changeset
   251
            }
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   252
            return worker.withTask(pt);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   253
        });
43586
cc7a4eb79b29 8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents: 43134
diff changeset
   254
    }
cc7a4eb79b29 8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents: 43134
diff changeset
   255
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
    private interface SourceHandler<T> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   258
        JavaFileObject sourceToFileObject(MemoryFileManager fm, T t);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   259
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   260
        Diag diag(Diagnostic<? extends JavaFileObject> d);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   262
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
    private class StringSourceHandler implements SourceHandler<String> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
        public JavaFileObject sourceToFileObject(MemoryFileManager fm, String src) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
            return fm.createSourceFileObject(src, "$NeverUsedName$", src);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   268
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   269
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   270
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   271
        public Diag diag(final Diagnostic<? extends JavaFileObject> d) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
            return new Diag() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   273
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   274
                @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   275
                public boolean isError() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   276
                    return d.getKind() == Diagnostic.Kind.ERROR;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   277
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   278
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   279
                @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   280
                public long getPosition() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   281
                    return d.getPosition();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   282
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   283
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   284
                @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   285
                public long getStartPosition() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   286
                    return d.getStartPosition();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   287
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   288
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   289
                @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   290
                public long getEndPosition() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   291
                    return d.getEndPosition();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   292
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   293
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   294
                @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   295
                public String getCode() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   296
                    return d.getCode();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   297
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   298
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   299
                @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   300
                public String getMessage(Locale locale) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   301
                    return expunge(d.getMessage(locale));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   302
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   303
            };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   304
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   305
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   306
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   307
    private class WrapSourceHandler implements SourceHandler<OuterWrap> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   308
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   309
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   310
        public JavaFileObject sourceToFileObject(MemoryFileManager fm, OuterWrap w) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   311
            return fm.createSourceFileObject(w, w.classFullName(), w.wrapped());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   312
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   313
49092
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   314
        /**
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   315
         * Get the source information from the wrap.  If this is external, or
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   316
         * otherwise does not have wrap info, just use source code.
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   317
         * @param d the Diagnostic from the compiler
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   318
         * @return the corresponding Diag
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   319
         */
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   320
        @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   321
        public Diag diag(Diagnostic<? extends JavaFileObject> d) {
49092
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   322
            JavaFileObject jfo = d.getSource();
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   323
            return jfo instanceof SourceMemoryJavaFileObject
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   324
                    ? ((OuterWrap) ((SourceMemoryJavaFileObject) jfo).getOrigin()).wrapDiag(d)
6dc5e0cdb44c 8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents: 48610
diff changeset
   325
                    : new StringSourceHandler().diag(d);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   326
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   327
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   328
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   329
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   330
     * Parse a snippet of code (as a String) using the parser subclass.  Return
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   331
     * the parse tree (and errors).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   332
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   333
    class ParseTask extends BaseTask {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   334
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   335
        private final Iterable<? extends CompilationUnitTree> cuts;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   336
        private final List<? extends Tree> units;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   337
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   338
        private ParseTask(SourceHandler<String> sh,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   339
                          JavacTaskImpl task,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   340
                          DiagnosticCollector<JavaFileObject> diagnostics,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   341
                          boolean forceExpression) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   342
            super(sh, task, diagnostics);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   343
            ReplParserFactory.preRegister(context, forceExpression);
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   344
            cuts = parse();
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   345
            units = Util.stream(cuts)
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   346
                    .flatMap(cut -> {
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   347
                        List<? extends ImportTree> imps = cut.getImports();
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   348
                        return (!imps.isEmpty() ? imps : cut.getTypeDecls()).stream();
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   349
                    })
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   350
                    .collect(toList());
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   351
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   352
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   353
        private Iterable<? extends CompilationUnitTree> parse() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   354
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   355
                return task.parse();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   356
            } catch (Exception ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   357
                throw new InternalError("Exception during parse - " + ex.getMessage(), ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   358
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   359
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   360
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   361
        List<? extends Tree> units() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   362
            return units;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   363
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   364
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   365
        @Override
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   366
        Iterable<? extends CompilationUnitTree> cuTrees() {
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   367
            return cuts;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   368
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   369
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   370
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   371
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   372
     * Run the normal "analyze()" pass of the compiler over the wrapped snippet.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   373
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   374
    class AnalyzeTask extends BaseTask {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   375
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   376
        private final Iterable<? extends CompilationUnitTree> cuts;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   377
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   378
        private AnalyzeTask(SourceHandler<OuterWrap> sh,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   379
                            JavacTaskImpl task,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   380
                            DiagnosticCollector<JavaFileObject> diagnostics) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   381
            super(sh, task, diagnostics);
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   382
            cuts = analyze();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   383
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   384
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   385
        private Iterable<? extends CompilationUnitTree> analyze() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   386
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   387
                Iterable<? extends CompilationUnitTree> cuts = task.parse();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   388
                task.analyze();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   389
                return cuts;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   390
            } catch (Exception ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   391
                throw new InternalError("Exception during analyze - " + ex.getMessage(), ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   392
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   393
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   394
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   395
        @Override
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   396
        Iterable<? extends CompilationUnitTree> cuTrees() {
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   397
            return cuts;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   398
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   399
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   400
        Elements getElements() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   401
            return task.getElements();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   402
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   403
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   404
        javax.lang.model.util.Types getTypes() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   405
            return task.getTypes();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   406
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   407
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   408
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   409
    /**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   410
     * Unit the wrapped snippet to class files.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   411
     */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   412
    class CompileTask extends BaseTask {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   413
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36526
diff changeset
   414
        private final Map<OuterWrap, List<OutputMemoryJavaFileObject>> classObjs = new HashMap<>();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   415
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   416
        CompileTask(SourceHandler<OuterWrap>sh,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   417
                    JavacTaskImpl jti,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   418
                    DiagnosticCollector<JavaFileObject> diagnostics) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   419
            super(sh, jti, diagnostics);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   420
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   421
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   422
        boolean compile() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   423
            fileManager.registerClassFileCreationListener(this::listenForNewClassFile);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   424
            boolean result = task.call();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   425
            fileManager.registerClassFileCreationListener(null);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   426
            return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   427
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   428
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   429
        // Returns the list of classes generated during this compile.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   430
        // Stores the mapping between class name and current compiled bytes.
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   431
        List<String> classList(OuterWrap w) {
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36526
diff changeset
   432
            List<OutputMemoryJavaFileObject> l = classObjs.get(w);
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   433
            if (l == null) {
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   434
                return Collections.emptyList();
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   435
            }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   436
            List<String> list = new ArrayList<>();
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   437
            for (OutputMemoryJavaFileObject fo : l) {
39807
ba0ff343d241 8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents: 39601
diff changeset
   438
                state.classTracker.setCurrentBytes(fo.getName(), fo.getBytes());
38535
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   439
                list.add(fo.getName());
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   440
            }
4a25025e0b0d 8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents: 38526
diff changeset
   441
            return list;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   442
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   443
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   444
        private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   445
                String className, JavaFileObject.Kind kind, FileObject sibling) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   446
            //debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   447
            if (location == CLASS_OUTPUT) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   448
                state.debug(DBG_GEN, "Compiler generating class %s\n", className);
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36526
diff changeset
   449
                OuterWrap w = ((sibling instanceof SourceMemoryJavaFileObject)
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36526
diff changeset
   450
                        && (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof OuterWrap))
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36526
diff changeset
   451
                        ? (OuterWrap) ((SourceMemoryJavaFileObject) sibling).getOrigin()
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   452
                        : null;
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36526
diff changeset
   453
                classObjs.compute(w, (k, v) -> (v == null)? new ArrayList<>() : v)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   454
                        .add(jfo);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   455
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   456
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   457
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   458
        @Override
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   459
        Iterable<? extends CompilationUnitTree> cuTrees() {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   460
            throw new UnsupportedOperationException("Not supported.");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   461
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   462
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   463
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   464
    private JavacTaskPool javacTaskPool;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   465
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   466
    private void initTaskPool() {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   467
        javacTaskPool = new JavacTaskPool(5);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   468
    }
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   469
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   470
    abstract class BaseTask {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   471
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   472
        final DiagnosticCollector<JavaFileObject> diagnostics;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   473
        final JavacTaskImpl task;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   474
        private DiagList diags = null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   475
        private final SourceHandler<?> sourceHandler;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   476
        final Context context;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   477
        private Types types;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   478
        private JavacMessages messages;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   479
        private Trees trees;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   480
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   481
        private <T>BaseTask(SourceHandler<T> sh,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   482
                            JavacTaskImpl task,
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   483
                            DiagnosticCollector<JavaFileObject> diagnostics) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   484
            this.sourceHandler = sh;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   485
            this.task = task;
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   486
            context = task.getContext();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   487
            this.diagnostics = diagnostics;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   488
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   489
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   490
        abstract Iterable<? extends CompilationUnitTree> cuTrees();
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   491
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   492
        CompilationUnitTree firstCuTree() {
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   493
            return cuTrees().iterator().next();
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 34092
diff changeset
   494
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   495
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   496
        Diag diag(Diagnostic<? extends JavaFileObject> diag) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   497
            return sourceHandler.diag(diag);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   498
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   499
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   500
        Context getContext() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   501
            return context;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   502
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   503
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   504
        Types types() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   505
            if (types == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   506
                types = Types.instance(context);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   507
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   508
            return types;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   509
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   510
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   511
        JavacMessages messages() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   512
            if (messages == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   513
                messages = JavacMessages.instance(context);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   514
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   515
            return messages;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   516
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   517
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   518
        Trees trees() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   519
            if (trees == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   520
                trees = Trees.instance(task);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   521
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   522
            return trees;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   523
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   524
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   525
        // ------------------ diags functionality
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   526
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   527
        DiagList getDiagnostics() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   528
            if (diags == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   529
                LinkedHashMap<String, Diag> diagMap = new LinkedHashMap<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   530
                for (Diagnostic<? extends JavaFileObject> in : diagnostics.getDiagnostics()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   531
                    Diag d = diag(in);
35000
952a7b4652f0 8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents: 34857
diff changeset
   532
                    String uniqueKey = d.getCode() + ":" + d.getPosition() + ":" + d.getMessage(PARSED_LOCALE);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   533
                    diagMap.put(uniqueKey, d);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   534
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   535
                diags = new DiagList(diagMap.values());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   536
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   537
            return diags;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   538
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   539
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   540
        boolean hasErrors() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   541
            return getDiagnostics().hasErrors();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   542
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   543
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   544
        String shortErrorMessage() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   545
            StringBuilder sb = new StringBuilder();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   546
            for (Diag diag : getDiagnostics()) {
35000
952a7b4652f0 8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents: 34857
diff changeset
   547
                for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   548
                    if (!line.trim().startsWith("location:")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   549
                        sb.append(line);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   550
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   551
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   552
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   553
            return sb.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   554
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   555
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   556
        void debugPrintDiagnostics(String src) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   557
            for (Diag diag : getDiagnostics()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   558
                state.debug(DBG_GEN, "ERROR --\n");
35000
952a7b4652f0 8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents: 34857
diff changeset
   559
                for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   560
                    if (!line.trim().startsWith("location:")) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   561
                        state.debug(DBG_GEN, "%s\n", line);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   562
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   563
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   564
                int start = (int) diag.getStartPosition();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   565
                int end = (int) diag.getEndPosition();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   566
                if (src != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   567
                    String[] srcLines = src.split("\\r?\\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   568
                    for (String line : srcLines) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   569
                        state.debug(DBG_GEN, "%s\n", line);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   570
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   571
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   572
                    StringBuilder sb = new StringBuilder();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   573
                    for (int i = 0; i < start; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   574
                        sb.append(' ');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   575
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   576
                    sb.append('^');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   577
                    if (end > start) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   578
                        for (int i = start + 1; i < end; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   579
                            sb.append('-');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   580
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   581
                        sb.append('^');
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   582
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   583
                    state.debug(DBG_GEN, "%s\n", sb.toString());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   584
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   585
                state.debug(DBG_GEN, "printDiagnostics start-pos = %d ==> %d -- wrap = %s\n",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   586
                        diag.getStartPosition(), start, this);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   587
                state.debug(DBG_GEN, "Code: %s\n", diag.getCode());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   588
                state.debug(DBG_GEN, "Pos: %d (%d - %d) -- %s\n", diag.getPosition(),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   589
                        diag.getStartPosition(), diag.getEndPosition(), diag.getMessage(null));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   590
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   591
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   592
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   593
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   594
    /**The variable types inferred for "var"s may be non-denotable.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   595
     * jshell desugars these variables into fields, and fields must have
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   596
     * a denotable type. So these fields are declared with some simpler denotable
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   597
     * type, and the listener here enhances the types of the fields to be the full
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   598
     * inferred types. This is mainly when the inferred type contains:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   599
     * -intersection types (e.g. <Z extends Runnable&CharSequence> Z get() {...} var z = get();)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   600
     * -types that are inaccessible at the given place
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   601
     *
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   602
     * This type enhancement does not need to do anything about anonymous classes, as these
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   603
     * are desugared into member classes.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   604
     */
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   605
    private static final class TaskListenerImpl implements TaskListener {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   606
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   607
        private final Context context;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   608
        private final JShell state;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   609
        /* Keep the original (declaration) types of the fields that were enhanced.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   610
         * The declaration types need to be put back before writing the fields
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   611
         * into classfiles.*/
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   612
        private final Map<VarSymbol, Type> var2OriginalType = new HashMap<>();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   613
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   614
        public TaskListenerImpl(Context context, JShell state) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   615
            this.context = context;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   616
            this.state = state;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   617
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   618
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   619
        @Override
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   620
        public void started(TaskEvent e) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   621
            if (e.getKind() != TaskEvent.Kind.GENERATE)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   622
                return ;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   623
            //clear enhanced types in fields we are about to write to the classfiles:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   624
            for (Tree clazz : e.getCompilationUnit().getTypeDecls()) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   625
                ClassTree ct = (ClassTree) clazz;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   626
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   627
                for (Tree member : ct.getMembers()) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   628
                    if (member.getKind() != Tree.Kind.VARIABLE)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   629
                        continue;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   630
                    VarSymbol vsym = ((JCVariableDecl) member).sym;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   631
                    Type original = var2OriginalType.remove(vsym);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   632
                    if (original != null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   633
                        vsym.type = original;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   634
                    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   635
                }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   636
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   637
        }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   638
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   639
        private boolean variablesSet = false;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   640
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   641
        @Override
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   642
        public void finished(TaskEvent e) {
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   643
            if (e.getKind() != TaskEvent.Kind.ENTER || variablesSet)
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   644
                return ;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   645
            state.maps
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   646
                 .snippetList()
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   647
                 .stream()
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   648
                 .filter(s -> s.status() == Status.VALID)
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   649
                 .filter(s -> s.kind() == Snippet.Kind.VAR)
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   650
                 .filter(s -> s.subKind() == Snippet.SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND ||
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   651
                              s.subKind() == Snippet.SubKind.TEMP_VAR_EXPRESSION_SUBKIND)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   652
                 .forEach(s -> setVariableType((VarSnippet) s));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   653
            variablesSet = true;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   654
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   655
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   656
        /* If the snippet contain enhanced types, enhance the type of
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   657
         * the variable from snippet s to be the enhanced type.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   658
         */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   659
        private void setVariableType(VarSnippet s) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   660
            String typeName = s.fullTypeName;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   661
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   662
            if (typeName == null)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   663
                return ;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   664
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   665
            Symtab syms = Symtab.instance(context);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   666
            Names names = Names.instance(context);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   667
            Log log  = Log.instance(context);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   668
            ParserFactory parserFactory = ParserFactory.instance(context);
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   669
            Attr attr = Attr.instance(context);
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   670
            Enter enter = Enter.instance(context);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   671
            DisableAccessibilityResolve rs = (DisableAccessibilityResolve) Resolve.instance(context);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47268
diff changeset
   672
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   673
            //find the variable:
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   674
            ClassSymbol clazz = syms.getClass(syms.unnamedModule, names.fromString(s.classFullName()));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   675
            if (clazz == null || !clazz.isCompleted())
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   676
                return;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   677
            VarSymbol field = (VarSymbol) clazz.members().findFirst(names.fromString(s.name()), sym -> sym.kind == Kinds.Kind.VAR);
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   678
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   679
            if (field != null && !var2OriginalType.containsKey(field)) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   680
                //if it was not enhanced yet:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   681
                //ignore any errors:
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   682
                JavaFileObject prev = log.useSource(null);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   683
                DiscardDiagnosticHandler h = new DiscardDiagnosticHandler(log);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   684
                try {
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   685
                    //parse the type as a cast, i.e. "(<typeName>) x". This is to support
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   686
                    //intersection types:
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   687
                    CharBuffer buf = CharBuffer.wrap(("(" + typeName +")x\u0000").toCharArray(), 0, typeName.length() + 3);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   688
                    Parser parser = parserFactory.newParser(buf, false, false, false);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   689
                    JCExpression expr = parser.parseExpression();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   690
                    if (expr.hasTag(Tag.TYPECAST)) {
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   691
                        //if parsed OK, attribute and set the type:
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   692
                        var2OriginalType.put(field, field.type);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   693
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   694
                        JCTypeCast tree = (JCTypeCast) expr;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   695
                        rs.runWithoutAccessChecks(() -> {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   696
                            field.type = attr.attribType(tree.clazz,
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   697
                                                         enter.getEnvs().iterator().next().enclClass.sym);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   698
                        });
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   699
                    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   700
                } finally {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   701
                    log.popDiagnosticHandler(h);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   702
                    log.useSource(prev);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   703
                }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   704
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   705
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   706
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   707
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   708
    private static final class DisableAccessibilityResolve extends Resolve {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   709
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   710
        public static void preRegister(Context context) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   711
            if (context.get(Marker.class) == null) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   712
                context.put(resolveKey, ((Factory<Resolve>) c -> new DisableAccessibilityResolve(c)));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   713
                context.put(Marker.class, new Marker());
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   714
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   715
        }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   716
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   717
        private boolean noAccessChecks;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   718
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   719
        public DisableAccessibilityResolve(Context context) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   720
            super(context);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   721
        }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   722
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   723
        /**Run the given Runnable with all access checks disabled.
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   724
         *
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   725
         * @param r Runnnable to run
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   726
         */
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   727
        public void runWithoutAccessChecks(Runnable r) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   728
            boolean prevNoAccessCheckes = noAccessChecks;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   729
            try {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   730
                noAccessChecks = true;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   731
                r.run();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   732
            } finally {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   733
                noAccessChecks = prevNoAccessCheckes;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   734
            }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   735
        }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   736
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   737
        @Override
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   738
        public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   739
            if (noAccessChecks) return true;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   740
            return super.isAccessible(env, c, checkInner);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   741
        }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   742
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   743
        @Override
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   744
        public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   745
            if (noAccessChecks) return true;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   746
            return super.isAccessible(env, site, sym, checkInner);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   747
        }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   748
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   749
        private static final class Marker {}
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   750
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47350
diff changeset
   751
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   752
}