1 /*
  2  * Copyright (c) 2021, Alibaba Group Holding Limited. 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  * @author Yi Yang
 28  * @summary Canonicalizes Foo.class.getModifiers() with interpreter mode
 29  * @modules java.base/jdk.internal.misc
 30  * @library /test/lib
 31  * @enablePreview
 32  * @run main/othervm -Xint
 33  *                   -XX:CompileCommand=compileonly,*CanonicalizeGetModifiers.test
 34  *                   compiler.c1.CanonicalizeGetModifiers
 35  */
 36 
 37 /*
 38  * @test
 39  * @author Yi Yang
 40  * @summary Canonicalizes Foo.class.getModifiers() with C1 mode
 41  * @modules java.base/jdk.internal.misc
 42  * @requires vm.compiler1.enabled
 43  * @library /test/lib
 44  * @enablePreview
 45  * @run main/othervm -XX:TieredStopAtLevel=1 -XX:+TieredCompilation
 46  *                   -XX:CompileCommand=compileonly,*CanonicalizeGetModifiers.test
 47  *                   compiler.c1.CanonicalizeGetModifiers
 48  */
 49 
 50 /*
 51  * @test
 52  * @author Yi Yang
 53  * @summary Canonicalizes Foo.class.getModifiers() with C2 mode
 54  * @modules java.base/jdk.internal.misc
 55  * @requires vm.compiler2.enabled
 56  * @library /test/lib
 57  * @enablePreview
 58  * @run main/othervm -XX:-TieredCompilation
 59  *                   -XX:CompileCommand=compileonly,*CanonicalizeGetModifiers.test
 60  *                   compiler.c1.CanonicalizeGetModifiers
 61  */
 62 
 63 package compiler.c1;
 64 
 65 import java.lang.reflect.Modifier;
 66 import java.lang.reflect.AccessFlag;
 67 
 68 import jdk.test.lib.Asserts;
 69 import jdk.internal.misc.PreviewFeatures;
 70 
 71 public class CanonicalizeGetModifiers {
 72     public static class T1 {
 73     }
 74 
 75     public static final class T2 {
 76     }
 77 
 78     private static class T3 {
 79     }
 80 
 81     protected static class T4 {
 82     }
 83 
 84     class T5 {
 85     }
 86 
 87     interface T6 {
 88     }
 89 
 90     static void test(Class poison) {
 91         Asserts.assertEQ(CanonicalizeGetModifiers.class.getModifiers(), Modifier.PUBLIC | Modifier.IDENTITY);
 92         Asserts.assertEQ(T1.class.getModifiers(), Modifier.PUBLIC | Modifier.STATIC | Modifier.IDENTITY);
 93         Asserts.assertEQ(T2.class.getModifiers(), Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC | Modifier.IDENTITY);
 94         Asserts.assertEQ(T3.class.getModifiers(), Modifier.PRIVATE | Modifier.STATIC | Modifier.IDENTITY);
 95         Asserts.assertEQ(T4.class.getModifiers(), Modifier.PROTECTED | Modifier.STATIC | Modifier.IDENTITY);
 96         Asserts.assertEQ(new CanonicalizeGetModifiers().new T5().getClass().getModifiers(), 0/* NONE */ | Modifier.IDENTITY);
 97         Asserts.assertEQ(T6.class.getModifiers(), Modifier.ABSTRACT | Modifier.STATIC | Modifier.INTERFACE);
 98 
 99         Asserts.assertEQ(int.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
100         Asserts.assertEQ(long.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
101         Asserts.assertEQ(double.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
102         Asserts.assertEQ(float.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
103         Asserts.assertEQ(char.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
104         Asserts.assertEQ(byte.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
105         Asserts.assertEQ(short.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
106         Asserts.assertEQ(void.class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL);
107         Asserts.assertEQ(int[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? Modifier.IDENTITY : 0));
108         Asserts.assertEQ(long[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? Modifier.IDENTITY : 0));
109         Asserts.assertEQ(double[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? Modifier.IDENTITY : 0));
110         Asserts.assertEQ(float[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? Modifier.IDENTITY : 0));
111         Asserts.assertEQ(char[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? Modifier.IDENTITY : 0));
112         Asserts.assertEQ(byte[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? Modifier.IDENTITY : 0));
113         Asserts.assertEQ(short[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? Modifier.IDENTITY : 0));
114         Asserts.assertEQ(Object[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? Modifier.IDENTITY : 0));
115         Asserts.assertEQ(CanonicalizeGetModifiers[].class.getModifiers(), Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | (PreviewFeatures.isEnabled() ? Modifier.IDENTITY : 0));
116 
117         Asserts.assertEQ(new CanonicalizeGetModifiers().getClass().getModifiers(), Modifier.PUBLIC | Modifier.IDENTITY);
118         Asserts.assertEQ(new T1().getClass().getModifiers(), Modifier.PUBLIC | Modifier.STATIC | Modifier.IDENTITY);
119         Asserts.assertEQ(new T2().getClass().getModifiers(), Modifier.PUBLIC | Modifier.FINAL | Modifier.STATIC | Modifier.IDENTITY);
120         Asserts.assertEQ(new T3().getClass().getModifiers(), Modifier.PRIVATE | Modifier.STATIC | Modifier.IDENTITY);
121         Asserts.assertEQ(new T4().getClass().getModifiers(), Modifier.PROTECTED | Modifier.STATIC | Modifier.IDENTITY);
122         try {
123             // null_check
124             poison.getModifiers();
125         } catch(NullPointerException npe) {
126             // got it!
127         }
128     }
129 
130     public static void main(String... args) {
131         for (int i = 0; i < 10_000; i++) {
132             test(i == 9999 ? null : CanonicalizeGetModifiers.class);
133         }
134     }
135 }