< prev index next >

test/jdk/java/lang/reflect/AccessFlag/ClassAccessFlagTest.java

Print this page

 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]")
 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             ExpectedClassFlags.class,
 58             TestOuterEnum.class
 59         };
 60         checkClasses(testClasses);
 61 
 62         // Nested classes of ClassAccessFlagTest
 63         checkClasses(ClassAccessFlagTest.class.getDeclaredClasses());
 64 
 65         checkPrimitives();
 66         checkArrays();
 67     }
 68 
 69     private static void checkClasses(Class<?>[] classes) {
 70         for (var clazz : classes) {
 71             checkClass(clazz);
 72         }
 73     }
 74 
 75     private static void checkClass(Class<?> clazz) {
 76         ExpectedClassFlags expected =

144                 accessLevel = AccessFlag.PRIVATE;
145             else
146                 accessLevel = null;
147 
148             var arrayClass = accessClass.arrayType();
149             // Access modifier must match on the array type
150             if (accessLevel != null) {
151                 if (!arrayClass.accessFlags().contains(accessLevel)) {
152                     throw new RuntimeException("Mismatched access flags on " +
153                                                arrayClass);
154                 }
155             } else {
156                 if (containsAny(arrayClass.accessFlags(),
157                                 Set.of(AccessFlag.PUBLIC,
158                                        AccessFlag.PROTECTED,
159                                        AccessFlag.PRIVATE))) {
160                     throw new RuntimeException("Unexpected access flags on " +
161                                                arrayClass);
162                 }
163             }












164         }
165 
166     }
167 
168     // inner classes and interfaces; possible flags on INNER_CLASS
169     // locations:
170     // PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, INTERFACE, ABSTRACT,
171     // SYNTHETIC, ANNOTATION, ENUM.

172 
173     @ExpectedClassFlags("[PUBLIC, STATIC, INTERFACE, ABSTRACT]")
174     public      interface PublicInterface {}





175     @ExpectedClassFlags("[PROTECTED, STATIC, INTERFACE, ABSTRACT]")
176     protected   interface ProtectedInterface {}





177     @ExpectedClassFlags("[PRIVATE, STATIC, INTERFACE, ABSTRACT]")
178     private     interface PrivateInterface {}





179     @ExpectedClassFlags("[STATIC, INTERFACE, ABSTRACT]")
180     /*package*/ interface PackageInterface {}




181 
182     @ExpectedClassFlags("[FINAL]")
183     /*package*/ final class TestFinalClass {}


184 
185     @ExpectedClassFlags("[ABSTRACT]")
186     /*package*/ abstract class TestAbstractClass {}


187 
188     @ExpectedClassFlags("[STATIC, INTERFACE, ABSTRACT, ANNOTATION]")
189     /*package*/ @interface TestMarkerAnnotation {}
190 
191     @ExpectedClassFlags("[PUBLIC, STATIC, FINAL, ENUM]")
192     public enum MetaSynVar {
193         QUUX;
194     }
195 
196     // Is there is at least one special enum constant, the enum class
197     // itself is implicitly abstract rather than final.
198     @ExpectedClassFlags("[PROTECTED, STATIC, ABSTRACT, ENUM]")
199     protected enum MetaSynVar2 {
200         WOMBAT{
201             @Override
202             public int foo() {return 42;}
203         };
204         public abstract int foo();
205     }
206 
207     @ExpectedClassFlags("[PRIVATE, ABSTRACT]")
208     private abstract class Foo {}


209 
210     @ExpectedClassFlags("[STATIC, INTERFACE, ABSTRACT]")
211     interface StaticTestInterface {}




212 }
213 
214 @Retention(RetentionPolicy.RUNTIME)
215 @ExpectedClassFlags("[INTERFACE, ABSTRACT, ANNOTATION]")
216 @interface ExpectedClassFlags {
217     String value();
218 }
219 
220 @ExpectedClassFlags("[INTERFACE, ABSTRACT]")
221 interface TestInterface {}




222 
223 
224 @ExpectedClassFlags("[FINAL, SUPER, ENUM]")
225 enum TestOuterEnum {
226     INSTANCE;
227 }

 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 =

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 }
< prev index next >