Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
1 /*
2 * Copyright (c) 2013, 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 */
24 /*
25 * @test
26 * @bug 4087314 4800342 4307565
27 * @summary Verify allowed access to protected class from another package
28 * @library /tools/javac/lib
29 * @build ToolBox
30 * @run main ProtectedInnerClassesTest
31 */
33 //original tests: test/tools/javac/ProtectedInnerClass/ProtectedInnerClass.sh
34 //and test/tools/javac/ProtectedInnerClass/ProtectedInnerClass_2.java
35 public class ProtectedInnerClassesTest {
37 private static final String protectedInnerClass1Src =
38 "package p1;\n" +
39 "\n" +
40 "public class ProtectedInnerClass1 {\n" +
41 " protected class Foo {\n" +
42 " public String getBar() { return \"bar\"; }\n" +
43 " }\n" +
44 "}";
46 private static final String protectedInnerClass2Src =
47 "package p2;\n" +
48 "\n" +
49 "public class ProtectedInnerClass2 extends p1.ProtectedInnerClass1\n" +
50 "{\n" +
51 " class Bug extends Foo {\n" +
52 " String getBug() { return getBar(); }\n" +
53 " }\n" +
54 "\n" +
55 " public static void main(String[] args) {\n" +
56 " ProtectedInnerClass2 x = new ProtectedInnerClass2();\n" +
57 " Bug y = x.new Bug();\n" +
58 " System.out.println(y.getBug());\n" +
59 " }\n" +
60 "}";
62 private static final String protectedInnerClass3Src =
63 "package p2;\n" +
64 "\n" +
65 "public class ProtectedInnerClass3 {\n" +
66 "\n" +
67 " void test() {\n" +
68 " p1.ProtectedInnerClass1.Foo x;\n" +
69 " }\n" +
70 "\n" +
71 "}";
73 public static void main(String args[]) throws Exception {
74 new ProtectedInnerClassesTest().run();
75 }
77 void run() throws Exception {
78 compileAndExecute();
79 compileOnly();
80 }
82 void compileAndExecute() throws Exception {
83 //"${TESTJAVA}${FS}bin${FS}javac" ${TESTTOOLVMOPTS} -d "${TESTCLASSES}" "${TESTSRC}${FS}p1${FS}ProtectedInnerClass1.java" "${TESTSRC}${FS}p2${FS}ProtectedInnerClass2.java"
84 ToolBox.JavaToolArgs javacParams =
85 new ToolBox.JavaToolArgs()
86 .setOptions("-d", ".")
87 .setSources(protectedInnerClass1Src, protectedInnerClass2Src);
89 ToolBox.javac(javacParams);
91 //"${TESTJAVA}${FS}bin${FS}java" ${TESTVMOPTS} -classpath "${CLASSPATH}${PS}${TESTCLASSES}" p2.ProtectedInnerClass2
92 ToolBox.AnyToolArgs javaParams =
93 new ToolBox.AnyToolArgs()
94 .appendArgs(ToolBox.javaBinary)
95 .appendArgs(ToolBox.testVMOpts)
96 .appendArgs("-classpath", System.getProperty("user.dir"),
97 "p2.ProtectedInnerClass2");
98 ToolBox.executeCommand(javaParams);
99 }
101 //from test/tools/javac/ProtectedInnerClass/ProtectedInnerClass_2.java
102 void compileOnly() throws Exception {
103 //@run compile p1/ProtectedInnerClass1.java
104 ToolBox.JavaToolArgs javacParams =
105 new ToolBox.JavaToolArgs()
106 .appendArgs("-d", ".")
107 .setSources(protectedInnerClass1Src);
109 ToolBox.javac(javacParams);
111 //@run compile/fail p2/ProtectedInnerClass3.java
112 javacParams = new ToolBox.JavaToolArgs(ToolBox.Expect.FAIL)
113 .appendArgs("-d", ".")
114 .setSources(protectedInnerClass3Src);
115 ToolBox.javac(javacParams);
116 }
118 }