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 29 * @compile -XDdisablePrivateAccessors TestPrivateField.java 30 * @run main TestPrivateField 31 */ 32 33 public class TestPrivateField { 34 35 // Private field of nest-top for nestmates to access 36 private int priv_field; 37 38 // public constructor so we aren't relying on private access 39 public TestPrivateField() {} 40 41 // Methods that will access private fields of nestmates 42 43 // NOTE: No StaticIface variants as interfaces can't have instance fields 44 45 void access_priv(TestPrivateField o) { 46 this.priv_field = o.priv_field++; 47 } 48 void access_priv(InnerNested o) { 49 this.priv_field = o.priv_field++; 50 } 51 void access_priv(StaticNested o) { 52 this.priv_field = o.priv_field++; 53 } 54 55 // The various nestmates 56 57 static interface StaticIface { 58 59 // Methods that will access private fields of nestmates 60 61 default void access_priv(TestPrivateField o) { 62 int priv_field = o.priv_field++; 63 } 64 default void access_priv(InnerNested o) { 65 int priv_field = o.priv_field++; 66 } 67 default void access_priv(StaticNested o) { 68 int priv_field = o.priv_field++; 69 } 70 } 71 72 static class StaticNested { 73 74 private int priv_field; 75 76 // public constructor so we aren't relying on private access 77 public StaticNested() {} 78 79 // Methods that will access private fields of nestmates 80 81 void access_priv(TestPrivateField o) { 82 this.priv_field = o.priv_field++; 83 } 84 void access_priv(InnerNested o) { 85 this.priv_field = o.priv_field++; 86 } 87 void access_priv(StaticNested o) { 88 this.priv_field = o.priv_field++; 89 } 90 } 91 92 class InnerNested { 93 94 private int priv_field; 95 96 // public constructor so we aren't relying on private access 97 public InnerNested() {} 98 99 void access_priv(TestPrivateField o) { 100 this.priv_field = o.priv_field++; 101 } 102 void access_priv(InnerNested o) { 103 this.priv_field = o.priv_field++; 104 } 105 void access_priv(StaticNested o) { 106 this.priv_field = o.priv_field++; 107 } 108 } 109 110 public static void main(String[] args) { 111 TestPrivateField o = new TestPrivateField(); 112 StaticNested s = new StaticNested(); 113 InnerNested i = o.new InnerNested(); 114 StaticIface intf = new StaticIface() {}; 115 116 o.access_priv(new TestPrivateField()); 117 o.access_priv(i); 118 o.access_priv(s); 119 120 s.access_priv(o); 121 s.access_priv(i); 122 s.access_priv(new StaticNested()); 123 124 i.access_priv(o); 125 i.access_priv(o.new InnerNested()); 126 i.access_priv(s); 127 128 intf.access_priv(o); 129 intf.access_priv(i); 130 intf.access_priv(s); 131 } 132 }