< prev index next >

test/jdk/java/lang/Class/getModifiers/TestPrimitiveAndArrayModifiers.java

Print this page

  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 import java.lang.reflect.Modifier;
 25 import java.lang.annotation.*;
 26 
 27 /*
 28  * @test
 29  * @bug 8296743

 30  * @summary Verify array classes and primitives have expected modifiers


 31  */
 32 @ExpectedModifiers(Modifier.PUBLIC | Modifier.FINAL | Modifier.ABSTRACT)
 33 public class TestPrimitiveAndArrayModifiers {
 34 
 35     /*
 36      * Relevant excerpt of the Class.getModifiers() specification:
 37      * <p> If the underlying class is an array class:
 38      * <ul>
 39      * <li> its {@code public}, {@code private} and {@code protected}
 40      *      modifiers are the same as those of its component type
 41      * <li> its {@code final} and {@code abstract} modifiers are always
 42      *      {@code true}
 43      * <li> its interface modifier is always {@code false}, even when
 44      *      the component type is an interface
 45      * </ul>
 46      */
 47 
 48     public static void main(String... args) throws Exception {
 49         testPrimitives();
 50         testArrays();
 51     }
 52 
 53     private static void testArrays() {
 54         Class<?>[] testCases = {
 55             TestPrimitiveAndArrayModifiers.class,
 56 
 57             PackagePrivateClass.class,
 58             ProtectedClass.class,
 59             PrivateClass.class,
 60 
 61             PublicInterface.class,
 62             PackagePrivateInterface.class,
 63             ProtectedInterface.class,
 64             PrivateInterface.class,
 65         };
 66 
 67         for(var testCase : testCases) {
 68             int expectedModifiers =
 69                 testCase.getAnnotation(ExpectedModifiers.class).value();




 70             Class<?> arrayClass = testCase.arrayType();
 71             int actualModifiers = arrayClass.getModifiers();
 72             if (expectedModifiers != actualModifiers) {
 73                 throw new RuntimeException("Expected " + Modifier.toString(expectedModifiers) +
 74                                            "on " + testCase.getCanonicalName() +
 75                                            ", but got " + Modifier.toString(actualModifiers));
 76             }
 77         }
 78     }
 79 
 80     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT)
 81     class PackagePrivateClass {}
 82 
 83     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PROTECTED)
 84     protected class ProtectedClass {}
 85 
 86     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PRIVATE)
 87     private class  PrivateClass {}
 88 
 89     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PUBLIC)
 90     public interface PublicInterface {}
 91 
 92     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT)
 93     interface PackagePrivateInterface {}
 94 

  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 import jdk.internal.misc.PreviewFeatures;
 25 
 26 import java.lang.reflect.Modifier;
 27 import java.lang.annotation.*;
 28 
 29 /*
 30  * @test
 31  * @bug 8296743
 32  * @modules java.base/jdk.internal.misc
 33  * @summary Verify array classes and primitives have expected modifiers
 34  * @run main/othervm TestPrimitiveAndArrayModifiers
 35  * @run main/othervm --enable-preview TestPrimitiveAndArrayModifiers
 36  */
 37 @ExpectedModifiers(Modifier.PUBLIC | Modifier.FINAL | Modifier.ABSTRACT)
 38 public class TestPrimitiveAndArrayModifiers {
 39 
 40     /*
 41      * Relevant excerpt of the Class.getModifiers() specification:
 42      * <p> If the underlying class is an array class:
 43      * <ul>
 44      * <li> its {@code public}, {@code private} and {@code protected}
 45      *      modifiers are the same as those of its component type
 46      * <li> its {@code final} and {@code abstract} modifiers are always
 47      *      {@code true}
 48      * <li> its interface modifier is always {@code false}, even when
 49      *      the component type is an interface
 50      * </ul>
 51      */
 52 
 53     public static void main(String... args) throws Exception {
 54         testPrimitives();
 55         testArrays();
 56     }
 57 
 58     private static void testArrays() {
 59         Class<?>[] testCases = {
 60             TestPrimitiveAndArrayModifiers.class,
 61 
 62             PackagePrivateClass.class,
 63             ProtectedClass.class,
 64             PrivateClass.class,
 65 
 66             PublicInterface.class,
 67             PackagePrivateInterface.class,
 68             ProtectedInterface.class,
 69             PrivateInterface.class,
 70         };
 71 
 72         for(var testCase : testCases) {
 73             int expectedModifiers =
 74                 testCase.getAnnotation(ExpectedModifiers.class).value();
 75             if (PreviewFeatures.isEnabled()) {
 76                 // All arrays under preview also have IDENTITY
 77                 expectedModifiers |= Modifier.IDENTITY;
 78             }
 79             Class<?> arrayClass = testCase.arrayType();
 80             int actualModifiers = arrayClass.getModifiers();
 81             if (expectedModifiers != actualModifiers) {
 82                 throw new RuntimeException("Expected " + Modifier.toString(expectedModifiers) +
 83                                            " on " + testCase.getCanonicalName() +
 84                                            ", but got " + Modifier.toString(actualModifiers));
 85             }
 86         }
 87     }
 88 
 89     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT)
 90     class PackagePrivateClass {}
 91 
 92     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PROTECTED)
 93     protected class ProtectedClass {}
 94 
 95     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PRIVATE)
 96     private class  PrivateClass {}
 97 
 98     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PUBLIC)
 99     public interface PublicInterface {}
100 
101     @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT)
102     interface PackagePrivateInterface {}
103 
< prev index next >