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 29 * @compile -XDdisablePrivateAccessors TestPrivateStaticField.java 30 * @run main TestPrivateStaticField 31 */ 32 33 public class TestPrivateStaticField { 34 35 // Private static field of nest-top for nestmates to access 36 private static int priv_field; 37 38 // public constructor so we aren't relying on private access 39 public TestPrivateStaticField() {} 40 41 // Methods that will access private static fields of nestmates 42 // We use the arguments for overloading purposes and use the fact 43 // you can access a static field through an object reference for 44 // convenience. 45 46 // NOTE: No InnerNested calls in this test because non-static nested types 47 // can't have static fields. Also no StaticIface calls as static interface 48 // fields must be public (and final) 49 50 void access_priv(TestPrivateStaticField o) { 51 priv_field = o.priv_field++; 52 } 53 void access_priv(StaticNested o) { 54 priv_field = o.priv_field++; 55 } 56 57 // The various nestmates 58 59 static interface StaticIface { 60 61 // Methods that will access private static fields of nestmates 62 63 default void access_priv(TestPrivateStaticField o) { 64 int priv_field = o.priv_field++; 65 } 66 default void access_priv(StaticNested o) { 67 int priv_field = o.priv_field++; 68 } 69 } 70 71 static class StaticNested { 72 73 private static int priv_field; 74 75 // public constructor so we aren't relying on private access 76 public StaticNested() {} 77 78 // Methods that will access private static fields of nestmates 79 80 void access_priv(TestPrivateStaticField o) { 81 priv_field = o.priv_field++; 82 } 83 void access_priv(StaticNested o) { 84 priv_field = o.priv_field++; 85 } 86 } 87 88 class InnerNested { 89 90 // public constructor so we aren't relying on private access 91 public InnerNested() {} 92 93 void access_priv(TestPrivateStaticField o) { 94 int priv_field = o.priv_field++; 95 } 96 void access_priv(StaticNested o) { 97 int priv_field = o.priv_field++; 98 } 99 } 100 101 public static void main(String[] args) { 102 TestPrivateStaticField o = new TestPrivateStaticField(); 103 StaticNested s = new StaticNested(); 104 InnerNested i = o.new InnerNested(); 105 StaticIface intf = new StaticIface() {}; 106 107 o.access_priv(new TestPrivateStaticField()); 108 o.access_priv(s); 109 110 s.access_priv(o); 111 s.access_priv(new StaticNested()); 112 113 i.access_priv(o); 114 i.access_priv(s); 115 116 intf.access_priv(o); 117 intf.access_priv(s); 118 } 119 }