Fri, 24 Apr 2020 03:58:51 +0100
Merge
vromero@1591 | 1 | /* |
vromero@1591 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
vromero@1591 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
vromero@1591 | 4 | * |
vromero@1591 | 5 | * This code is free software; you can redistribute it and/or modify it |
vromero@1591 | 6 | * under the terms of the GNU General Public License version 2 only, as |
vromero@1591 | 7 | * published by the Free Software Foundation. |
vromero@1591 | 8 | * |
vromero@1591 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
vromero@1591 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
vromero@1591 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
vromero@1591 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
vromero@1591 | 13 | * accompanied this code). |
vromero@1591 | 14 | * |
vromero@1591 | 15 | * You should have received a copy of the GNU General Public License version |
vromero@1591 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
vromero@1591 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
vromero@1591 | 18 | * |
vromero@1591 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
vromero@1591 | 20 | * or visit www.oracle.com if you need additional information or have any |
vromero@1591 | 21 | * questions. |
vromero@1591 | 22 | */ |
vromero@1591 | 23 | |
vromero@1591 | 24 | /* |
vromero@1591 | 25 | * @test |
vromero@1591 | 26 | * @bug 4087314 4800342 4307565 |
vromero@1591 | 27 | * @summary Verify allowed access to protected class from another package |
vromero@1591 | 28 | * @library /tools/javac/lib |
vromero@1591 | 29 | * @build ToolBox |
vromero@1591 | 30 | * @run main ProtectedInnerClassesTest |
vromero@1591 | 31 | */ |
vromero@1591 | 32 | |
vromero@1591 | 33 | //original tests: test/tools/javac/ProtectedInnerClass/ProtectedInnerClass.sh |
vromero@1591 | 34 | //and test/tools/javac/ProtectedInnerClass/ProtectedInnerClass_2.java |
vromero@1591 | 35 | public class ProtectedInnerClassesTest { |
vromero@1591 | 36 | |
vromero@1591 | 37 | private static final String protectedInnerClass1Src = |
vromero@1591 | 38 | "package p1;\n" + |
vromero@1591 | 39 | "\n" + |
vromero@1591 | 40 | "public class ProtectedInnerClass1 {\n" + |
vromero@1591 | 41 | " protected class Foo {\n" + |
vromero@1591 | 42 | " public String getBar() { return \"bar\"; }\n" + |
vromero@1591 | 43 | " }\n" + |
vromero@1591 | 44 | "}"; |
vromero@1591 | 45 | |
vromero@1591 | 46 | private static final String protectedInnerClass2Src = |
vromero@1591 | 47 | "package p2;\n" + |
vromero@1591 | 48 | "\n" + |
vromero@1591 | 49 | "public class ProtectedInnerClass2 extends p1.ProtectedInnerClass1\n" + |
vromero@1591 | 50 | "{\n" + |
vromero@1591 | 51 | " class Bug extends Foo {\n" + |
vromero@1591 | 52 | " String getBug() { return getBar(); }\n" + |
vromero@1591 | 53 | " }\n" + |
vromero@1591 | 54 | "\n" + |
vromero@1591 | 55 | " public static void main(String[] args) {\n" + |
vromero@1591 | 56 | " ProtectedInnerClass2 x = new ProtectedInnerClass2();\n" + |
vromero@1591 | 57 | " Bug y = x.new Bug();\n" + |
vromero@1591 | 58 | " System.out.println(y.getBug());\n" + |
vromero@1591 | 59 | " }\n" + |
vromero@1591 | 60 | "}"; |
vromero@1591 | 61 | |
vromero@1591 | 62 | private static final String protectedInnerClass3Src = |
vromero@1591 | 63 | "package p2;\n" + |
vromero@1591 | 64 | "\n" + |
vromero@1591 | 65 | "public class ProtectedInnerClass3 {\n" + |
vromero@1591 | 66 | "\n" + |
vromero@1591 | 67 | " void test() {\n" + |
vromero@1591 | 68 | " p1.ProtectedInnerClass1.Foo x;\n" + |
vromero@1591 | 69 | " }\n" + |
vromero@1591 | 70 | "\n" + |
vromero@1591 | 71 | "}"; |
vromero@1591 | 72 | |
vromero@1591 | 73 | public static void main(String args[]) throws Exception { |
vromero@1591 | 74 | new ProtectedInnerClassesTest().run(); |
vromero@1591 | 75 | } |
vromero@1591 | 76 | |
vromero@1591 | 77 | void run() throws Exception { |
vromero@1591 | 78 | compileAndExecute(); |
vromero@1591 | 79 | compileOnly(); |
vromero@1591 | 80 | } |
vromero@1591 | 81 | |
vromero@1591 | 82 | void compileAndExecute() throws Exception { |
vromero@1591 | 83 | //"${TESTJAVA}${FS}bin${FS}javac" ${TESTTOOLVMOPTS} -d "${TESTCLASSES}" "${TESTSRC}${FS}p1${FS}ProtectedInnerClass1.java" "${TESTSRC}${FS}p2${FS}ProtectedInnerClass2.java" |
vromero@1591 | 84 | ToolBox.JavaToolArgs javacParams = |
vromero@1591 | 85 | new ToolBox.JavaToolArgs() |
vromero@1591 | 86 | .setOptions("-d", ".") |
vromero@1591 | 87 | .setSources(protectedInnerClass1Src, protectedInnerClass2Src); |
vromero@1591 | 88 | |
vromero@1591 | 89 | ToolBox.javac(javacParams); |
vromero@1591 | 90 | |
vromero@1591 | 91 | //"${TESTJAVA}${FS}bin${FS}java" ${TESTVMOPTS} -classpath "${CLASSPATH}${PS}${TESTCLASSES}" p2.ProtectedInnerClass2 |
vromero@1591 | 92 | ToolBox.AnyToolArgs javaParams = |
vromero@1591 | 93 | new ToolBox.AnyToolArgs() |
vromero@1637 | 94 | .appendArgs(ToolBox.javaBinary) |
vromero@1637 | 95 | .appendArgs(ToolBox.testVMOpts) |
vromero@1637 | 96 | .appendArgs("-classpath", System.getProperty("user.dir"), |
vromero@1591 | 97 | "p2.ProtectedInnerClass2"); |
vromero@1591 | 98 | ToolBox.executeCommand(javaParams); |
vromero@1591 | 99 | } |
vromero@1591 | 100 | |
vromero@1591 | 101 | //from test/tools/javac/ProtectedInnerClass/ProtectedInnerClass_2.java |
vromero@1591 | 102 | void compileOnly() throws Exception { |
vromero@1591 | 103 | //@run compile p1/ProtectedInnerClass1.java |
vromero@1591 | 104 | ToolBox.JavaToolArgs javacParams = |
vromero@1591 | 105 | new ToolBox.JavaToolArgs() |
vromero@1637 | 106 | .appendArgs("-d", ".") |
vromero@1591 | 107 | .setSources(protectedInnerClass1Src); |
vromero@1591 | 108 | |
vromero@1591 | 109 | ToolBox.javac(javacParams); |
vromero@1591 | 110 | |
vromero@1591 | 111 | //@run compile/fail p2/ProtectedInnerClass3.java |
vromero@1637 | 112 | javacParams = new ToolBox.JavaToolArgs(ToolBox.Expect.FAIL) |
vromero@1637 | 113 | .appendArgs("-d", ".") |
vromero@1637 | 114 | .setSources(protectedInnerClass3Src); |
vromero@1591 | 115 | ToolBox.javac(javacParams); |
vromero@1591 | 116 | } |
vromero@1591 | 117 | |
vromero@1591 | 118 | } |