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 fields 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 import java.lang.reflect.Field; 34 35 public class TestReflection { 36 37 // Private field of nest-top for nestmates to access 38 private int priv_field; 39 40 // public constructor so we aren't relying on private access 41 public TestReflection() {} 42 43 // Methods that will access private fields of nestmates 44 45 // NOTE: No StaticIface variants as interfaces can't have instance fields 46 47 void access_priv(TestReflection o) throws Throwable { 48 Field f = o.getClass().getDeclaredField("priv_field"); 49 this.priv_field = f.getInt(o); 50 f.setInt(o, 2); 51 } 52 void access_priv(InnerNested o) throws Throwable { 53 Field f = o.getClass().getDeclaredField("priv_field"); 54 this.priv_field = f.getInt(o); 55 f.setInt(o, 2); 56 } 57 void access_priv(StaticNested o) throws Throwable { 58 Field f = o.getClass().getDeclaredField("priv_field"); 59 this.priv_field = f.getInt(o); 60 f.setInt(o, 2); 61 } 62 63 // The various nestmates 64 65 static interface StaticIface { 66 67 // Methods that will access private fields of nestmates 68 69 default void access_priv(TestReflection o) throws Throwable { 70 Field f = o.getClass().getDeclaredField("priv_field"); 71 int priv_field = f.getInt(o); 72 f.setInt(o, 2); 73 } 74 default void access_priv(InnerNested o) throws Throwable { 75 Field f = o.getClass().getDeclaredField("priv_field"); 76 int priv_field = f.getInt(o); 77 f.setInt(o, 2); 78 } 79 default void access_priv(StaticNested o) throws Throwable { 80 Field f = o.getClass().getDeclaredField("priv_field"); 81 int priv_field = f.getInt(o); 82 f.setInt(o, 2); 83 } 84 } 85 86 static class StaticNested { 87 88 private int priv_field; 89 90 // public constructor so we aren't relying on private access 91 public StaticNested() {} 92 93 // Methods that will access private fields of nestmates 94 95 void access_priv(TestReflection o) throws Throwable { 96 Field f = o.getClass().getDeclaredField("priv_field"); 97 this.priv_field = f.getInt(o); 98 f.setInt(o, 2); 99 } 100 void access_priv(InnerNested o) throws Throwable { 101 Field f = o.getClass().getDeclaredField("priv_field"); 102 this.priv_field = f.getInt(o); 103 f.setInt(o, 2); 104 } 105 void access_priv(StaticNested o) throws Throwable { 106 Field f = o.getClass().getDeclaredField("priv_field"); 107 this.priv_field = f.getInt(o); 108 f.setInt(o, 2); 109 } 110 } 111 112 class InnerNested { 113 114 private int priv_field; 115 116 // public constructor so we aren't relying on private access 117 public InnerNested() {} 118 119 void access_priv(TestReflection o) throws Throwable { 120 Field f = o.getClass().getDeclaredField("priv_field"); 121 this.priv_field = f.getInt(o); 122 f.setInt(o, 2); 123 } 124 void access_priv(InnerNested o) throws Throwable { 125 Field f = o.getClass().getDeclaredField("priv_field"); 126 this.priv_field = f.getInt(o); 127 f.setInt(o, 2); 128 } 129 void access_priv(StaticNested o) throws Throwable { 130 Field f = o.getClass().getDeclaredField("priv_field"); 131 this.priv_field = f.getInt(o); 132 f.setInt(o, 2); 133 } 134 } 135 136 public static void main(String[] args) throws Throwable { 137 TestReflection o = new TestReflection(); 138 StaticNested s = new StaticNested(); 139 InnerNested i = o.new InnerNested(); 140 StaticIface intf = new StaticIface() {}; 141 142 o.access_priv(new TestReflection()); 143 o.access_priv(i); 144 o.access_priv(s); 145 146 s.access_priv(o); 147 s.access_priv(i); 148 s.access_priv(new StaticNested()); 149 150 i.access_priv(o); 151 i.access_priv(o.new InnerNested()); 152 i.access_priv(s); 153 154 intf.access_priv(o); 155 intf.access_priv(i); 156 intf.access_priv(s); 157 } 158 }