1 /*
  2  * Copyright (c) 2022, 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 8266670 8291734
 27  * @summary Test expected AccessFlag's on classes.
 28  */
 29 
 30 import java.lang.annotation.*;
 31 import java.lang.reflect.*;
 32 import java.util.*;
 33 
 34 /*
 35  * Class access flags that can directly or indirectly declared in
 36  * source include:
 37  * public, private, protected, static, final, interface, abstract,
 38  * annotation, enum.
 39  *
 40  * Additionally, the access flags super and synthetic cannot be
 41  * explicitly applied.
 42  *
 43  * This test is written on top of the facilities of core reflection.
 44  *
 45  * Note that core reflection does not offer a supported mechanism to
 46  * return the Class object created from a module-info.class
 47  * file. Therefore, this test does not attempt to probe the setting of
 48  * that access flag.
 49  */
 50 @ExpectedClassFlags("[PUBLIC, FINAL, SUPER, IDENTITY]")
 51 public final class ClassAccessFlagTest {
 52     public static void main(String... args) {
 53         // Top-level and auxiliary classes; i.e. non-inner classes
 54         Class<?>[] testClasses = {
 55             ClassAccessFlagTest.class,
 56             TestInterface.class,
 57             TestIdentityInterface.class,
 58             TestValueInterface.class,
 59             ExpectedClassFlags.class,
 60             TestOuterEnum.class
 61         };
 62         checkClasses(testClasses);
 63 
 64         // Nested classes of ClassAccessFlagTest
 65         checkClasses(ClassAccessFlagTest.class.getDeclaredClasses());
 66 
 67         checkPrimitives();
 68         checkArrays();
 69     }
 70 
 71     private static void checkClasses(Class<?>[] classes) {
 72         for (var clazz : classes) {
 73             checkClass(clazz);
 74         }
 75     }
 76 
 77     private static void checkClass(Class<?> clazz) {
 78         ExpectedClassFlags expected =
 79             clazz.getAnnotation(ExpectedClassFlags.class);
 80         if (expected != null) {
 81             String actual = clazz.accessFlags().toString();
 82             if (!expected.value().equals(actual)) {
 83                 throw new RuntimeException("On " + clazz +
 84                                            " expected " + expected.value() +
 85                                            " got " + actual);
 86             }
 87         }
 88     }
 89 
 90     private static void checkPrimitives() {
 91         final Class<?>[] primitives = {
 92             byte.class,
 93             int.class,
 94             long.class,
 95             short.class,
 96             char.class,
 97             float.class,
 98             double.class,
 99             boolean.class,
100             void.class // same access flag rules
101         };
102 
103         var mustBePresent = Set.of(AccessFlag.PUBLIC, AccessFlag.FINAL);
104         var mustBeAbsent = Set.of(AccessFlag.PRIVATE,
105                                   AccessFlag.PROTECTED,
106                                   AccessFlag.INTERFACE);
107 
108         for(var primClass : primitives) {
109             // PUBLIC must be present, PROTECTED and PRIVATE must be
110             // absent.
111             // FINAL must be present, INTERFACE must be absent.
112             var accessFlags = primClass.accessFlags();
113             if (!accessFlags.containsAll(mustBePresent)) {
114                 throw new RuntimeException("Missing mandatory flags on " +
115                                            primClass);
116             }
117 
118             if (containsAny(accessFlags, mustBeAbsent)) {
119                 throw new RuntimeException("Unexpected flags present on " +
120                                            primClass);
121             }
122         }
123     }
124 
125     private static boolean containsAny(Set<AccessFlag> input,
126                                        Set<AccessFlag> test) {
127         var copy = new HashSet<>(input);
128         return copy.removeAll(test);
129     }
130 
131     private static void checkArrays() {
132         Class<?>[] accessClasses = {
133             PublicInterface.class,
134             ProtectedInterface.class,
135             PrivateInterface.class,
136         };
137 
138         for (var accessClass : accessClasses) {
139             AccessFlag accessLevel;
140             var flags = accessClass.accessFlags();
141             if (flags.contains(AccessFlag.PUBLIC))
142                 accessLevel = AccessFlag.PUBLIC;
143             else if (flags.contains(AccessFlag.PROTECTED))
144                 accessLevel = AccessFlag.PROTECTED;
145             else if (flags.contains(AccessFlag.PRIVATE))
146                 accessLevel = AccessFlag.PRIVATE;
147             else
148                 accessLevel = null;
149 
150             var arrayClass = accessClass.arrayType();
151             // Access modifier must match on the array type
152             if (accessLevel != null) {
153                 if (!arrayClass.accessFlags().contains(accessLevel)) {
154                     throw new RuntimeException("Mismatched access flags on " +
155                                                arrayClass);
156                 }
157             } else {
158                 if (containsAny(arrayClass.accessFlags(),
159                                 Set.of(AccessFlag.PUBLIC,
160                                        AccessFlag.PROTECTED,
161                                        AccessFlag.PRIVATE))) {
162                     throw new RuntimeException("Unexpected access flags on " +
163                                                arrayClass);
164                 }
165             }
166             // Verify IDENTITY, ABSTRACT, FINAL, and access mode
167             Set<AccessFlag> expected = new HashSet<>(4);
168             expected.add(AccessFlag.ABSTRACT);
169             expected.add(AccessFlag.FINAL);
170 //            expected.add(AccessFlag.IDENTITY);  // NYI Pending: JDK-8294866
171             if (accessLevel != null)
172                 expected.add(accessLevel);
173             if (!expected.equals(arrayClass.accessFlags())) {
174                 throw new RuntimeException("Unexpected access flags for array: " + accessClass +
175                         ": actual: " + arrayClass.accessFlags() +
176                         ", expected: " + expected);
177             }
178         }
179 
180     }
181 
182     // inner classes and interfaces; possible flags on INNER_CLASS
183     // locations:
184     // PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, INTERFACE, ABSTRACT,
185     // SYNTHETIC, ANNOTATION, ENUM.
186     // Include cases for classes with identity, value modifier, or no modifier.
187 
188     @ExpectedClassFlags("[PUBLIC, STATIC, INTERFACE, ABSTRACT]")
189     public      interface PublicInterface {}
190     @ExpectedClassFlags("[PUBLIC, STATIC, IDENTITY, INTERFACE, ABSTRACT]")
191     public      identity interface PublicIdentityInterface {}
192     @ExpectedClassFlags("[PUBLIC, STATIC, VALUE, INTERFACE, ABSTRACT]")
193     public      value interface PublicValueInterface {}
194 
195     @ExpectedClassFlags("[PROTECTED, STATIC, INTERFACE, ABSTRACT]")
196     protected   interface ProtectedInterface {}
197     @ExpectedClassFlags("[PROTECTED, STATIC, IDENTITY, INTERFACE, ABSTRACT]")
198     protected   identity interface ProtectedIdentityInterface {}
199     @ExpectedClassFlags("[PROTECTED, STATIC, VALUE, INTERFACE, ABSTRACT]")
200     protected   value interface ProtectedValueInterface {}
201 
202     @ExpectedClassFlags("[PRIVATE, STATIC, INTERFACE, ABSTRACT]")
203     private     interface PrivateInterface {}
204     @ExpectedClassFlags("[PRIVATE, STATIC, IDENTITY, INTERFACE, ABSTRACT]")
205     private     identity interface PrivateIdentityInterface {}
206     @ExpectedClassFlags("[PRIVATE, STATIC, VALUE, INTERFACE, ABSTRACT]")
207     private     value interface PrivateValueInterface {}
208 
209     @ExpectedClassFlags("[STATIC, INTERFACE, ABSTRACT]")
210     /*package*/ interface PackageInterface {}
211     @ExpectedClassFlags("[STATIC, IDENTITY, INTERFACE, ABSTRACT]")
212     /*package*/ identity interface PackageIdentityInterface {}
213     @ExpectedClassFlags("[STATIC, VALUE, INTERFACE, ABSTRACT]")
214     /*package*/ value interface PackageValueInterface {}
215 
216     @ExpectedClassFlags("[FINAL, IDENTITY]")
217     /*package*/ final class TestFinalClass {}
218     @ExpectedClassFlags("[FINAL, IDENTITY]")
219     /*package*/ final identity class TestFinalIdentityClass {}
220 
221     @ExpectedClassFlags("[IDENTITY, ABSTRACT]")
222     /*package*/ abstract class TestAbstractClass {}
223     @ExpectedClassFlags("[IDENTITY, ABSTRACT]")
224     /*package*/ abstract identity class TestAbstractIdentityClass {}
225 
226     @ExpectedClassFlags("[STATIC, INTERFACE, ABSTRACT, ANNOTATION]")
227     /*package*/ @interface TestMarkerAnnotation {}
228 
229     @ExpectedClassFlags("[PUBLIC, STATIC, FINAL, IDENTITY, ENUM]")
230     public enum MetaSynVar {
231         QUUX;
232     }
233 
234     // Is there is at least one special enum constant, the enum class
235     // itself is implicitly abstract rather than final.
236     @ExpectedClassFlags("[PROTECTED, STATIC, IDENTITY, ABSTRACT, ENUM]")
237     protected enum MetaSynVar2 {
238         WOMBAT{
239             @Override
240             public int foo() {return 42;}
241         };
242         public abstract int foo();
243     }
244 
245     @ExpectedClassFlags("[PRIVATE, IDENTITY, ABSTRACT]")
246     private abstract class Foo {}
247     @ExpectedClassFlags("[PRIVATE, IDENTITY, ABSTRACT]")
248     private abstract identity class IdentityFoo {}
249 
250     @ExpectedClassFlags("[STATIC, INTERFACE, ABSTRACT]")
251     interface StaticTestInterface {}
252     @ExpectedClassFlags("[STATIC, IDENTITY, INTERFACE, ABSTRACT]")
253     identity interface StaticTestIdentityInterface {}
254     @ExpectedClassFlags("[STATIC, VALUE, INTERFACE, ABSTRACT]")
255     value interface StaticTestValueInterface {}
256 }
257 
258 @Retention(RetentionPolicy.RUNTIME)
259 @ExpectedClassFlags("[INTERFACE, ABSTRACT, ANNOTATION]")
260 @interface ExpectedClassFlags {
261     String value();
262 }
263 
264 @ExpectedClassFlags("[INTERFACE, ABSTRACT]")
265 interface TestInterface {}
266 @ExpectedClassFlags("[SUPER, IDENTITY, INTERFACE, ABSTRACT]")
267 identity interface TestIdentityInterface {}
268 @ExpectedClassFlags("[VALUE, INTERFACE, ABSTRACT]")
269 value interface TestValueInterface {}
270 
271 
272 @ExpectedClassFlags("[FINAL, SUPER, IDENTITY, ENUM]")
273 enum TestOuterEnum {
274     INSTANCE;
275 }