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