1 /*
  2  * Copyright (c) 2022, Red Hat, Inc. 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 /*
 26  * @test
 27  * @library /test/lib
 28  * @enablePreview
 29  * @run main/othervm -Xint
 30  *                   -XX:CompileCommand=dontinline,*TestGetModifiers.test
 31  *                   compiler.intrinsics.klass.TestGetModifiers
 32  */
 33 
 34 /*
 35  * @test
 36  * @requires vm.compiler1.enabled
 37  * @library /test/lib
 38  * @enablePreview
 39  * @run main/othervm -XX:TieredStopAtLevel=1 -XX:+TieredCompilation
 40  *                   -XX:CompileCommand=dontinline,*TestGetModifiers.test
 41  *                   compiler.intrinsics.klass.TestGetModifiers
 42  */
 43 
 44 /*
 45  * @test
 46  * @requires vm.compiler2.enabled
 47  * @library /test/lib
 48  * @enablePreview
 49  * @run main/othervm -XX:-TieredCompilation
 50  *                   -XX:CompileCommand=dontinline,*TestGetModifiers.test
 51  *                   compiler.intrinsics.klass.TestGetModifiers
 52  */
 53 
 54 package compiler.intrinsics.klass;
 55 
 56 import java.lang.reflect.Modifier;
 57 import static java.lang.reflect.Modifier.*;
 58 
 59 import jdk.test.lib.Asserts;
 60 
 61 public class TestGetModifiers {
 62     public static class T1 {
 63     }
 64 
 65     public static final class T2 {
 66     }
 67 
 68     private static class T3 {
 69     }
 70 
 71     protected static class T4 {
 72     }
 73 
 74     class T5 {
 75     }
 76 
 77     interface T6 {
 78     }
 79 
 80     static void test(Class cl, int expectedMods) {
 81         for (int i = 0; i < 100_000; i++) {
 82             int actualMods = cl.getModifiers();
 83             if (actualMods != expectedMods) {
 84                 throw new IllegalStateException("Error with: " + cl);
 85             }
 86         }
 87     }
 88 
 89     public static void main(String... args) {
 90         test(T1.class,                                      PUBLIC | STATIC | IDENTITY);
 91         test(T2.class,                                      PUBLIC | FINAL | STATIC | IDENTITY);
 92         test(T3.class,                                      PRIVATE | STATIC | IDENTITY);
 93         test(T4.class,                                      PROTECTED | STATIC | IDENTITY);
 94         test(new TestGetModifiers().new T5().getClass(),    IDENTITY);
 95         test(T6.class,                                      ABSTRACT | STATIC | INTERFACE);
 96 
 97         test(int.class,                                     PUBLIC | ABSTRACT | FINAL);
 98         test(long.class,                                    PUBLIC | ABSTRACT | FINAL);
 99         test(double.class,                                  PUBLIC | ABSTRACT | FINAL);
100         test(float.class,                                   PUBLIC | ABSTRACT | FINAL);
101         test(char.class,                                    PUBLIC | ABSTRACT | FINAL);
102         test(byte.class,                                    PUBLIC | ABSTRACT | FINAL);
103         test(short.class,                                   PUBLIC | ABSTRACT | FINAL);
104         test(void.class,                                    PUBLIC | ABSTRACT | FINAL);
105         test(int[].class,                                   PUBLIC | ABSTRACT | FINAL);
106         test(long[].class,                                  PUBLIC | ABSTRACT | FINAL);
107         test(double[].class,                                PUBLIC | ABSTRACT | FINAL);
108         test(float[].class,                                 PUBLIC | ABSTRACT | FINAL);
109         test(char[].class,                                  PUBLIC | ABSTRACT | FINAL);
110         test(byte[].class,                                  PUBLIC | ABSTRACT | FINAL);
111         test(short[].class,                                 PUBLIC | ABSTRACT | FINAL);
112         test(Object[].class,                                PUBLIC | ABSTRACT | FINAL);
113         test(TestGetModifiers[].class,                      PUBLIC | ABSTRACT | FINAL);
114 
115         test(new TestGetModifiers().getClass(),             PUBLIC | IDENTITY);
116         test(new T1().getClass(),                           PUBLIC | STATIC | IDENTITY);
117         test(new T2().getClass(),                           PUBLIC | FINAL | STATIC | IDENTITY);
118         test(new T3().getClass(),                           PRIVATE | STATIC | IDENTITY);
119         test(new T4().getClass(),                           PROTECTED | STATIC | IDENTITY);
120     }
121 }