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 /*
  26  * @test
  27  * @bug 8150669
  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 -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 
  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 
  58         testOK(true,  StaticConstants::testBoolean);
  59         testOK(true,  StaticConstants::testByte);
  60         testOK(true,  StaticConstants::testShort);
  61         testOK(true,  StaticConstants::testChar);
  62         testOK(true,  StaticConstants::testInt);
  63         testOK(true,  StaticConstants::testFloat);
  64         testOK(true,  StaticConstants::testLong);
  65         testOK(true,  StaticConstants::testDouble);
  66         testOK(false, StaticConstants::testObject);
  67         testOK(false, StaticConstants::testArray);
  68         testNPE(      StaticConstants::testNull);
  69 
  70         testOK(true,  NoConstants::testBoolean);
  71         testOK(true,  NoConstants::testByte);
  72         testOK(true,  NoConstants::testShort);
  73         testOK(true,  NoConstants::testChar);
  74         testOK(true,  NoConstants::testInt);
  75         testOK(true,  NoConstants::testFloat);
  76         testOK(true,  NoConstants::testLong);
  77         testOK(true,  NoConstants::testDouble);
  78         testOK(false, NoConstants::testObject);
  79         testOK(false, NoConstants::testArray);
  80         testNPE(      NoConstants::testNull);
  81     }
  82 
  83     public static void testOK(boolean expected, Callable<Object> test) throws Exception {
  84         for (int c = 0; c < ITERS; c++) {
  85             Object res = test.call();
  86             if (!res.equals(expected)) {
  87                 throw new IllegalStateException("Wrong result: expected = " + expected + ", but got " + res);
  88             }
  89         }
  90     }
  91 
  92     static volatile Object sink;
  93 
  94     public static void testNPE(Callable<Object> test) throws Exception {
  95         for (int c = 0; c < ITERS; c++) {
  96             try {
  97                sink = test.call();
  98                throw new IllegalStateException("Expected NPE");
  99             } catch (NullPointerException iae) {
 100                // expected
 101             }
 102         }
 103     }
 104 
 105     static volatile Class<?> classBoolean = boolean.class;
 106     static volatile Class<?> classByte    = byte.class;
 107     static volatile Class<?> classShort   = short.class;
 108     static volatile Class<?> classChar    = char.class;
 109     static volatile Class<?> classInt     = int.class;
 110     static volatile Class<?> classFloat   = float.class;
 111     static volatile Class<?> classLong    = long.class;
 112     static volatile Class<?> classDouble  = double.class;
 113     static volatile Class<?> classObject  = Object.class;
 114     static volatile Class<?> classArray   = Object[].class;
 115     static volatile Class<?> classNull    = null;
 116 
 117     static final Class<?> staticClassBoolean = boolean.class;
 118     static final Class<?> staticClassByte    = byte.class;
 119     static final Class<?> staticClassShort   = short.class;
 120     static final Class<?> staticClassChar    = char.class;
 121     static final Class<?> staticClassInt     = int.class;
 122     static final Class<?> staticClassFloat   = float.class;
 123     static final Class<?> staticClassLong    = long.class;
 124     static final Class<?> staticClassDouble  = double.class;
 125     static final Class<?> staticClassObject  = Object.class;
 126     static final Class<?> staticClassArray   = Object[].class;
 127     static final Class<?> staticClassNull    = null;
 128 
 129     static class InlineConstants {
 130         static boolean testBoolean() { return boolean.class.isPrimitive();  }
 131         static boolean testByte()    { return byte.class.isPrimitive();     }
 132         static boolean testShort()   { return short.class.isPrimitive();    }
 133         static boolean testChar()    { return char.class.isPrimitive();     }
 134         static boolean testInt()     { return int.class.isPrimitive();      }
 135         static boolean testFloat()   { return float.class.isPrimitive();    }
 136         static boolean testLong()    { return long.class.isPrimitive();     }
 137         static boolean testDouble()  { return double.class.isPrimitive();   }
 138         static boolean testObject()  { return Object.class.isPrimitive();   }
 139         static boolean testArray()   { return Object[].class.isPrimitive(); }
 140     }
 141 
 142     static class StaticConstants {
 143         static boolean testBoolean() { return staticClassBoolean.isPrimitive(); }
 144         static boolean testByte()    { return staticClassByte.isPrimitive();    }
 145         static boolean testShort()   { return staticClassShort.isPrimitive();   }
 146         static boolean testChar()    { return staticClassChar.isPrimitive();    }
 147         static boolean testInt()     { return staticClassInt.isPrimitive();     }
 148         static boolean testFloat()   { return staticClassFloat.isPrimitive();   }
 149         static boolean testLong()    { return staticClassLong.isPrimitive();    }
 150         static boolean testDouble()  { return staticClassDouble.isPrimitive();  }
 151         static boolean testObject()  { return staticClassObject.isPrimitive();  }
 152         static boolean testArray()   { return staticClassArray.isPrimitive();   }
 153         static boolean testNull()    { return staticClassNull.isPrimitive();    }
 154     }
 155 
 156     static class NoConstants {
 157         static boolean testBoolean() { return classBoolean.isPrimitive(); }
 158         static boolean testByte()    { return classByte.isPrimitive();    }
 159         static boolean testShort()   { return classShort.isPrimitive();   }
 160         static boolean testChar()    { return classChar.isPrimitive();    }
 161         static boolean testInt()     { return classInt.isPrimitive();     }
 162         static boolean testFloat()   { return classFloat.isPrimitive();   }
 163         static boolean testLong()    { return classLong.isPrimitive();    }
 164         static boolean testDouble()  { return classDouble.isPrimitive();  }
 165         static boolean testObject()  { return classObject.isPrimitive();  }
 166         static boolean testArray()   { return classArray.isPrimitive();   }
 167         static boolean testNull()    { return classNull.isPrimitive();    }
 168     }
 169 
 170 }
 171