1 /*
  2  * Copyright (c) 2021, Alibaba Group Holding Limited. All Rights Reserved.
  3  * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 /*
 27  * @test
 28  * @author Yi Yang
 29  * @summary Canonicalizes Foo.class.getModifiers() with interpreter mode
 30  * @modules java.base/jdk.internal.misc
 31  * @library /test/lib
 32  * @enablePreview
 33  * @run main/othervm -Xint
 34  *                   -XX:CompileCommand=compileonly,*CanonicalizeGetModifiers.test
 35  *                   compiler.c1.CanonicalizeGetModifiers
 36  */
 37 
 38 /*
 39  * @test
 40  * @author Yi Yang
 41  * @summary Canonicalizes Foo.class.getModifiers() with C1 mode
 42  * @modules java.base/jdk.internal.misc
 43  * @requires vm.compiler1.enabled
 44  * @library /test/lib
 45  * @enablePreview
 46  * @run main/othervm -XX:TieredStopAtLevel=1 -XX:+TieredCompilation
 47  *                   -XX:CompileCommand=compileonly,*CanonicalizeGetModifiers.test
 48  *                   compiler.c1.CanonicalizeGetModifiers
 49  */
 50 
 51 /*
 52  * @test
 53  * @author Yi Yang
 54  * @summary Canonicalizes Foo.class.getModifiers() with C2 mode
 55  * @modules java.base/jdk.internal.misc
 56  * @requires vm.compiler2.enabled
 57  * @library /test/lib
 58  * @enablePreview
 59  * @run main/othervm -XX:-TieredCompilation
 60  *                   -XX:CompileCommand=compileonly,*CanonicalizeGetModifiers.test
 61  *                   compiler.c1.CanonicalizeGetModifiers
 62  */
 63 
 64 package compiler.c1;
 65 
 66 import java.lang.classfile.ClassFile;
 67 import java.lang.reflect.Modifier;
 68 import java.lang.reflect.AccessFlag;
 69 
 70 import jdk.test.lib.Asserts;
 71 import jdk.internal.misc.PreviewFeatures;
 72 
 73 public class CanonicalizeGetModifiers {
 74     public static class T1 {
 75     }
 76 
 77     public static final class T2 {
 78     }
 79 
 80     private static class T3 {
 81     }
 82 
 83     protected static class T4 {
 84     }
 85 
 86     class T5 {
 87     }
 88 
 89     interface T6 {
 90     }
 91 
 92     static void test(Class poison) {
 93         Asserts.assertEQ(CanonicalizeGetModifiers.class.getModifiers(), Modifier.PUBLIC | ClassFile.ACC_IDENTITY);
 94         Asserts.assertEQ(T1.class.getModifiers(), Modifier.PUBLIC | Modifier.STATIC | ClassFile.ACC_IDENTITY);
 95         Asserts.assertEQ(T2.class.getModifiers(), Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC | ClassFile.ACC_IDENTITY);
 96         Asserts.assertEQ(T3.class.getModifiers(), Modifier.PRIVATE | Modifier.STATIC | ClassFile.ACC_IDENTITY);
 97         Asserts.assertEQ(T4.class.getModifiers(), Modifier.PROTECTED | Modifier.STATIC | ClassFile.ACC_IDENTITY);
 98         Asserts.assertEQ(new CanonicalizeGetModifiers().new T5().getClass().getModifiers(), 0/* NONE */ | ClassFile.ACC_IDENTITY);
 99         Asserts.assertEQ(T6.class.getModifiers(), Modifier.ABSTRACT | Modifier.STATIC | Modifier.INTERFACE);
100 
101         Asserts.assertEQ(int.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
102         Asserts.assertEQ(long.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
103         Asserts.assertEQ(double.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
104         Asserts.assertEQ(float.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
105         Asserts.assertEQ(char.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
106         Asserts.assertEQ(byte.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
107         Asserts.assertEQ(short.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
108         Asserts.assertEQ(void.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
109         Asserts.assertEQ(int[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0));
110         Asserts.assertEQ(long[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0));
111         Asserts.assertEQ(double[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0));
112         Asserts.assertEQ(float[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0));
113         Asserts.assertEQ(char[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0));
114         Asserts.assertEQ(byte[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0));
115         Asserts.assertEQ(short[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0));
116         Asserts.assertEQ(Object[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0));
117         Asserts.assertEQ(CanonicalizeGetModifiers[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? ClassFile.ACC_IDENTITY : 0));
118 
119         Asserts.assertEQ(new CanonicalizeGetModifiers().getClass().getModifiers(), Modifier.PUBLIC | ClassFile.ACC_IDENTITY);
120         Asserts.assertEQ(new T1().getClass().getModifiers(), Modifier.PUBLIC | Modifier.STATIC | ClassFile.ACC_IDENTITY);
121         Asserts.assertEQ(new T2().getClass().getModifiers(), Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC | ClassFile.ACC_IDENTITY);
122         Asserts.assertEQ(new T3().getClass().getModifiers(), Modifier.PRIVATE | Modifier.STATIC | ClassFile.ACC_IDENTITY);
123         Asserts.assertEQ(new T4().getClass().getModifiers(), Modifier.PROTECTED | Modifier.STATIC | ClassFile.ACC_IDENTITY);
124         try {
125             // null_check
126             poison.getModifiers();
127         } catch(NullPointerException npe) {
128             // got it!
129         }
130     }
131 
132     public static void main(String... args) {
133         for (int i = 0; i < 10_000; i++) {
134             test(i == 9999 ? null : CanonicalizeGetModifiers.class);
135         }
136     }
137 }