test/langtools/jdk/jshell/VariablesTest.java
author jlahoda
Fri, 19 Jan 2018 17:11:52 +0100
changeset 48610 a587f95313f1
parent 47268 48ec75306997
child 48649 6a014a1e8d2b
permissions -rw-r--r--
8191842: JShell: Inferred type information is lost when assigning types to a \"var\" Summary: For vars, upgrading all anonymous classes to member classes; stripping intersection types from fields before writing. Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
     2
 * Copyright (c) 2015, 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.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * 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
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * 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
    12
 * 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
    13
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 * 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
    18
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * 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
    20
 * 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
    21
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
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
 * @test
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
    26
 * @bug 8144903 8177466 8191842
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    27
 * @summary Tests for EvaluationState.variables
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
    28
 * @library /tools/lib
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
    29
 * @build Compiler KullaTesting TestingInputStream ExpectedDiagnostic
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
 * @run testng VariablesTest
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
    33
import java.nio.file.Path;
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
    34
import java.nio.file.Paths;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
import java.util.List;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import javax.tools.Diagnostic;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import jdk.jshell.Snippet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import jdk.jshell.TypeDeclSnippet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import jdk.jshell.VarSnippet;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import jdk.jshell.Snippet.SubKind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import jdk.jshell.SnippetEvent;
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
    43
import org.testng.annotations.BeforeMethod;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import org.testng.annotations.Test;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 37644
diff changeset
    46
