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