1 /* 2 * Copyright (c) 2013, 2016, 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 26 * @bug 4087314 4800342 4307565 27 * @summary Verify allowed access to protected class from another package 28 * @library /tools/lib 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * @build toolbox.ToolBox toolbox.JavaTask toolbox.JavacTask 32 * @run main ProtectedInnerClassesTest 33 */ 34 35 import toolbox.JavaTask; 36 import toolbox.JavacTask; 37 import toolbox.Task; 38 import toolbox.ToolBox; 39 40 // Original tests: test/tools/javac/ProtectedInnerClass/ProtectedInnerClass.sh 41 // and test/tools/javac/ProtectedInnerClass/ProtectedInnerClass_2.java 42 public class ProtectedInnerClassesTest { 43 44 private static final String protectedInnerClass1Src = 45 "package p1;\n" + 46 "\n" + 47 "public class ProtectedInnerClass1 {\n" + 48 " protected class Foo {\n" + 49 " public String getBar() { return \"bar\"; }\n" + 50 " }\n" + 51 "}"; 52 53 private static final String protectedInnerClass2Src = 54 "package p2;\n" + 55 "\n" + 56 "public class ProtectedInnerClass2 extends p1.ProtectedInnerClass1\n" + 57 "{\n" + 58 " class Bug extends Foo {\n" + 59 " String getBug() { return getBar(); }\n" + 60 " }\n" + 61 "\n" + 62 " public static void main(String[] args) {\n" + 63 " ProtectedInnerClass2 x = new ProtectedInnerClass2();\n" + 64 " Bug y = x.new Bug();\n" + 65 " System.out.println(y.getBug());\n" + 66 " }\n" + 67 "}"; 68 69 private static final String protectedInnerClass3Src = 70 "package p2;\n" + 71 "\n" + 72 "public class ProtectedInnerClass3 {\n" + 73 "\n" + 74 " void test() {\n" + 75 " p1.ProtectedInnerClass1.Foo x;\n" + 76 " }\n" + 77 "\n" + 78 "}"; 79 main(String args[])80 public static void main(String args[]) throws Exception { 81 new ProtectedInnerClassesTest().run(); 82 } 83 84 ToolBox tb = new ToolBox(); 85 run()86 void run() throws Exception { 87 compileAndExecute(); 88 compileOnly(); 89 } 90 compileAndExecute()91 void compileAndExecute() throws Exception { 92 new JavacTask(tb) 93 .outdir(".") 94 .sources(protectedInnerClass1Src, protectedInnerClass2Src) 95 .run(); 96 97 new JavaTask(tb) 98 .classpath(System.getProperty("user.dir")) 99 .className("p2.ProtectedInnerClass2") 100 .run(); 101 } 102 103 //from test/tools/javac/ProtectedInnerClass/ProtectedInnerClass_2.java compileOnly()104 void compileOnly() throws Exception { 105 new JavacTask(tb) 106 .outdir(".") 107 .sources(protectedInnerClass1Src) 108 .run(); 109 110 new JavacTask(tb) 111 .outdir(".") 112 .sources(protectedInnerClass3Src) 113 .run(Task.Expect.FAIL); 114 } 115 116 } 117