1 /* 2 * Copyright (c) 2008, 2012, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.invoke.util; 27 28 public enum Wrapper { 29 // wrapperType primitiveType char zero emptyArray format 30 BOOLEAN( Boolean.class, boolean.class, 'Z', Boolean.FALSE, 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((byte)0), new byte[0], Format.signed( 8)), 34 SHORT ( Short.class, short.class, 'S', new Short((short)0), new short[0], Format.signed( 16)), 35 CHAR (Character.class, char.class, 'C', new Character((char)0), new char[0], Format.unsigned(16)), 36 INT ( Integer.class, int.class, 'I', new Integer(0), new int[0], Format.signed( 32)), 37 LONG ( Long.class, long.class, 'J', new Long(0), new long[0], Format.signed( 64)), 38 FLOAT ( Float.class, float.class, 'F', (Float)(float)0, new float[0], Format.floating(32)), 39 DOUBLE ( Double.class, double.class, 'D', (Double)(double)0, new double[0], Format.floating(64)), 40 OBJECT ( Object.class, Object.class, 'L', null, 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, 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 zero; 49 private final Object emptyArray; 50 private final int format; 51 private final String wrapperSimpleName; 52 private final String primitiveSimpleName; 53 54 private Wrapper(Class<?> wtype, Class<?> ptype, char tchar, Object zero, Object emptyArray, int format) { 55 this.wrapperType = wtype; 56 this.primitiveType = ptype; 57 this.basicTypeChar = tchar; 58 this.zero = zero; 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() { 67 return wrapperSimpleName+ 68 java.util.Arrays.asList(wrapperType, primitiveType, 69 basicTypeChar, zero, 70 "0x"+Integer.toHexString(format)); 71 } 72 73 private abstract static class Format { 74 static final int SLOT_SHIFT = 0, SIZE_SHIFT = 2, KIND_SHIFT = 12; 75 static final int 76 SIGNED = (-1) << KIND_SHIFT, 77 UNSIGNED = 0 << KIND_SHIFT, 78 FLOATING = 1 << KIND_SHIFT; 79 static final int 80 SLOT_MASK = ((1<<(SIZE_SHIFT-SLOT_SHIFT))-1), 81 SIZE_MASK = ((1<<(KIND_SHIFT-SIZE_SHIFT))-1); 82 static int format(int kind, int size, int slots) { 83 assert(((kind >> KIND_SHIFT) << KIND_SHIFT) == kind); 84 assert((size & (size-1)) == 0); // power of two 85 assert((kind == SIGNED) ? (size > 0) : 86 (kind == UNSIGNED) ? (size > 0) : 87 (kind == FLOATING) ? (size == 32 || size == 64) : 88 false); 89 assert((slots == 2) ? (size == 64) : 90 (slots == 1) ? (size <= 32) : 91 false); 92 return kind | (size << SIZE_SHIFT) | (slots << SLOT_SHIFT); 93 } 94 static final int 95 INT = SIGNED | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT), 96 SHORT = SIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT), 97 BOOLEAN = UNSIGNED | (1 << SIZE_SHIFT) | (1 << SLOT_SHIFT), 98 CHAR = UNSIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT), 99 FLOAT = FLOATING | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT), 100 VOID = UNSIGNED | (0 << SIZE_SHIFT) | (0 << SLOT_SHIFT), 101 NUM_MASK = (-1) << SIZE_SHIFT; 102 static int signed(int size) { return format(SIGNED, size, (size > 32 ? 2 : 1)); } 103 static int unsigned(int size) { return format(UNSIGNED, size, (size > 32 ? 2 : 1)); } 104 static int floating(int size) { return format(FLOATING, size, (size > 32 ? 2 : 1)); } 105 static int other(int slots) { return slots << SLOT_SHIFT; } 106 } 107 108 /// format queries: 109 110 /** How many bits are in the wrapped value? Returns 0 for OBJECT or VOID. */ 111 public int bitWidth() { return (format >> Format.SIZE_SHIFT) & Format.SIZE_MASK; } 112 /** How many JVM stack slots occupied by the wrapped value? Returns 0 for VOID. */ 113 public int stackSlots() { return (format >> Format.SLOT_SHIFT) & Format.SLOT_MASK; } 114 /** Does the wrapped value occupy a single JVM stack slot? */ 115 public boolean isSingleWord() { return (format & (1 << Format.SLOT_SHIFT)) != 0; } 116 /** Does the wrapped value occupy two JVM stack slots? */ 117 public boolean isDoubleWord() { return (format & (2 << Format.SLOT_SHIFT)) != 0; } 118 /** Is the wrapped type numeric (not void or object)? */ 119 public boolean isNumeric() { return (format & Format.NUM_MASK) != 0; } 120 /** Is the wrapped type a primitive other than float, double, or void? */ 121 public boolean isIntegral() { return isNumeric() && format < Format.FLOAT; } 122 /** Is the wrapped type one of int, boolean, byte, char, or short? */ 123 public boolean isSubwordOrInt() { return isIntegral() && isSingleWord(); } 124 /* Is the wrapped value a signed integral type (one of byte, short, int, or long)? */ 125 public boolean isSigned() { return format < Format.VOID; } 126 /* Is the wrapped value an unsigned integral type (one of boolean or char)? */ 127 public boolean isUnsigned() { return format >= Format.BOOLEAN && format < Format.FLOAT; } 128 /** Is the wrapped type either float or double? */ 129 public boolean isFloating() { return format >= Format.FLOAT; } 130 /** Is the wrapped type either void or a reference? */ 131 public boolean isOther() { return (format & ~Format.SLOT_MASK) == 0; } 132 133 /** Does the JLS 5.1.2 allow a variable of this wrapper's 134 * primitive type to be assigned from a value of the given wrapper's primitive type? 135 * Cases: 136 * <ul> 137 * <li>unboxing followed by widening primitive conversion 138 * <li>any type converted to {@code void} (i.e., dropping a method call's value) 139 * <li>boxing conversion followed by widening reference conversion to {@code Object} 140 * </ul> 141 * These are the cases allowed by MethodHandle.asType. 142 */ 143 public boolean isConvertibleFrom(Wrapper source) { 144 if (this == source) return true; 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 { assert(checkConvertibleFrom()); } 166 private static boolean checkConvertibleFrom() { 167 // Check the matrix for correct classification of widening conversions. 168 for (Wrapper w : values()) { 169 assert(w.isConvertibleFrom(w)); 170 assert(VOID.isConvertibleFrom(w)); 171 if (w != VOID) { 172 assert(OBJECT.isConvertibleFrom(w)); 173 assert(!w.isConvertibleFrom(VOID)); 174 } 175 // check relations with unsigned integral types: 176 if (w != CHAR) { 177 assert(!CHAR.isConvertibleFrom(w)); 178 if (!w.isConvertibleFrom(INT)) 179 assert(!w.isConvertibleFrom(CHAR)); 180 } 181 if (w != BOOLEAN) { 182 assert(!BOOLEAN.isConvertibleFrom(w)); 183 if (w != VOID && w != OBJECT) 184 assert(!w.isConvertibleFrom(BOOLEAN)); 185 } 186 // check relations with signed integral types: 187 if (w.isSigned()) { 188 for (Wrapper x : values()) { 189 if (w == x) continue; 190 if (x.isFloating()) 191 assert(!w.isConvertibleFrom(x)); 192 else if (x.isSigned()) { 193 if (w.compareTo(x) < 0) 194 assert(!w.isConvertibleFrom(x)); 195 else 196 assert(w.isConvertibleFrom(x)); 197 } 198 } 199 } 200 // check relations with floating types: 201 if (w.isFloating()) { 202 for (Wrapper x : values()) { 203 if (w == x) continue; 204 if (x.isSigned()) 205 assert(w.isConvertibleFrom(x)); 206 else if (x.isFloating()) { 207 if (w.compareTo(x) < 0) 208 assert(!w.isConvertibleFrom(x)); 209 else 210 assert(w.isConvertibleFrom(x)); 211 } 212 } 213 } 214 } 215 return true; // i.e., assert(true) 216 } 217 218 /** Produce a zero value for the given wrapper type. 219 * This will be a numeric zero for a number or character, 220 * false for a boolean, and null for a reference or void. 221 * The common thread is that this is what is contained 222 * in a default-initialized variable of the given primitive 223 * type. (For void, it is what a reflective method returns 224 * instead of no value at all.) 225 */ 226 public Object zero() { return zero; } 227 228 /** Produce a zero value for the given wrapper type T. 229 * The optional argument must a type compatible with this wrapper. 230 * Equivalent to {@code this.cast(this.zero(), type)}. 231 */ 232 public <T> T zero(Class<T> type) { return convert(zero, type); } 233 234 /** Return the wrapper that wraps values of the given type. 235 * The type may be {@code Object}, meaning the {@code OBJECT} wrapper. 236 * Otherwise, the type must be a primitive. 237 * @throws IllegalArgumentException for unexpected types 238 */ 239 public static Wrapper forPrimitiveType(Class<?> type) { 240 Wrapper w = findPrimitiveType(type); 241 if (w != null) return w; 242 if (type.isPrimitive()) 243 throw new InternalError(); // redo hash function 244 throw newIllegalArgumentException("not primitive: "+type); 245 } 246 247 static Wrapper findPrimitiveType(Class<?> type) { 248 Wrapper w = FROM_PRIM[hashPrim(type)]; 249 if (w != null && w.primitiveType == type) { 250 return w; 251 } 252 return null; 253 } 254 255 /** Return the wrapper that wraps values into the given wrapper type. 256 * If it is {@code Object}, return {@code OBJECT}. 257 * Otherwise, it must be a wrapper type. 258 * The type must not be a primitive type. 259 * @throws IllegalArgumentException for unexpected types 260 */ 261 public static Wrapper forWrapperType(Class<?> type) { 262 Wrapper w = findWrapperType(type); 263 if (w != null) return w; 264 for (Wrapper x : values()) 265 if (x.wrapperType == type) 266 throw new InternalError(); // redo hash function 267 throw newIllegalArgumentException("not wrapper: "+type); 268 } 269 270 static Wrapper findWrapperType(Class<?> type) { 271 Wrapper w = FROM_WRAP[hashWrap(type)]; 272 if (w != null && w.wrapperType == type) { 273 return w; 274 } 275 return null; 276 } 277 278 /** Return the wrapper that corresponds to the given bytecode 279 * signature character. Return {@code OBJECT} for the character 'L'. 280 * @throws IllegalArgumentException for any non-signature character or {@code '['}. 281 */ 282 public static Wrapper forBasicType(char type) { 283 Wrapper w = FROM_CHAR[hashChar(type)]; 284 if (w != null && w.basicTypeChar == type) { 285 return w; 286 } 287 for (Wrapper x : values()) 288 if (w.basicTypeChar == type) 289 throw new InternalError(); // redo hash function 290 throw newIllegalArgumentException("not basic type char: "+type); 291 } 292 293 /** Return the wrapper for the given type, if it is 294 * a primitive type, else return {@code OBJECT}. 295 */ 296 public static Wrapper forBasicType(Class<?> type) { 297 if (type.isPrimitive()) 298 return forPrimitiveType(type); 299 return OBJECT; // any reference, including wrappers or arrays 300 } 301 302 // Note on perfect hashes: 303 // for signature chars c, do (c + (c >> 1)) % 16 304 // for primitive type names n, do (n[0] + n[2]) % 16 305 // The type name hash works for both primitive and wrapper names. 306 // You can add "java/lang/Object" to the primitive names. 307 // But you add the wrapper name Object, use (n[2] + (3*n[1])) % 16. 308 private static final Wrapper[] FROM_PRIM = new Wrapper[16]; 309 private static final Wrapper[] FROM_WRAP = new Wrapper[16]; 310 private static final Wrapper[] FROM_CHAR = new Wrapper[16]; 311 private static int hashPrim(Class<?> x) { 312 String xn = x.getName(); 313 if (xn.length() < 3) return 0; 314 return (xn.charAt(0) + xn.charAt(2)) % 16; 315 } 316 private static int hashWrap(Class<?> x) { 317 String xn = x.getName(); 318 final int offset = 10; assert(offset == "java.lang.".length()); 319 if (xn.length() < offset+3) return 0; 320 return (3*xn.charAt(offset+1) + xn.charAt(offset+2)) % 16; 321 } 322 private static int hashChar(char x) { 323 return (x + (x >> 1)) % 16; 324 } 325 static { 326 for (Wrapper w : values()) { 327 int pi = hashPrim(w.primitiveType); 328 int wi = hashWrap(w.wrapperType); 329 int ci = hashChar(w.basicTypeChar); 330 assert(FROM_PRIM[pi] == null); 331 assert(FROM_WRAP[wi] == null); 332 assert(FROM_CHAR[ci] == null); 333 FROM_PRIM[pi] = w; 334 FROM_WRAP[wi] = w; 335 FROM_CHAR[ci] = w; 336 } 337 //assert(jdk.sun.invoke.util.WrapperTest.test(false)); 338 } 339 340 /** What is the primitive type wrapped by this wrapper? */ 341 public Class<?> primitiveType() { return primitiveType; } 342 343 /** What is the wrapper type for this wrapper? */ 344 public Class<?> wrapperType() { return wrapperType; } 345 346 /** What is the wrapper type for this wrapper? 347 * Otherwise, the example type must be the wrapper type, 348 * or the corresponding primitive type. 349 * (For {@code OBJECT}, the example type can be any non-primitive, 350 * and is normalized to {@code Object.class}.) 351 * The resulting class type has the same type parameter. 352 */ 353 public <T> Class<T> wrapperType(Class<T> exampleType) { 354 if (exampleType == wrapperType) { 355 return exampleType; 356 } else if (exampleType == primitiveType || 357 wrapperType == Object.class || 358 exampleType.isInterface()) { 359 return forceType(wrapperType, exampleType); 360 } 361 throw newClassCastException(exampleType, primitiveType); 362 } 363 364 private static ClassCastException newClassCastException(Class<?> actual, Class<?> expected) { 365 return new ClassCastException(actual + " is not compatible with " + expected); 366 } 367 368 /** If {@code type} is a primitive type, return the corresponding 369 * wrapper type, else return {@code type} unchanged. 370 */ 371 public static <T> Class<T> asWrapperType(Class<T> type) { 372 if (type.isPrimitive()) { 373 return forPrimitiveType(type).wrapperType(type); 374 } 375 return type; 376 } 377 378 /** If {@code type} is a wrapper type, return the corresponding 379 * primitive type, else return {@code type} unchanged. 380 */ 381 public static <T> Class<T> asPrimitiveType(Class<T> type) { 382 Wrapper w = findWrapperType(type); 383 if (w != null) { 384 return forceType(w.primitiveType(), type); 385 } 386 return type; 387 } 388 389 /** Query: Is the given type a wrapper, such as {@code Integer} or {@code Void}? */ 390 public static boolean isWrapperType(Class<?> type) { 391 return findWrapperType(type) != null; 392 } 393 394 /** Query: Is the given type a primitive, such as {@code int} or {@code void}? */ 395 public static boolean isPrimitiveType(Class<?> type) { 396 return type.isPrimitive(); 397 } 398 399 /** What is the bytecode signature character for this type? 400 * All non-primitives, including array types, report as 'L', the signature character for references. 401 */ 402 public static char basicTypeChar(Class<?> type) { 403 if (!type.isPrimitive()) 404 return 'L'; 405 else 406 return forPrimitiveType(type).basicTypeChar(); 407 } 408 409 /** What is the bytecode signature character for this wrapper's 410 * primitive type? 411 */ 412 public char basicTypeChar() { return basicTypeChar; } 413 414 /** What is the simple name of the wrapper type? 415 */ 416 public String wrapperSimpleName() { return wrapperSimpleName; } 417 418 /** What is the simple name of the primitive type? 419 */ 420 public String primitiveSimpleName() { return primitiveSimpleName; } 421 422 // /** Wrap a value in the given type, which may be either a primitive or wrapper type. 423 // * Performs standard primitive conversions, including truncation and float conversions. 424 // */ 425 // public static <T> T wrap(Object x, Class<T> type) { 426 // return Wrapper.valueOf(type).cast(x, type); 427 // } 428 429 /** Cast a wrapped value to the given type, which may be either a primitive or wrapper type. 430 * The given target type must be this wrapper's primitive or wrapper type. 431 * If this wrapper is OBJECT, the target type may also be an interface, perform no runtime check. 432 * Performs standard primitive conversions, including truncation and float conversions. 433 * The given type must be compatible with this wrapper. That is, it must either 434 * be the wrapper type (or a subtype, in the case of {@code OBJECT}) or else 435 * it must be the wrapper's primitive type. 436 * Primitive conversions are only performed if the given type is itself a primitive. 437 * @throws ClassCastException if the given type is not compatible with this wrapper 438 */ 439 public <T> T cast(Object x, Class<T> type) { 440 return convert(x, type, true); 441 } 442 443 /** Convert a wrapped value to the given type. 444 * The given target type must be this wrapper's primitive or wrapper type. 445 * This is equivalent to {@link #cast}, except that it refuses to perform 446 * narrowing primitive conversions. 447 */ 448 public <T> T convert(Object x, Class<T> type) { 449 return convert(x, type, false); 450 } 451 452 private <T> T convert(Object x, Class<T> type, boolean isCast) { 453 if (this == OBJECT) { 454 // If the target wrapper is OBJECT, just do a reference cast. 455 // If the target type is an interface, perform no runtime check. 456 // (This loophole is safe, and is allowed by the JVM verifier.) 457 // If the target type is a primitive, change it to a wrapper. 458 assert(!type.isPrimitive()); 459 if (!type.isInterface()) 460 type.cast(x); 461 @SuppressWarnings("unchecked") 462 T result = (T) x; // unchecked warning is expected here 463 return result; 464 } 465 Class<T> wtype = wrapperType(type); 466 if (wtype.isInstance(x)) { 467 return wtype.cast(x); 468 } 469 if (!isCast) { 470 Class<?> sourceType = x.getClass(); // throw NPE if x is null 471 Wrapper source = findWrapperType(sourceType); 472 if (source == null || !this.isConvertibleFrom(source)) { 473 throw newClassCastException(wtype, sourceType); 474 } 475 } else if (x == null) { 476 @SuppressWarnings("unchecked") 477 T z = (T) zero; 478 return z; 479 } 480 @SuppressWarnings("unchecked") 481 T result = (T) wrap(x); // unchecked warning is expected here 482 assert (result == null ? Void.class : result.getClass()) == wtype; 483 return result; 484 } 485 486 /** Cast a reference type to another reference type. 487 * If the target type is an interface, perform no runtime check. 488 * (This loophole is safe, and is allowed by the JVM verifier.) 489 * If the target type is a primitive, change it to a wrapper. 490 */ 491 static <T> Class<T> forceType(Class<?> type, Class<T> exampleType) { 492 boolean z = (type == exampleType || 493 type.isPrimitive() && forPrimitiveType(type) == findWrapperType(exampleType) || 494 exampleType.isPrimitive() && forPrimitiveType(exampleType) == findWrapperType(type) || 495 type == Object.class && !exampleType.isPrimitive()); 496 if (!z) 497 System.out.println(type+" <= "+exampleType); 498 assert(type == exampleType || 499 type.isPrimitive() && forPrimitiveType(type) == findWrapperType(exampleType) || 500 exampleType.isPrimitive() && forPrimitiveType(exampleType) == findWrapperType(type) || 501 type == Object.class && !exampleType.isPrimitive()); 502 @SuppressWarnings("unchecked") 503 Class<T> result = (Class<T>) type; // unchecked warning is expected here 504 return result; 505 } 506 507 /** Wrap a value in this wrapper's type. 508 * Performs standard primitive conversions, including truncation and float conversions. 509 * Performs returns the unchanged reference for {@code OBJECT}. 510 * Returns null for {@code VOID}. 511 * Returns a zero value for a null input. 512 * @throws ClassCastException if this wrapper is numeric and the operand 513 * is not a number, character, boolean, or null 514 */ 515 public Object wrap(Object x) { 516 // do non-numeric wrappers first 517 switch (basicTypeChar) { 518 case 'L': return x; 519 case 'V': return null; 520 } 521 Number xn = numberValue(x); 522 switch (basicTypeChar) { 523 case 'I': return Integer.valueOf(xn.intValue()); 524 case 'J': return Long.valueOf(xn.longValue()); 525 case 'F': return Float.valueOf(xn.floatValue()); 526 case 'D': return Double.valueOf(xn.doubleValue()); 527 case 'S': return Short.valueOf((short) xn.intValue()); 528 case 'B': return Byte.valueOf((byte) xn.intValue()); 529 case 'C': return Character.valueOf((char) xn.intValue()); 530 case 'Z': return Boolean.valueOf(boolValue(xn.byteValue())); 531 } 532 throw new InternalError("bad wrapper"); 533 } 534 535 /** Wrap a value (an int or smaller value) in this wrapper's type. 536 * Performs standard primitive conversions, including truncation and float conversions. 537 * Produces an {@code Integer} for {@code OBJECT}, although the exact type 538 * of the operand is not known. 539 * Returns null for {@code VOID}. 540 */ 541 public Object wrap(int x) { 542 if (basicTypeChar == 'L') return (Integer)x; 543 switch (basicTypeChar) { 544 case 'L': throw newIllegalArgumentException("cannot wrap to object type"); 545 case 'V': return null; 546 case 'I': return Integer.valueOf(x); 547 case 'J': return Long.valueOf(x); 548 case 'F': return Float.valueOf(x); 549 case 'D': return Double.valueOf(x); 550 case 'S': return Short.valueOf((short) x); 551 case 'B': return Byte.valueOf((byte) x); 552 case 'C': return Character.valueOf((char) x); 553 case 'Z': return Boolean.valueOf(boolValue((byte) x)); 554 } 555 throw new InternalError("bad wrapper"); 556 } 557 558 private static Number numberValue(Object x) { 559 if (x instanceof Number) return (Number)x; 560 if (x instanceof Character) return (int)(Character)x; 561 if (x instanceof Boolean) return (Boolean)x ? 1 : 0; 562 // Remaining allowed case of void: Must be a null reference. 563 return (Number)x; 564 } 565 566 // Parameter type of boolValue must be byte, because 567 // MethodHandles.explicitCastArguments defines boolean 568 // conversion as first converting to byte. 569 private static boolean boolValue(byte bits) { 570 bits &= 1; // simple 31-bit zero extension 571 return (bits != 0); 572 } 573 574 private static RuntimeException newIllegalArgumentException(String message, Object x) { 575 return newIllegalArgumentException(message + x); 576 } 577 private static RuntimeException newIllegalArgumentException(String message) { 578 return new IllegalArgumentException(message); 579 } 580 581 // primitive array support 582 public Object makeArray(int len) { 583 return java.lang.reflect.Array.newInstance(primitiveType, len); 584 } 585 public Class<?> arrayType() { 586 return emptyArray.getClass(); 587 } 588 public void copyArrayUnboxing(Object[] values, int vpos, Object a, int apos, int length) { 589 if (a.getClass() != arrayType()) 590 arrayType().cast(a); // throw NPE or CCE if bad type 591 for (int i = 0; i < length; i++) { 592 Object value = values[i+vpos]; 593 value = convert(value, primitiveType); 594 java.lang.reflect.Array.set(a, i+apos, value); 595 } 596 } 597 public void copyArrayBoxing(Object a, int apos, Object[] values, int vpos, int length) { 598 if (a.getClass() != arrayType()) 599 arrayType().cast(a); // throw NPE or CCE if bad type 600 for (int i = 0; i < length; i++) { 601 Object value = java.lang.reflect.Array.get(a, i+apos); 602 //Already done: value = convert(value, primitiveType); 603 assert(value.getClass() == wrapperType); 604 values[i+vpos] = value; 605 } 606 } 607 }