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