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