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