< prev index next >

src/java.base/share/classes/sun/invoke/util/Wrapper.java

Print this page
rev 15320 : 8163370: Reduce number of classes loaded by common usage of java.lang.invoke
Reviewed-by: igerasim, psandoz


  25 
  26 package sun.invoke.util;
  27 
  28 public enum Wrapper {
  29     //        wrapperType    primitiveType  char     emptyArray          format
  30     BOOLEAN(  Boolean.class, boolean.class, 'Z', new boolean[0], Format.unsigned( 1)),
  31     // These must be in the order defined for widening primitive conversions in JLS 5.1.2
  32     // Avoid boxing integral types here to defer initialization of internal caches
  33     BYTE   (     Byte.class,    byte.class, 'B', new    byte[0], Format.signed(   8)),
  34     SHORT  (    Short.class,   short.class, 'S', new   short[0], Format.signed(  16)),
  35     CHAR   (Character.class,    char.class, 'C', new    char[0], Format.unsigned(16)),
  36     INT    (  Integer.class,     int.class, 'I', new     int[0], Format.signed(  32)),
  37     LONG   (     Long.class,    long.class, 'J', new    long[0], Format.signed(  64)),
  38     FLOAT  (    Float.class,   float.class, 'F', new   float[0], Format.floating(32)),
  39     DOUBLE (   Double.class,  double.class, 'D', new  double[0], Format.floating(64)),
  40     OBJECT (   Object.class,  Object.class, 'L', new  Object[0], Format.other(    1)),
  41     // VOID must be the last type, since it is "assignable" from any other type:
  42     VOID   (     Void.class,    void.class, 'V',           null, Format.other(    0)),
  43     ;
  44 


  45     private final Class<?> wrapperType;
  46     private final Class<?> primitiveType;
  47     private final char     basicTypeChar;
  48     private final Object   emptyArray;
  49     private final int      format;
  50     private final String   wrapperSimpleName;
  51     private final String   primitiveSimpleName;
  52 
  53     private Wrapper(Class<?> wtype, Class<?> ptype, char tchar, Object emptyArray, int format) {
  54         this.wrapperType = wtype;
  55         this.primitiveType = ptype;
  56         this.basicTypeChar = tchar;
  57         this.emptyArray = emptyArray;
  58         this.format = format;
  59         this.wrapperSimpleName = wtype.getSimpleName();
  60         this.primitiveSimpleName = ptype.getSimpleName();
  61     }
  62 
  63     /** For debugging, give the details of this wrapper. */
  64     public String detailString() {


 143         if (this.compareTo(source) < 0) {
 144             // At best, this is a narrowing conversion.
 145             return false;
 146         }
 147         // All conversions are allowed in the enum order between floats and signed ints.
 148         // First detect non-signed non-float types (boolean, char, Object, void).
 149         boolean floatOrSigned = (((this.format & source.format) & Format.SIGNED) != 0);
 150         if (!floatOrSigned) {
 151             if (this.isOther())  return true;
 152             // can convert char to int or wider, but nothing else
 153             if (source.format == Format.CHAR)  return true;
 154             // no other conversions are classified as widening
 155             return false;
 156         }
 157         // All signed and float conversions in the enum order are widening.
 158         assert(this.isFloating() || this.isSigned());
 159         assert(source.isFloating() || source.isSigned());
 160         return true;
 161     }
 162 
 163     static { assert(checkConvertibleFrom()); }



 164     private static boolean checkConvertibleFrom() {
 165         // Check the matrix for correct classification of widening conversions.
 166         for (Wrapper w : values()) {
 167             assert(w.isConvertibleFrom(w));
 168             assert(VOID.isConvertibleFrom(w));
 169             if (w != VOID) {
 170                 assert(OBJECT.isConvertibleFrom(w));
 171                 assert(!w.isConvertibleFrom(VOID));
 172             }
 173             // check relations with unsigned integral types:
 174             if (w != CHAR) {
 175                 assert(!CHAR.isConvertibleFrom(w));
 176                 if (!w.isConvertibleFrom(INT))
 177                     assert(!w.isConvertibleFrom(CHAR));
 178             }
 179             if (w != BOOLEAN) {
 180                 assert(!BOOLEAN.isConvertibleFrom(w));
 181                 if (w != VOID && w != OBJECT)
 182                     assert(!w.isConvertibleFrom(BOOLEAN));
 183             }




  25 
  26 package sun.invoke.util;
  27 
  28 public enum Wrapper {
  29     //        wrapperType    primitiveType  char     emptyArray          format
  30     BOOLEAN(  Boolean.class, boolean.class, 'Z', new boolean[0], Format.unsigned( 1)),
  31     // These must be in the order defined for widening primitive conversions in JLS 5.1.2
  32     // Avoid boxing integral types here to defer initialization of internal caches
  33     BYTE   (     Byte.class,    byte.class, 'B', new    byte[0], Format.signed(   8)),
  34     SHORT  (    Short.class,   short.class, 'S', new   short[0], Format.signed(  16)),
  35     CHAR   (Character.class,    char.class, 'C', new    char[0], Format.unsigned(16)),
  36     INT    (  Integer.class,     int.class, 'I', new     int[0], Format.signed(  32)),
  37     LONG   (     Long.class,    long.class, 'J', new    long[0], Format.signed(  64)),
  38     FLOAT  (    Float.class,   float.class, 'F', new   float[0], Format.floating(32)),
  39     DOUBLE (   Double.class,  double.class, 'D', new  double[0], Format.floating(64)),
  40     OBJECT (   Object.class,  Object.class, 'L', new  Object[0], Format.other(    1)),
  41     // VOID must be the last type, since it is "assignable" from any other type:
  42     VOID   (     Void.class,    void.class, 'V',           null, Format.other(    0)),
  43     ;
  44 
  45     public static final int COUNT = 10;
  46 
  47     private final Class<?> wrapperType;
  48     private final Class<?> primitiveType;
  49     private final char     basicTypeChar;
  50     private final Object   emptyArray;
  51     private final int      format;
  52     private final String   wrapperSimpleName;
  53     private final String   primitiveSimpleName;
  54 
  55     private Wrapper(Class<?> wtype, Class<?> ptype, char tchar, Object emptyArray, int format) {
  56         this.wrapperType = wtype;
  57         this.primitiveType = ptype;
  58         this.basicTypeChar = tchar;
  59         this.emptyArray = emptyArray;
  60         this.format = format;
  61         this.wrapperSimpleName = wtype.getSimpleName();
  62         this.primitiveSimpleName = ptype.getSimpleName();
  63     }
  64 
  65     /** For debugging, give the details of this wrapper. */
  66     public String detailString() {


 145         if (this.compareTo(source) < 0) {
 146             // At best, this is a narrowing conversion.
 147             return false;
 148         }
 149         // All conversions are allowed in the enum order between floats and signed ints.
 150         // First detect non-signed non-float types (boolean, char, Object, void).
 151         boolean floatOrSigned = (((this.format & source.format) & Format.SIGNED) != 0);
 152         if (!floatOrSigned) {
 153             if (this.isOther())  return true;
 154             // can convert char to int or wider, but nothing else
 155             if (source.format == Format.CHAR)  return true;
 156             // no other conversions are classified as widening
 157             return false;
 158         }
 159         // All signed and float conversions in the enum order are widening.
 160         assert(this.isFloating() || this.isSigned());
 161         assert(source.isFloating() || source.isSigned());
 162         return true;
 163     }
 164 
 165     static {
 166         assert(checkConvertibleFrom());
 167         assert(COUNT == Wrapper.values().length);
 168     }
 169     private static boolean checkConvertibleFrom() {
 170         // Check the matrix for correct classification of widening conversions.
 171         for (Wrapper w : values()) {
 172             assert(w.isConvertibleFrom(w));
 173             assert(VOID.isConvertibleFrom(w));
 174             if (w != VOID) {
 175                 assert(OBJECT.isConvertibleFrom(w));
 176                 assert(!w.isConvertibleFrom(VOID));
 177             }
 178             // check relations with unsigned integral types:
 179             if (w != CHAR) {
 180                 assert(!CHAR.isConvertibleFrom(w));
 181                 if (!w.isConvertibleFrom(INT))
 182                     assert(!w.isConvertibleFrom(CHAR));
 183             }
 184             if (w != BOOLEAN) {
 185                 assert(!BOOLEAN.isConvertibleFrom(w));
 186                 if (w != VOID && w != OBJECT)
 187                     assert(!w.isConvertibleFrom(BOOLEAN));
 188             }


< prev index next >