1 /*
2  * Copyright (c) 2016, Oracle and/or its affiliates. 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  * @test
26  * @bug 8150669
27  * @bug 8233019
28  * @summary C1 intrinsic for Class.isPrimitive
29  * @modules java.base/jdk.internal.misc
30  *
31  * @run main/othervm -ea -Diters=200   -Xint
32  *      compiler.intrinsics.klass.TestIsPrimitive
33  * @run main/othervm -ea -XX:-UseSharedSpaces -Diters=30000 -XX:TieredStopAtLevel=1
34  *      compiler.intrinsics.klass.TestIsPrimitive
35  * @run main/othervm -ea -Diters=30000 -XX:TieredStopAtLevel=4
36  *      compiler.intrinsics.klass.TestIsPrimitive
37  */
38 
39 package compiler.intrinsics.klass;
40 
41 import java.util.concurrent.Callable;
42 
43 public class TestIsPrimitive {
44     static final int ITERS = Integer.getInteger("iters", 1);
45 
main(String... args)46     public static void main(String... args) throws Exception {
47         testOK(true,  InlineConstants::testBoolean);
48         testOK(true,  InlineConstants::testByte);
49         testOK(true,  InlineConstants::testShort);
50         testOK(true,  InlineConstants::testChar);
51         testOK(true,  InlineConstants::testInt);
52         testOK(true,  InlineConstants::testFloat);
53         testOK(true,  InlineConstants::testLong);
54         testOK(true,  InlineConstants::testDouble);
55         testOK(false, InlineConstants::testObject);
56         testOK(false, InlineConstants::testArray);
57         testOK(false, InlineConstants::testBooleanArray);
58 
59         testOK(true,  StaticConstants::testBoolean);
60         testOK(true,  StaticConstants::testByte);
61         testOK(true,  StaticConstants::testShort);
62         testOK(true,  StaticConstants::testChar);
63         testOK(true,  StaticConstants::testInt);
64         testOK(true,  StaticConstants::testFloat);
65         testOK(true,  StaticConstants::testLong);
66         testOK(true,  StaticConstants::testDouble);
67         testOK(false, StaticConstants::testObject);
68         testOK(false, StaticConstants::testArray);
69         testOK(false, StaticConstants::testBooleanArray);
70         testNPE(      StaticConstants::testNull);
71 
72         testOK(true,  NoConstants::testBoolean);
73         testOK(true,  NoConstants::testByte);
74         testOK(true,  NoConstants::testShort);
75         testOK(true,  NoConstants::testChar);
76         testOK(true,  NoConstants::testInt);
77         testOK(true,  NoConstants::testFloat);
78         testOK(true,  NoConstants::testLong);
79         testOK(true,  NoConstants::testDouble);
80         testOK(false, NoConstants::testObject);
81         testOK(false, NoConstants::testArray);
82         testOK(false, NoConstants::testBooleanArray);
83         testNPE(      NoConstants::testNull);
84     }
85 
testOK(boolean expected, Callable<Object> test)86     public static void testOK(boolean expected, Callable<Object> test) throws Exception {
87         for (int c = 0; c < ITERS; c++) {
88             Object res = test.call();
89             if (!res.equals(expected)) {
90                 throw new IllegalStateException("Wrong result: expected = " + expected + ", but got " + res);
91             }
92         }
93     }
94 
95     static volatile Object sink;
96 
testNPE(Callable<Object> test)97     public static void testNPE(Callable<Object> test) throws Exception {
98         for (int c = 0; c < ITERS; c++) {
99             try {
100                sink = test.call();
101                throw new IllegalStateException("Expected NPE");
102             } catch (NullPointerException iae) {
103                // expected
104             }
105         }
106     }
107 
108     static volatile Class<?> classBoolean = boolean.class;
109     static volatile Class<?> classByte    = byte.class;
110     static volatile Class<?> classShort   = short.class;
111     static volatile Class<?> classChar    = char.class;
112     static volatile Class<?> classInt     = int.class;
113     static volatile Class<?> classFloat   = float.class;
114     static volatile Class<?> classLong    = long.class;
115     static volatile Class<?> classDouble  = double.class;
116     static volatile Class<?> classObject  = Object.class;
117     static volatile Class<?> classArray   = Object[].class;
118     static volatile Class<?> classNull    = null;
119     static volatile Class<?> classBooleanArray = boolean[].class;
120 
121     static final Class<?> staticClassBoolean = boolean.class;
122     static final Class<?> staticClassByte    = byte.class;
123     static final Class<?> staticClassShort   = short.class;
124     static final Class<?> staticClassChar    = char.class;
125     static final Class<?> staticClassInt     = int.class;
126     static final Class<?> staticClassFloat   = float.class;
127     static final Class<?> staticClassLong    = long.class;
128     static final Class<?> staticClassDouble  = double.class;
129     static final Class<?> staticClassObject  = Object.class;
130     static final Class<?> staticClassArray   = Object[].class;
131     static final Class<?> staticClassNull    = null;
132     static final Class<?> staticClassBooleanArray = boolean[].class;
133 
134     static class InlineConstants {
testBoolean()135         static boolean testBoolean() { return boolean.class.isPrimitive();  }
testByte()136         static boolean testByte()    { return byte.class.isPrimitive();     }
testShort()137         static boolean testShort()   { return short.class.isPrimitive();    }
testChar()138         static boolean testChar()    { return char.class.isPrimitive();     }
testInt()139         static boolean testInt()     { return int.class.isPrimitive();      }
testFloat()140         static boolean testFloat()   { return float.class.isPrimitive();    }
testLong()141         static boolean testLong()    { return long.class.isPrimitive();     }
testDouble()142         static boolean testDouble()  { return double.class.isPrimitive();   }
testObject()143         static boolean testObject()  { return Object.class.isPrimitive();   }
testArray()144         static boolean testArray()   { return Object[].class.isPrimitive(); }
testBooleanArray()145         static boolean testBooleanArray() { return boolean[].class.isPrimitive(); }
146     }
147 
148     static class StaticConstants {
testBoolean()149         static boolean testBoolean() { return staticClassBoolean.isPrimitive(); }
testByte()150         static boolean testByte()    { return staticClassByte.isPrimitive();    }
testShort()151         static boolean testShort()   { return staticClassShort.isPrimitive();   }
testChar()152         static boolean testChar()    { return staticClassChar.isPrimitive();    }
testInt()153         static boolean testInt()     { return staticClassInt.isPrimitive();     }
testFloat()154         static boolean testFloat()   { return staticClassFloat.isPrimitive();   }
testLong()155         static boolean testLong()    { return staticClassLong.isPrimitive();    }
testDouble()156         static boolean testDouble()  { return staticClassDouble.isPrimitive();  }
testObject()157         static boolean testObject()  { return staticClassObject.isPrimitive();  }
testArray()158         static boolean testArray()   { return staticClassArray.isPrimitive();   }
testNull()159         static boolean testNull()    { return staticClassNull.isPrimitive();    }
testBooleanArray()160         static boolean testBooleanArray() { return staticClassBooleanArray.isPrimitive(); }
161     }
162 
163     static class NoConstants {
testBoolean()164         static boolean testBoolean() { return classBoolean.isPrimitive(); }
testByte()165         static boolean testByte()    { return classByte.isPrimitive();    }
testShort()166         static boolean testShort()   { return classShort.isPrimitive();   }
testChar()167         static boolean testChar()    { return classChar.isPrimitive();    }
testInt()168         static boolean testInt()     { return classInt.isPrimitive();     }
testFloat()169         static boolean testFloat()   { return classFloat.isPrimitive();   }
testLong()170         static boolean testLong()    { return classLong.isPrimitive();    }
testDouble()171         static boolean testDouble()  { return classDouble.isPrimitive();  }
testObject()172         static boolean testObject()  { return classObject.isPrimitive();  }
testArray()173         static boolean testArray()   { return classArray.isPrimitive();   }
testNull()174         static boolean testNull()    { return classNull.isPrimitive();    }
testBooleanArray()175         static boolean testBooleanArray() { return classBooleanArray.isPrimitive();    }
176     }
177 
178 
179 }
180 
181