1 /*
2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test 8190939 8191842
26  * @summary test expressions whose type is inaccessible
27  * @modules jdk.compiler/com.sun.tools.javac.api
28  *          jdk.compiler/com.sun.tools.javac.main
29  *          jdk.jdeps/com.sun.tools.javap
30  * @library /tools/lib
31  * @build KullaTesting Compiler
32  * @run testng InaccessibleExpressionTest
33  */
34 
35 
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 
39 import org.testng.annotations.BeforeMethod;
40 import jdk.jshell.VarSnippet;
41 import org.testng.annotations.Test;
42 
43 import static org.testng.Assert.assertEquals;
44 
45 @Test
46 public class InaccessibleExpressionTest extends KullaTesting {
47 
48     @BeforeMethod
49     @Override
setUp()50     public void setUp() {
51         Path path = Paths.get("eit");
52         Compiler compiler = new Compiler();
53         compiler.compile(path,
54                 "package priv;\n" +
55                 "\n" +
56                 "import java.util.function.Supplier;\n" +
57                 "import java.util.ArrayList;\n" +
58                 "\n" +
59                 "public class GetPriv {\n" +
60                 "   private enum Count { One };\n" +
61                 "   public static Packp down() { return new Packp(); }\n" +
62                 "   public static MyList list() { return new MyList(); }\n" +
63                 "   public static Count priv() { return Count.One; }\n" +
64                 "}\n" +
65                 "\n" +
66                 "class Packp extends Packp2 {\n" +
67                         "public String toString() { return \"Packp\"; } }\n" +
68                 "\n" +
69                 "class Packp2 implements Supplier<Integer>  {" +
70                         "public Integer get() { return 5; }}\n" +
71                 "\n" +
72                 "class MyList extends ArrayList<Integer> {}");
73         String tpath = compiler.getPath(path).toString();
74         setUp(b -> b
75                 .remoteVMOptions("--class-path", tpath)
76                 .compilerOptions("--class-path", tpath));
77     }
78 
testExternal()79     public void testExternal() {
80         assertEval("import static priv.GetPriv.*;");
81         VarSnippet down = varKey(assertEval("down()", "Packp"));
82         assertEquals(down.typeName(), "priv.Packp");
83         assertEval(down.name() + ".get()", "5");
84         VarSnippet list = varKey(assertEval("list()", "[]"));
85         assertEquals(list.typeName(), "priv.MyList");
86         assertEval(list.name() + ".size()", "0");
87         VarSnippet one = varKey(assertEval("priv()", "One"));
88         assertEquals(one.typeName(), "priv.GetPriv.Count");
89         assertEval("var v = down();", "Packp");
90         assertDeclareFail("v.toString()", "compiler.err.not.def.access.class.intf.cant.access");
91     }
92 
testInternal()93     public void testInternal() {
94         assertEval(
95                 "class Top {" +
96                 "    private class Inner {" +
97                 "        public String toString() { return \"Inner\"; }" +
98                 "    }" +
99                 "    Inner n = new Inner(); }");
100         VarSnippet n = varKey(assertEval("new Top().n", "Inner"));
101         assertEquals(n.typeName(), "Top.Inner");
102     }
103 
104 }
105