Path: blob/master/test/langtools/jdk/jshell/InaccessibleExpressionTest.java
40931 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test 8190939 819184225* @summary test expressions whose type is inaccessible26* @modules jdk.compiler/com.sun.tools.javac.api27* jdk.compiler/com.sun.tools.javac.main28* jdk.jdeps/com.sun.tools.javap29* @library /tools/lib30* @build KullaTesting Compiler31* @run testng InaccessibleExpressionTest32*/333435import java.nio.file.Path;36import java.nio.file.Paths;3738import org.testng.annotations.BeforeMethod;39import jdk.jshell.VarSnippet;40import org.testng.annotations.Test;4142import static org.testng.Assert.assertEquals;4344@Test45public class InaccessibleExpressionTest extends KullaTesting {4647@BeforeMethod48@Override49public void setUp() {50Path path = Paths.get("eit");51Compiler compiler = new Compiler();52compiler.compile(path,53"package priv;\n" +54"\n" +55"import java.util.function.Supplier;\n" +56"import java.util.ArrayList;\n" +57"\n" +58"public class GetPriv {\n" +59" private enum Count { One };\n" +60" public static Packp down() { return new Packp(); }\n" +61" public static MyList list() { return new MyList(); }\n" +62" public static Count priv() { return Count.One; }\n" +63"}\n" +64"\n" +65"class Packp extends Packp2 {\n" +66"public String toString() { return \"Packp\"; } }\n" +67"\n" +68"class Packp2 implements Supplier<Integer> {" +69"public Integer get() { return 5; }}\n" +70"\n" +71"class MyList extends ArrayList<Integer> {}");72String tpath = compiler.getPath(path).toString();73setUp(b -> b74.remoteVMOptions("--class-path", tpath)75.compilerOptions("--class-path", tpath));76}7778public void testExternal() {79assertEval("import static priv.GetPriv.*;");80VarSnippet down = varKey(assertEval("down()", "Packp"));81assertEquals(down.typeName(), "priv.Packp");82assertEval(down.name() + ".get()", "5");83VarSnippet list = varKey(assertEval("list()", "[]"));84assertEquals(list.typeName(), "priv.MyList");85assertEval(list.name() + ".size()", "0");86VarSnippet one = varKey(assertEval("priv()", "One"));87assertEquals(one.typeName(), "priv.GetPriv.Count");88assertEval("var v = down();", "Packp");89assertDeclareFail("v.toString()", "compiler.err.not.def.access.class.intf.cant.access");90}9192public void testInternal() {93assertEval(94"class Top {" +95" private class Inner {" +96" public String toString() { return \"Inner\"; }" +97" }" +98" Inner n = new Inner(); }");99VarSnippet n = varKey(assertEval("new Top().n", "Inner"));100assertEquals(n.typeName(), "Top.Inner");101}102103}104105106