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  *
  30  * @run main/othervm -ea -Diters=200   -Xint
  31  *      TestIsPrimitive
  32  * @run main/othervm -ea -XX:-UseSharedSpaces -Diters=30000 -XX:TieredStopAtLevel=1
  33  *      TestIsPrimitive
  34  * @run main/othervm -ea -Diters=30000 -XX:TieredStopAtLevel=4
  35  *      TestIsPrimitive
  36  */
  37 
  38 import java.util.concurrent.Callable;
  39 
  40 public class TestIsPrimitive {
  41     static final int ITERS = Integer.getInteger("iters", 1);
  42 
  43     public static void main(String... args) throws Exception {
  44         testOK(true,  InlineConstants::testBoolean);
  45         testOK(true,  InlineConstants::testByte);
  46         testOK(true,  InlineConstants::testShort);
  47         testOK(true,  InlineConstants::testChar);
  48         testOK(true,  InlineConstants::testInt);
  49         testOK(true,  InlineConstants::testFloat);
  50         testOK(true,  InlineConstants::testLong);
  51         testOK(true,  InlineConstants::testDouble);
  52         testOK(false, InlineConstants::testObject);
  53         testOK(false, InlineConstants::testArray);
  54         testOK(false, InlineConstants::testBooleanArray);
  55 
  56         testOK(true,  StaticConstants::testBoolean);
  57         testOK(true,  StaticConstants::testByte);
  58         testOK(true,  StaticConstants::testShort);
  59         testOK(true,  StaticConstants::testChar);
  60         testOK(true,  StaticConstants::testInt);
  61         testOK(true,  StaticConstants::testFloat);
  62         testOK(true,  StaticConstants::testLong);
  63         testOK(true,  StaticConstants::testDouble);
  64         testOK(false, StaticConstants::testObject);
  65         testOK(false, StaticConstants::testArray);
  66         testOK(false, StaticConstants::testBooleanArray);
  67         testNPE(      StaticConstants::testNull);
  68 
  69         testOK(true,  NoConstants::testBoolean);
  70         testOK(true,  NoConstants::testByte);
  71         testOK(true,  NoConstants::testShort);
  72         testOK(true,  NoConstants::testChar);
  73         testOK(true,  NoConstants::testInt);
  74         testOK(true,  NoConstants::testFloat);
  75         testOK(true,  NoConstants::testLong);
  76         testOK(true,  NoConstants::testDouble);
  77         testOK(false, NoConstants::testObject);
  78         testOK(false, NoConstants::testArray);
  79         testOK(false, NoConstants::testBooleanArray);
  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     static volatile Class<?> classBooleanArray = boolean[].class;
 117 
 118     static final Class<?> staticClassBoolean = boolean.class;
 119     static final Class<?> staticClassByte    = byte.class;
 120     static final Class<?> staticClassShort   = short.class;
 121     static final Class<?> staticClassChar    = char.class;
 122     static final Class<?> staticClassInt     = int.class;
 123     static final Class<?> staticClassFloat   = float.class;
 124     static final Class<?> staticClassLong    = long.class;
 125     static final Class<?> staticClassDouble  = double.class;
 126     static final Class<?> staticClassObject  = Object.class;
 127     static final Class<?> staticClassArray   = Object[].class;
 128     static final Class<?> staticClassNull    = null;
 129     static final Class<?> staticClassBooleanArray = boolean[].class;
 130 
 131     static class InlineConstants {
 132         static boolean testBoolean() { return boolean.class.isPrimitive();  }
 133         static boolean testByte()    { return byte.class.isPrimitive();     }
 134         static boolean testShort()   { return short.class.isPrimitive();    }
 135         static boolean testChar()    { return char.class.isPrimitive();     }
 136         static boolean testInt()     { return int.class.isPrimitive();      }
 137         static boolean testFloat()   { return float.class.isPrimitive();    }
 138         static boolean testLong()    { return long.class.isPrimitive();     }
 139         static boolean testDouble()  { return double.class.isPrimitive();   }
 140         static boolean testObject()  { return Object.class.isPrimitive();   }
 141         static boolean testArray()   { return Object[].class.isPrimitive(); }
 142         static boolean testBooleanArray() { return boolean[].class.isPrimitive(); }
 143     }
 144 
 145     static class StaticConstants {
 146         static boolean testBoolean() { return staticClassBoolean.isPrimitive(); }
 147         static boolean testByte()    { return staticClassByte.isPrimitive();    }
 148         static boolean testShort()   { return staticClassShort.isPrimitive();   }
 149         static boolean testChar()    { return staticClassChar.isPrimitive();    }
 150         static boolean testInt()     { return staticClassInt.isPrimitive();     }
 151         static boolean testFloat()   { return staticClassFloat.isPrimitive();   }
 152         static boolean testLong()    { return staticClassLong.isPrimitive();    }
 153         static boolean testDouble()  { return staticClassDouble.isPrimitive();  }
 154         static boolean testObject()  { return staticClassObject.isPrimitive();  }
 155         static boolean testArray()   { return staticClassArray.isPrimitive();   }
 156         static boolean testNull()    { return staticClassNull.isPrimitive();    }
 157         static boolean testBooleanArray() { return staticClassBooleanArray.isPrimitive(); }
 158     }
 159 
 160     static class NoConstants {
 161         static boolean testBoolean() { return classBoolean.isPrimitive(); }
 162         static boolean testByte()    { return classByte.isPrimitive();    }
 163         static boolean testShort()   { return classShort.isPrimitive();   }
 164         static boolean testChar()    { return classChar.isPrimitive();    }
 165         static boolean testInt()     { return classInt.isPrimitive();     }
 166         static boolean testFloat()   { return classFloat.isPrimitive();   }
 167         static boolean testLong()    { return classLong.isPrimitive();    }
 168         static boolean testDouble()  { return classDouble.isPrimitive();  }
 169         static boolean testObject()  { return classObject.isPrimitive();  }
 170         static boolean testArray()   { return classArray.isPrimitive();   }
 171         static boolean testNull()    { return classNull.isPrimitive();    }
 172         static boolean testBooleanArray() { return classBooleanArray.isPrimitive();    }
 173     }
 174 
 175 
 176 }
 177