1 /* 2 * Copyright (c) 2017, 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 8046171 27 * @summary Test access to private static methods between nestmates and nest-top 28 * using different flavours of named nested types using core reflection 29 * @compile -XDdisablePrivateAccessors TestReflection.java 30 * @run main TestReflection 31 */ 32 33 public class TestReflection { 34 35 // Private method of nest-top for nestmates to access 36 private static void priv_static_invoke() { 37 System.out.println("TestReflection::priv_static_invoke"); 38 } 39 40 // public constructor so we aren't relying on private access 41 public TestReflection() {} 42 43 // Methods that will access private static methods of nestmates 44 45 // NOTE: No InnerNested calls in this test because non-static nested types 46 // can't have static methods 47 48 void access_priv(TestReflection o) throws Throwable { 49 o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 50 } 51 void access_priv(StaticNested o) throws Throwable { 52 o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 53 } 54 void access_priv(StaticIface o) throws Throwable { 55 StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 56 } 57 58 // The various nestmates 59 60 static interface StaticIface { 61 62 private static void priv_static_invoke() { 63 System.out.println("StaticIface::priv_static_invoke"); 64 } 65 66 // Methods that will access private static methods of nestmates 67 68 default void access_priv(TestReflection o) throws Throwable { 69 o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 70 } 71 default void access_priv(StaticNested o) throws Throwable { 72 o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 73 } 74 default void access_priv(StaticIface o) throws Throwable { 75 StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 76 } 77 } 78 79 static class StaticNested { 80 81 private static void priv_static_invoke() { 82 System.out.println("StaticNested::priv_static_invoke"); 83 } 84 85 // public constructor so we aren't relying on private access 86 public StaticNested() {} 87 88 // Methods that will access private static methods of nestmates 89 90 void access_priv(TestReflection o) throws Throwable { 91 o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 92 } 93 void access_priv(StaticNested o) throws Throwable { 94 o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 95 } 96 void access_priv(StaticIface o) throws Throwable { 97 StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 98 } 99 } 100 101 class InnerNested { 102 103 // public constructor so we aren't relying on private access 104 public InnerNested() {} 105 106 void access_priv(TestReflection o) throws Throwable { 107 o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 108 } 109 void access_priv(StaticNested o) throws Throwable { 110 o.getClass().getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 111 } 112 void access_priv(StaticIface o) throws Throwable { 113 StaticIface.class.getDeclaredMethod("priv_static_invoke", new Class<?>[0]).invoke(null, new Object[0]); 114 } 115 } 116 117 public static void main(String[] args) throws Throwable { 118 TestReflection o = new TestReflection(); 119 StaticNested s = new StaticNested(); 120 InnerNested i = o.new InnerNested(); 121 StaticIface intf = new StaticIface() {}; 122 123 o.access_priv(new TestReflection()); 124 o.access_priv(s); 125 o.access_priv(intf); 126 127 s.access_priv(o); 128 s.access_priv(new StaticNested()); 129 s.access_priv(intf); 130 131 i.access_priv(o); 132 i.access_priv(s); 133 i.access_priv(intf); 134 135 intf.access_priv(o); 136 intf.access_priv(s); 137 intf.access_priv(new StaticIface(){}); 138 } 139 }