import static java.util.stream.Collectors.toList;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import static jdk.jshell.Snippet.Status.*;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import static jdk.jshell.Snippet.SubKind.VAR_DECLARATION_SUBKIND;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
import static org.testng.Assert.assertEquals;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import static org.testng.Assert.fail;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
@Test
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
public class VariablesTest extends KullaTesting {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
    public void noVariables() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
        assertNumberOfActiveVariables(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
    private void badVarValue(VarSnippet key) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
            getState().varValue(key);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
            fail("Expected exception for: " + key.source());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
        } catch (IllegalArgumentException e) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
            // ok
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
    public void testVarValue1() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
        VarSnippet v1 = varKey(assertEval("und1 a;", added(RECOVERABLE_NOT_DEFINED)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
        badVarValue(v1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    71
        VarSnippet v2 = varKey(assertEval("und2 a;",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
                ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, RECOVERABLE_NOT_DEFINED, false, null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
                ste(v1, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    74
        badVarValue(v2);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
        TypeDeclSnippet und = classKey(assertEval("class und2 {}",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
                added(VALID),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
                ste(v2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
        assertVarValue(v2, "null");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
        assertDrop(und,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
                DiagCheck.DIAG_OK,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    81
                DiagCheck.DIAG_ERROR,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
                ste(und, VALID, DROPPED, true, null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
                ste(v2, VALID, RECOVERABLE_NOT_DEFINED, true, und));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
        badVarValue(v1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
        badVarValue(v2);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
    public void testVarValue2() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
        VarSnippet v1 = (VarSnippet) assertDeclareFail("int a = 0.0;", "compiler.err.prob.found.req");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
        badVarValue(v1);
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 34750
diff changeset
    91
        VarSnippet v2 = varKey(assertEval("int a = 0;", added(VALID)));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
        assertDrop(v2, ste(MAIN_SNIPPET, VALID, DROPPED, true, null));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
        badVarValue(v2);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
    }
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
    public void testSignature1() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    97
        VarSnippet v1 = varKey(assertEval("und1 a;", added(RECOVERABLE_NOT_DEFINED)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    98
        assertVariableDeclSnippet(v1, "a", "und1", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    99
        VarSnippet v2 = varKey(assertEval("und2 a;",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   100
                ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, RECOVERABLE_NOT_DEFINED, false, null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
                ste(v1, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
        assertVariableDeclSnippet(v2, "a", "und2", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
        TypeDeclSnippet und = classKey(assertEval("class und2 {}",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   104
                added(VALID),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
                ste(v2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
        assertVariableDeclSnippet(v2, "a", "und2", VALID, VAR_DECLARATION_SUBKIND, 0, 0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   107
        assertDrop(und,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   108
                DiagCheck.DIAG_OK,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   109
                DiagCheck.DIAG_ERROR,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   110
                ste(und, VALID, DROPPED, true, null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   111
                ste(v2, VALID, RECOVERABLE_NOT_DEFINED, true, und));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   112
        assertVariableDeclSnippet(v2, "a", "und2", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   114
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   115
    public void testSignature2() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   116
        VarSnippet v1 = (VarSnippet) assertDeclareFail("int a = 0.0;", "compiler.err.prob.found.req");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
        assertVariableDeclSnippet(v1, "a", "int", REJECTED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   118
        VarSnippet v2 = varKey(assertEval("int a = 0;",
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 34750
diff changeset
   119
                added(VALID)));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   120
        assertVariableDeclSnippet(v2, "a", "int", VALID, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   121
        assertDrop(v2, ste(MAIN_SNIPPET, VALID, DROPPED, true, null));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   122
        assertVariableDeclSnippet(v2, "a", "int", DROPPED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 0);
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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
    public void variables() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   126
        VarSnippet snx = varKey(assertEval("int x = 10;"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   127
        VarSnippet sny = varKey(assertEval("String y = \"hi\";"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   128
        VarSnippet snz = varKey(assertEval("long z;"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   129
        assertVariables(variable("int", "x"), variable("String", "y"), variable("long", "z"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   130
        assertVarValue(snx, "10");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
        assertVarValue(sny, "\"hi\"");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   132
        assertVarValue(snz, "0");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
        assertActiveKeys();
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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
    public void variablesArray() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
        VarSnippet sn = varKey(assertEval("int[] a = new int[12];"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
        assertEquals(sn.typeName(), "int[]");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
        assertEval("int len = a.length;", "12");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
        assertVariables(variable("int[]", "a"), variable("int", "len"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   143
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
    public void variablesArrayOld() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   145
        VarSnippet sn = varKey(assertEval("int a[] = new int[12];"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   146
        assertEquals(sn.typeName(), "int[]");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
        assertEval("int len = a.length;", "12");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   148
        assertVariables(variable("int[]", "a"), variable("int", "len"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   149
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
    public void variablesRedefinition() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   153
        Snippet x = varKey(assertEval("int x = 10;"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   154
        Snippet y = varKey(assertEval("String y = \"\";", added(VALID)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   155
        assertVariables(variable("int", "x"), variable("String", "y"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   156
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   157
        assertEval("long x;",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   158
                ste(MAIN_SNIPPET, VALID, VALID, true, null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
                ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   160
        assertVariables(variable("long", "x"), variable("String", "y"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   161
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
        assertEval("String y;",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   163
                ste(MAIN_SNIPPET, VALID, VALID, false, null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
                ste(y, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
        assertVariables(variable("long", "x"), variable("String", "y"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   168
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   169
    public void variablesTemporary() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   170
        assertEval("int $1 = 10;", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   171
        assertEval("2 * $1;", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   172
        assertVariables(variable("int", "$1"), variable("int", "$2"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
        assertEval("String y;", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
        assertVariables(variable("int", "$1"), variable("int", "$2"), variable("String", "y"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
    public void variablesTemporaryNull() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
        assertEval("null;", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
        assertVariables(variable("Object", "$1"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
        assertEval("(String) null;", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
        assertVariables(variable("Object", "$1"), variable("String", "$2"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
        assertEval("\"\";", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
        assertVariables(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
                variable("Object", "$1"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
                variable("String", "$2"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
                variable("String", "$3"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
34750
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   193
    public void variablesTemporaryArrayOfCapturedType() {
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   194
        assertEval("class Test<T> { T[][] get() { return null; } }", added(VALID));
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   195
        assertEval("Test<? extends String> test() { return new Test<>(); }", added(VALID));
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   196
        assertEval("test().get()", added(VALID));
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   197
        assertVariables(variable("String[][]", "$1"));
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   198
        assertEval("\"\".getClass().getEnumConstants()", added(VALID));
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   199
        assertVariables(variable("String[][]", "$1"), variable("String[]", "$2"));
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   200
        assertActiveKeys();
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   201
    }
36d62753f5da 8144903: JShell: determine incorrectly the type of the expression which is array type of captured type
rfield
parents: 33362
diff changeset
   202
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
    public void variablesClassReplace() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
        assertEval("import java.util.*;", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
        Snippet var = varKey(assertEval("List<Integer> list = new ArrayList<>();", "[]",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   206
                added(VALID)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   207
        assertVariables(variable("List<Integer>", "list"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
        assertEval("class List {}",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
                DiagCheck.DIAG_OK,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   210
                DiagCheck.DIAG_ERROR,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   211
                added(VALID),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   212
                ste(var, VALID, RECOVERABLE_NOT_DEFINED, true, MAIN_SNIPPET));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   213
        assertVariables();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
        assertEval("List list = new List();",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   215
                DiagCheck.DIAG_OK, DiagCheck.DIAG_IGNORE,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   216
                ste(MAIN_SNIPPET, RECOVERABLE_NOT_DEFINED, VALID, true, null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   217
                ste(var, RECOVERABLE_NOT_DEFINED, OVERWRITTEN, false, MAIN_SNIPPET));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   218
        assertVariables(variable("List", "list"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   219
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   220
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   221
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   222
    public void variablesErrors() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   223
        assertDeclareFail("String;", new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 6, 0, -1, -1, Diagnostic.Kind.ERROR));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   224
        assertNumberOfActiveVariables(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   225
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   226
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   227
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   228
    public void variablesUnresolvedActiveFailed() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
        VarSnippet key = varKey(assertEval("und x;", added(RECOVERABLE_NOT_DEFINED)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
        assertVariableDeclSnippet(key, "x", "und", RECOVERABLE_NOT_DEFINED, VAR_DECLARATION_SUBKIND, 1, 0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   231
        assertUnresolvedDependencies1(key, RECOVERABLE_NOT_DEFINED, "class und");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   232
        assertNumberOfActiveVariables(1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   233
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   234
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   235
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   236
    public void variablesUnresolvedError() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   237
        assertDeclareFail("und y = null;", new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 3, 0, -1, -1, Diagnostic.Kind.ERROR));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   238
        assertNumberOfActiveVariables(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   239
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   240
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   241
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
    public void variablesMultiByteCharacterType() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   243
        assertEval("class \u3042 {}");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
        assertEval("\u3042 \u3042 = null;", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
        assertVariables(variable("\u3042", "\u3042"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   246
        assertEval("new \u3042()", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
        assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   249
        assertEval("class \u3042\u3044\u3046\u3048\u304a {}");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   250
        assertEval("\u3042\u3044\u3046\u3048\u304a \u3042\u3044\u3046\u3048\u304a = null;", added(VALID));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
        assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   252
                variable("\u3042\u3044\u3046\u3048\u304a", "\u3042\u3044\u3046\u3048\u304a"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   253
        assertEval("new \u3042\u3044\u3046\u3048\u304a();");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   254
        assertVariables(variable("\u3042", "\u3042"), variable("\u3042", "$1"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   255
                variable("\u3042\u3044\u3046\u3048\u304a", "\u3042\u3044\u3046\u3048\u304a"),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
                variable("\u3042\u3044\u3046\u3048\u304a", "$2"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   258
    }
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
    @Test(enabled = false) // TODO 8081689
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
    public void methodVariablesAreNotVisible() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   262
        Snippet foo = varKey(assertEval("int foo() {" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
                        "int x = 10;" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
                        "int y = 2 * x;" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
                        "return x * y;" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
                        "}", added(VALID)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
        assertNumberOfActiveVariables(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   268
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   269
        assertEval("int x = 10;", "10");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   270
        assertEval("int foo() {" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   271
                        "int y = 2 * x;" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
                        "return x * y;" +
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
                ste(foo, VALID, VALID, false, null));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   275
        assertVariables(variable("int", "x"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   276
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   277
        assertEval("foo();", "200");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   278
        assertVariables(variable("int", "x"), variable("int", "$1"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   279
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   280
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   281
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   282
    @Test(enabled = false) // TODO 8081689
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   283
    public void classFieldsAreNotVisible() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   284
        Snippet key = classKey(assertEval("class clazz {" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   285
                        "int x = 10;" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   286
                        "int y = 2 * x;" +
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
        assertNumberOfActiveVariables(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   289
        assertEval("int x = 10;", "10");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   290
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   291
        assertEval(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   292
                "class clazz {" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   293
                        "int y = 2 * x;" +
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   294
                        "}",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   295
                ste(key, VALID, VALID, true, null));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   296
        assertVariables(variable("int", "x"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   297
        assertEval("new clazz().y;", "20");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   298
        assertVariables(variable("int", "x"), variable("int", "$1"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   299
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   300
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   301
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   302
    public void multiVariables() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   303
        List<SnippetEvent> abc = assertEval("int a, b, c = 10;",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   304
                DiagCheck.DIAG_OK, DiagCheck.DIAG_OK,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   305
                chain(added(VALID)),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   306
                chain(added(VALID)),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   307
                chain(added(VALID)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   308
        Snippet a = abc.get(0).snippet();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   309
        Snippet b = abc.get(1).snippet();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   310
        Snippet c = abc.get(2).snippet();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   311
        assertVariables(variable("int", "a"), variable("int", "b"), variable("int", "c"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   312
        assertEval("double a = 1.4, b = 8.8;", DiagCheck.DIAG_OK, DiagCheck.DIAG_OK,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   313
                chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   314
                chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   315
        assertVariables(variable("double", "a"), variable("double", "b"), variable("int", "c"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   316
        assertEval("double c = a + b;",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   317
                ste(MAIN_SNIPPET, VALID, VALID, true, null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   318
                ste(c, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   319
        assertVariables(variable("double", "a"), variable("double", "b"), variable("double", "c"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   320
        assertActiveKeys();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   321
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   322
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   323
    public void syntheticVariables() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   324
        assertEval("assert false;");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   325
        assertNumberOfActiveVariables(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   326
        assertActiveKeys();
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
    public void undefinedReplaceVariable() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   330
        Snippet key = varKey(assertEval("int d = 234;", "234"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   331
        assertVariables(variable("int", "d"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   332
        String src = "undefined d;";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   333
        Snippet undefKey = varKey(assertEval(src,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   334
                ste(MAIN_SNIPPET, VALID, RECOVERABLE_NOT_DEFINED, true, null),
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   335
                ste(key, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   336
        //assertEquals(getState().source(snippet), src);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   337
        //assertEquals(snippet, undefKey);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   338
        assertEquals(getState().status(undefKey), RECOVERABLE_NOT_DEFINED);
40304
0318f4e75c6d 8143964: JShell API: convert query responses to Stream instead of List
rfield
parents: 37644
diff changeset
   339
        List<String> unr = getState().unresolvedDependencies((VarSnippet) undefKey).collect(toList());;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   340
        assertEquals(unr.size(), 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   341
        assertEquals(unr.get(0), "class undefined");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   342
        assertVariables(variable("undefined", "d"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   343
    }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   344
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   345
    public void lvti() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   346
        assertEval("var d = 234;", "234");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   347
        assertEval("class Test<T> { T[][] get() { return null; } }", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   348
        assertEval("Test<? extends String> test() { return new Test<>(); }", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   349
        assertEval("var t = test().get();", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   350
        assertEval("<Z extends Runnable & CharSequence> Z get1() { return null; }", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   351
        assertEval("var i1 = get1();", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   352
        assertEval("void t1() { i1.run(); i1.length(); }", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   353
        assertEval("i1 = 1;", DiagCheck.DIAG_ERROR, DiagCheck.DIAG_OK, ste(MAIN_SNIPPET, NONEXISTENT, REJECTED, false, null));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   354
        assertEval("<Z extends Number & CharSequence> Z get2() { return null; }", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   355
        assertEval("var i2 = get2();", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   356
        assertEval("void t2() { i2.length(); }", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   357
        assertEval("var r1 = new Runnable() { public void run() { } public String get() { return \"good\"; } };", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   358
        assertEval("Runnable r2 = r1;");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   359
        assertEval("r1.get()", "\"good\"");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   360
        assertEval("var v = r1.get();", "\"good\"");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   361
        assertEval("var r3 = new java.util.ArrayList<String>(42) { public String get() { return \"good\"; } };", added(VALID));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   362
        assertEval("r3.get()", "\"good\"");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   363
        assertEval("class O { public class Inner { public String test() { return \"good\"; } } }");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   364
        assertEval("var r4 = new O().new Inner() { public String get() { return \"good\"; } };");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   365
        assertEval("r4.get()", "\"good\"");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   366
        assertEval("class O2 { public class Inner { public Inner(int i) { } public String test() { return \"good\"; } } }");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   367
        assertEval("var r5 = new O2().new Inner(1) { public String get() { return \"good\"; } };");
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   368
        assertEval("r5.get()", "\"good\"");
48610
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   369
        assertEval("<Z> Z identity(Z z) { return z; }");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   370
        assertEval("var r6 = identity(new Object() { String s = \"good\"; });");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   371
        assertEval("r6.s", "\"good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   372
        assertEval("interface I<B, C> { C get(B b); }");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   373
        assertEval("<A, B, C> C cascade(A a, I<A, B> c1, I<B, C> c2) { return c2.get(c1.get(a)); }");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   374
        assertEval("var r7 = cascade(\"good\", a -> new Object() { String s = a; }, b -> new java.util.ArrayList<String>(5) { String s = b.s; });");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   375
        assertEval("r7.s", "\"good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   376
        assertEval("var r8 = cascade(\"good\", a -> new Object() { String s = a; public String getS() { return s; } }, b -> new java.util.ArrayList<String>(5) { String s = b.getS(); public String getS() { return s; } });");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   377
        assertEval("r8.getS()", "\"good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   378
        assertEval("var r9 = new Object() { class T { class Inner { public String g() { return outer(); } } public String outer() { return \"good\"; } public String test() { return new Inner() {}.g(); } } public String test() { return new T().test(); } };");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   379
        assertEval("r9.test()", "\"good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   380
        assertEval("var nested1 = new Object() { class N { public String get() { return \"good\"; } } };");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   381
        assertEval("nested1.new N().get()", "\"good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   382
        assertEval("var nested2 = cascade(\"good\", a -> new Object() { abstract class G { abstract String g(); } G g = new G() { String g() { return a; } }; }, b -> new java.util.ArrayList<String>(5) { String s = b.g.g(); });");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   383
        assertEval("nested2.s", "\"good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   384
        assertEval("<A, B> B convert(A a, I<A, B> c) { return c.get(a); }");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   385
        assertEval("var r10 = convert(\"good\", a -> new api.C(12) { public String val = \"\" + i + s + l + a; } );");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   386
        assertEval("r10.val", "\"12empty[empty]good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   387
        assertEval("var r11 = convert(\"good\", a -> new api.C(\"a\") { public String val = \"\" + i + s + l + a; } );");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   388
        assertEval("r11.val", "\"3a[empty]good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   389
        assertEval("import api.C;");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   390
        assertEval("var r12 = convert(\"good\", a -> new C(java.util.List.of(\"a\")) { public String val = \"\" + i + s + l + a; } );");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   391
        assertEval("r12.val", "\"4empty[a]good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   392
        assertEval("var r13 = convert(\"good\", a -> new api.G<String>(java.util.List.of(\"b\")) { public String val = \"\" + l + a; } );");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   393
        assertEval("r13.val", "\"[b]good\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   394
        assertEval("var r14 = convert(\"good\", a -> new api.J<String>() { public java.util.List<String> get() { return java.util.List.of(a, \"c\"); } } );");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   395
        assertEval("r14.get()", "[good, c]");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   396
        assertEval("var r15a = new java.util.ArrayList<String>();");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   397
        assertEval("r15a.add(\"a\");");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   398
        assertEval("var r15b = r15a.get(0);");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   399
        assertEval("r15b", "\"a\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   400
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   401
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   402
    public void test8191842() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   403
        assertEval("import java.util.stream.*;");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   404
        assertEval("var list = Stream.of(1, 2, 3).map(j -> new Object() { int i = j; }).collect(Collectors.toList());");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   405
        assertEval("list.stream().map(a -> String.valueOf(a.i)).collect(Collectors.joining(\", \"));", "\"1, 2, 3\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   406
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   407
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   408
    public void lvtiRecompileDependentsWithIntersectionTypes() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   409
        assertEval("<Z extends Runnable & CharSequence> Z get1() { return null; }", added(VALID));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   410
        VarSnippet var = varKey(assertEval("var i1 = get1();", added(VALID)));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   411
        assertEval("import java.util.stream.*;", added(VALID),
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   412
                                                 ste(var, VALID, VALID, true, MAIN_SNIPPET));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   413
        assertEval("void t1() { i1.run(); i1.length(); }", added(VALID));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   414
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   415
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   416
    public void arrayInit() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   417
        assertEval("int[] d = {1, 2, 3};");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   418
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   419
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   420
    public void testAnonymousVar() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   421
        assertEval("new Object() { public String get() { return \"a\"; } }");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   422
        assertEval("$1.get()", "\"a\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   423
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   424
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   425
    public void testIntersectionVar() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   426
        assertEval("<Z extends Runnable & CharSequence> Z get() { return null; }", added(VALID));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   427
        assertEval("get();", added(VALID));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   428
        assertEval("void t1() { $1.run(); $1.length(); }", added(VALID));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   429
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   430
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   431
    public void multipleCaptures() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   432
        assertEval("class D { D(int foo, String bar) { this.foo = foo; this.bar = bar; } int foo; String bar; } ");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   433
        assertEval("var d = new D(34, \"hi\") { String z = foo + bar; };");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   434
        assertEval("d.z", "\"34hi\"");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   435
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   436
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   437
    public void multipleAnonymous() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   438
        VarSnippet v1 = varKey(assertEval("new Object() { public int i = 42; public int i1 = i; public int m1() { return i1; } };"));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   439
        VarSnippet v2 = varKey(assertEval("new Object() { public int i = 42; public int i2 = i; public int m2() { return i2; } };"));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   440
        assertEval(v1.name() + ".i", "42");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   441
        assertEval(v1.name() + ".i1", "42");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   442
        assertEval(v1.name() + ".m1()", "42");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   443
        assertDeclareFail(v1.name() + ".i2",
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   444
                          new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 5, 2,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   445
                                                 -1, -1, Diagnostic.Kind.ERROR));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   446
        assertEval(v2.name() + ".i", "42");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   447
        assertEval(v2.name() + ".i2", "42");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   448
        assertEval(v2.name() + ".m2()", "42");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   449
        assertDeclareFail(v2.name() + ".i1",
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   450
                          new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 5, 2,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   451
                                                 -1, -1, Diagnostic.Kind.ERROR));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   452
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   453
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   454
    public void displayName() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   455
        assertVarDisplayName("var v1 = 234;", "int");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   456
        assertVarDisplayName("var v2 = new int[] {234};", "int[]");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   457
        assertEval("<Z extends Runnable & CharSequence> Z get() { return null; }", added(VALID));
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   458
        assertVarDisplayName("var v3 = get();", "CharSequence&Runnable");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   459
        assertVarDisplayName("var v4a = new java.util.ArrayList<String>();", "java.util.ArrayList<String>");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   460
        assertEval("v4a.add(\"a\");");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   461
        assertVarDisplayName("var v4b = v4a.get(0);", "String");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   462
        assertVarDisplayName("var v5 = new Object() { };", "<anonymous class extending Object>");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   463
        assertVarDisplayName("var v6 = new Runnable() { public void run() { } };", "<anonymous class implementing Runnable>");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   464
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   465
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   466
    private void assertVarDisplayName(String var, String typeName) {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   467
        assertEquals(varKey(assertEval(var)).typeName(), typeName);
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   468
    }
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   469
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   470
    @BeforeMethod
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   471
    @Override
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   472
    public void setUp() {
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   473
        Path path = Paths.get("cp");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   474
        Compiler compiler = new Compiler();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   475
        compiler.compile(path,
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   476
                "package api;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   477
                "\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   478
                "import java.util.List;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   479
                "\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   480
                "public class C {\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   481
                "   public int i;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   482
                "   public String s;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   483
                "   public List<String> l;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   484
                "   public C(int i) {\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   485
                "       this.i = i;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   486
                "       this.s = \"empty\";\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   487
                "       this.l = List.of(\"empty\");\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   488
                "   }\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   489
                "   public C(String s) {\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   490
                "       this.i = 3;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   491
                "       this.s = s;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   492
                "       this.l = List.of(\"empty\");\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   493
                "   }\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   494
                "   public C(List<String> l) {\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   495
                "       this.i = 4;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   496
                "       this.s = \"empty\";\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   497
                "       this.l = l;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   498
                "   }\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   499
                "}\n",
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   500
                "package api;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   501
                "\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   502
                "import java.util.List;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   503
                "\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   504
                "public class G<T> {\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   505
                "   public List<T> l;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   506
                "   public G(List<T> l) {\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   507
                "       this.l = l;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   508
                "   }\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   509
                "}\n",
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   510
                "package api;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   511
                "\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   512
                "import java.util.List;\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   513
                "\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   514
                "public interface J<T> {\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   515
                "   public List<T> get();\n" +
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   516
                "}\n");
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   517
        String tpath = compiler.getPath(path).toString();
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   518
        setUp(b -> b
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   519
                .remoteVMOptions("--class-path", tpath)
a587f95313f1 8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents: 47268
diff changeset
   520
                .compilerOptions("--class-path", tpath));
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   521
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   522
}