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 8186961
27  * @run main/othervm StaticFieldsOnInterface C
28  * @run main/othervm StaticFieldsOnInterface D
29  * @run main/othervm StaticFieldsOnInterface Y
30  */
31 
32 public class StaticFieldsOnInterface {
33     /*
34             A
35            / \
36           B  C
37           \  /
38            D
39 
40         Interface A has a public field
41         Ensure B, C, D only report exactly one public field
42 
43           A
44          /
45         X A
46         |/
47         Y
48 
49         Ensure class Y, extending class X, reports exactly one public field
50      */
51 
52     public interface A {
53         public static final int CONSTANT = 42;
54     }
55 
56     public interface B extends A {
57     }
58 
59     public interface C extends A {
60     }
61 
62     public interface D extends B, C {
63     }
64 
65     static class X implements A {}
66     static class Y extends X implements A {}
67 
main(String[] args)68     public static void main(String[] args) throws Exception {
69         char first = 'C';
70         if (args.length > 0) {
71             first = args[0].charAt(0);
72         }
73 
74         assertOneField(A.class);
75         // D first
76         if (first == 'D') {
77             assertOneField(D.class);
78             assertOneField(C.class);
79         }
80         // C first
81         else if (first == 'C') {
82             assertOneField(C.class);
83             assertOneField(D.class);
84         }
85         else {
86             assertOneField(Y.class);
87         }
88     }
89 
assertOneField(Class<?> c)90     static void assertOneField(Class<?> c) {
91         int nfs = c.getFields().length;
92         if (nfs != 1) {
93             throw new AssertionError(String.format(
94                     "Class %s does not have exactly one field: %d", c.getName(), nfs));
95         }
96     }
97 }
98