sun.invoke.util.Wrapper Java Examples
The following examples show how to use
sun.invoke.util.Wrapper.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExplicitCastArgumentsTest.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * Tests that primitive is successfully converted to other primitive type. */ public static void testPrim2Prim() { for (Wrapper from : Wrapper.values()) { for (Wrapper to : Wrapper.values()) { if (from == Wrapper.VOID || to == Wrapper.VOID || from == Wrapper.OBJECT || to == Wrapper.OBJECT) { continue; } Object value = RANDOM_VALUES.get(from); Object convValue = to.wrap(value); for (TestConversionMode mode : TestConversionMode.values()) { testConversion(mode, from.primitiveType(), to.primitiveType(), value, convValue, false, null); } } } }
Example #2
Source File: InvokerBytecodeGenerator.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
private void emitPushArgument(Class<?> ptype, Object arg) { BasicType bptype = basicType(ptype); if (arg instanceof Name) { Name n = (Name) arg; emitLoadInsn(n.type, n.index()); emitImplicitConversion(n.type, ptype, n); } else if ((arg == null || arg instanceof String) && bptype == L_TYPE) { emitConst(arg); } else { if (Wrapper.isWrapperType(arg.getClass()) && bptype != L_TYPE) { emitConst(arg); } else { mv.visitLdcInsn(constantPlaceholder(arg)); emitImplicitConversion(L_TYPE, ptype, arg); } } }
Example #3
Source File: InvokerBytecodeGenerator.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private void emitPushArgument(Class<?> ptype, Object arg) { BasicType bptype = basicType(ptype); if (arg instanceof Name) { Name n = (Name) arg; emitLoadInsn(n.type, n.index()); emitImplicitConversion(n.type, ptype, n); } else if ((arg == null || arg instanceof String) && bptype == L_TYPE) { emitConst(arg); } else { if (Wrapper.isWrapperType(arg.getClass()) && bptype != L_TYPE) { emitConst(arg); } else { mv.visitLdcInsn(constantPlaceholder(arg)); emitImplicitConversion(L_TYPE, ptype, arg); } } }
Example #4
Source File: InvokerBytecodeGenerator.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
private void emitI2X(Wrapper type) { switch (type) { case BYTE: mv.visitInsn(Opcodes.I2B); break; case SHORT: mv.visitInsn(Opcodes.I2S); break; case CHAR: mv.visitInsn(Opcodes.I2C); break; case INT: /* naught */ break; case LONG: mv.visitInsn(Opcodes.I2L); break; case FLOAT: mv.visitInsn(Opcodes.I2F); break; case DOUBLE: mv.visitInsn(Opcodes.I2D); break; case BOOLEAN: // For compatibility with ValueConversions and explicitCastArguments: mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IAND); break; default: throw new InternalError("unknown type: " + type); } }
Example #5
Source File: ValueConversionsTest.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
@Test public void testBox() throws Throwable { for (Wrapper w : Wrapper.values()) { if (w == Wrapper.VOID) continue; // skip this; no unboxed form if (w == Wrapper.OBJECT) continue; // skip this; already unboxed for (int n = -5; n < 10; n++) { Object box = w.wrap(n); MethodHandle boxer = ValueConversions.boxExact(w); Object expResult = box; Object result = null; switch (w) { case INT: result = (Integer) boxer.invokeExact(/*int*/n); break; case LONG: result = (Long) boxer.invokeExact((long)n); break; case FLOAT: result = (Float) boxer.invokeExact((float)n); break; case DOUBLE: result = (Double) boxer.invokeExact((double)n); break; case CHAR: result = (Character) boxer.invokeExact((char)n); break; case BYTE: result = (Byte) boxer.invokeExact((byte)n); break; case SHORT: result = (Short) boxer.invokeExact((short)n); break; case BOOLEAN: result = (Boolean) boxer.invokeExact((n & 1) != 0); break; } assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box), expResult, result); } } }
Example #6
Source File: ValueConversionsTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
@Test public void testConvert() throws Throwable { for (long tval = 0, ctr = 0;;) { if (++ctr > 99999) throw new AssertionError("too many test values"); // prints 3776 test patterns (3776 = 8*59*8) tval = nextTestValue(tval); if (tval == 0) { break; // repeat } } for (Wrapper src : Wrapper.values()) { for (Wrapper dst : Wrapper.values()) { testConvert(src, dst, 0); } } }
Example #7
Source File: InvokerBytecodeGenerator.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private void emitI2X(Wrapper type) { switch (type) { case BYTE: mv.visitInsn(Opcodes.I2B); break; case SHORT: mv.visitInsn(Opcodes.I2S); break; case CHAR: mv.visitInsn(Opcodes.I2C); break; case INT: /* naught */ break; case LONG: mv.visitInsn(Opcodes.I2L); break; case FLOAT: mv.visitInsn(Opcodes.I2F); break; case DOUBLE: mv.visitInsn(Opcodes.I2D); break; case BOOLEAN: // For compatibility with ValueConversions and explicitCastArguments: mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IAND); break; default: throw new InternalError("unknown type: " + type); } }
Example #8
Source File: MethodHandles.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * Produces a method handle of the requested return type which returns the given * constant value every time it is invoked. * <p> * Before the method handle is returned, the passed-in value is converted to the requested type. * If the requested type is primitive, widening primitive conversions are attempted, * else reference conversions are attempted. * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}. * @param type the return type of the desired method handle * @param value the value to return * @return a method handle of the given return type and no arguments, which always returns the given value * @throws NullPointerException if the {@code type} argument is null * @throws ClassCastException if the value cannot be converted to the required return type * @throws IllegalArgumentException if the given type is {@code void.class} */ public static MethodHandle constant(Class<?> type, Object value) { if (type.isPrimitive()) { if (type == void.class) throw newIllegalArgumentException("void type"); Wrapper w = Wrapper.forPrimitiveType(type); value = w.convert(value, type); if (w.zero().equals(value)) return zero(w, type); return insertArguments(identity(type), 0, value); } else { if (value == null) return zero(Wrapper.OBJECT, type); return identity(type).bindTo(value); } }
Example #9
Source File: ExplicitCastArgumentsTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * Tests that non-null wrapper reference is successfully converted to * primitive types. */ public static void testRef2Prim() { for (Wrapper from : Wrapper.values()) { for (Wrapper to : Wrapper.values()) { if (from == Wrapper.VOID || to == Wrapper.VOID || to == Wrapper.OBJECT) { continue; } Object value = RANDOM_VALUES.get(from); for (TestConversionMode mode : TestConversionMode.values()) { if (from != Wrapper.OBJECT) { Object convValue = to.wrap(value); testConversion(mode, from.wrapperType(), to.primitiveType(), value, convValue, false, null); } else { testConversion(mode, from.wrapperType(), to.primitiveType(), value, null, true, ClassCastException.class); } } } } }
Example #10
Source File: Transformers.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
public static void spreadArray(int[] array, StackFrameWriter writer, MethodType type, int numArgs, int offset) { final Class<?>[] ptypes = type.ptypes(); for (int i = 0; i < numArgs; ++i) { Class<?> argumentType = ptypes[i + offset]; int j = array[i]; switch (Wrapper.basicTypeChar(argumentType)) { case 'L': { writer.putNextReference(j, argumentType); break; } case 'I': { writer.putNextInt(j); break; } case 'J': { writer.putNextLong(j); break; } case 'F': { writer.putNextFloat(j); break; } case 'D': { writer.putNextDouble(j); break; } default : { throw new AssertionError(); } } } }
Example #11
Source File: MethodHandles.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * Produces a method handle of the requested return type which returns the given * constant value every time it is invoked. * <p> * Before the method handle is returned, the passed-in value is converted to the requested type. * If the requested type is primitive, widening primitive conversions are attempted, * else reference conversions are attempted. * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}. * @param type the return type of the desired method handle * @param value the value to return * @return a method handle of the given return type and no arguments, which always returns the given value * @throws NullPointerException if the {@code type} argument is null * @throws ClassCastException if the value cannot be converted to the required return type * @throws IllegalArgumentException if the given type is {@code void.class} */ public static MethodHandle constant(Class<?> type, Object value) { if (type.isPrimitive()) { if (type == void.class) throw newIllegalArgumentException("void type"); Wrapper w = Wrapper.forPrimitiveType(type); value = w.convert(value, type); if (w.zero().equals(value)) return zero(w, type); return insertArguments(identity(type), 0, value); } else { if (value == null) return zero(Wrapper.OBJECT, type); return identity(type).bindTo(value); } }
Example #12
Source File: ExplicitCastArgumentsTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
/** * Tests that non-null wrapper reference is successfully converted to * primitive types. */ public static void testRef2Prim() { for (Wrapper from : Wrapper.values()) { for (Wrapper to : Wrapper.values()) { if (from == Wrapper.VOID || to == Wrapper.VOID || to == Wrapper.OBJECT) { continue; } Object value = RANDOM_VALUES.get(from); for (TestConversionMode mode : TestConversionMode.values()) { if (from != Wrapper.OBJECT) { Object convValue = to.wrap(value); testConversion(mode, from.wrapperType(), to.primitiveType(), value, convValue, false, null); } else { testConversion(mode, from.wrapperType(), to.primitiveType(), value, null, true, ClassCastException.class); } } } } }
Example #13
Source File: MethodHandles.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
/** * Produces a method handle of the requested return type which returns the given * constant value every time it is invoked. * <p> * Before the method handle is returned, the passed-in value is converted to the requested type. * If the requested type is primitive, widening primitive conversions are attempted, * else reference conversions are attempted. * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}. * @param type the return type of the desired method handle * @param value the value to return * @return a method handle of the given return type and no arguments, which always returns the given value * @throws NullPointerException if the {@code type} argument is null * @throws ClassCastException if the value cannot be converted to the required return type * @throws IllegalArgumentException if the given type is {@code void.class} */ public static MethodHandle constant(Class<?> type, Object value) { if (type.isPrimitive()) { if (type == void.class) throw newIllegalArgumentException("void type"); Wrapper w = Wrapper.forPrimitiveType(type); value = w.convert(value, type); if (w.zero().equals(value)) return zero(w, type); return insertArguments(identity(type), 0, value); } else { if (value == null) return zero(Wrapper.OBJECT, type); return identity(type).bindTo(value); } }
Example #14
Source File: Transformers.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
private static Object intArray(StackFrameReader reader, Class<?> ptypes[], int offset, int length) { int[] arityArray = new int[length]; for (int i = 0; i < length; ++i) { Class<?> argumentType = ptypes[i + offset]; switch (Wrapper.basicTypeChar(argumentType)) { case 'I': { arityArray[i] = reader.nextInt(); break; } case 'S': { arityArray[i] = reader.nextShort(); break; } case 'B': { arityArray[i] = reader.nextByte(); break; } default: { arityArray[i] = (Integer) reader.nextReference(argumentType); break; } } } return arityArray; }
Example #15
Source File: Transformers.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
private static Object referenceArray(StackFrameReader reader, Class<?>[] ptypes, Class<?> elementType, int offset, int length) { Object arityArray = Array.newInstance(elementType, length); for (int i = 0; i < length; ++i) { Class<?> argumentType = ptypes[i + offset]; Object o = null; switch (Wrapper.basicTypeChar(argumentType)) { case 'L': { o = reader.nextReference(argumentType); break; } case 'I': { o = reader.nextInt(); break; } case 'J': { o = reader.nextLong(); break; } case 'B': { o = reader.nextByte(); break; } case 'S': { o = reader.nextShort(); break; } case 'C': { o = reader.nextChar(); break; } case 'Z': { o = reader.nextBoolean(); break; } case 'F': { o = reader.nextFloat(); break; } case 'D': { o = reader.nextDouble(); break; } } Array.set(arityArray, i, elementType.cast(o)); } return arityArray; }
Example #16
Source File: InvokerBytecodeGenerator.java From Java8CN with Apache License 2.0 | 6 votes |
private void emitPushArgument(Class<?> ptype, Object arg) { BasicType bptype = basicType(ptype); if (arg instanceof Name) { Name n = (Name) arg; emitLoadInsn(n.type, n.index()); emitImplicitConversion(n.type, ptype, n); } else if ((arg == null || arg instanceof String) && bptype == L_TYPE) { emitConst(arg); } else { if (Wrapper.isWrapperType(arg.getClass()) && bptype != L_TYPE) { emitConst(arg); } else { mv.visitLdcInsn(constantPlaceholder(arg)); emitImplicitConversion(L_TYPE, ptype, arg); } } }
Example #17
Source File: Transformers.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
public static void spreadArray(char[] array, StackFrameWriter writer, MethodType type, int numArgs, int offset) { final Class<?>[] ptypes = type.ptypes(); for (int i = 0; i < numArgs; ++i) { Class<?> argumentType = ptypes[i + offset]; char c = array[i]; switch (Wrapper.basicTypeChar(argumentType)) { case 'L': { writer.putNextReference(c, argumentType); break; } case 'I': { writer.putNextInt(c); break; } case 'J': { writer.putNextLong(c); break; } case 'C': { writer.putNextChar(c); break; } case 'F': { writer.putNextFloat(c); break; } case 'D': { writer.putNextDouble(c); break; } default : { throw new AssertionError(); } } } }
Example #18
Source File: InvokerBytecodeGenerator.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private void emitPushArgument(Class<?> ptype, Object arg) { BasicType bptype = basicType(ptype); if (arg instanceof Name) { Name n = (Name) arg; emitLoadInsn(n.type, n.index()); emitImplicitConversion(n.type, ptype, n); } else if ((arg == null || arg instanceof String) && bptype == L_TYPE) { emitConst(arg); } else { if (Wrapper.isWrapperType(arg.getClass()) && bptype != L_TYPE) { emitConst(arg); } else { mv.visitLdcInsn(constantPlaceholder(arg)); emitImplicitConversion(L_TYPE, ptype, arg); } } }
Example #19
Source File: InvokerBytecodeGenerator.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
/** * Emit an unboxing call (plus preceding checkcast). * * @param wrapper wrapper type class to unbox. */ private void emitUnboxing(Wrapper wrapper) { String owner = "java/lang/" + wrapper.wrapperType().getSimpleName(); String name = wrapper.primitiveSimpleName() + "Value"; String desc = "()" + wrapper.basicTypeChar(); emitReferenceCast(wrapper.wrapperType(), null); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, desc, false); }
Example #20
Source File: DirectMethodHandle.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private static int ftypeKind(Class<?> ftype) { if (ftype.isPrimitive()) return Wrapper.forPrimitiveType(ftype).ordinal(); else if (VerifyType.isNullReferenceConversion(Object.class, ftype)) return FT_UNCHECKED_REF; else return FT_CHECKED_REF; }
Example #21
Source File: TypeConvertingMethodAdapter.java From JDKSourceCode1.8 with MIT License | 5 votes |
private Wrapper toWrapper(String desc) { char first = desc.charAt(0); if (first == '[' || first == '(') { first = 'L'; } return Wrapper.forBasicType(first); }
Example #22
Source File: ExplicitCastArgumentsTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Tests that primitive is successfully converted to wrapper reference * types, to the Number type (if possible) and to the Object type. */ public static void testPrim2Ref() { for (Wrapper from : Wrapper.values()) { for (Wrapper to : Wrapper.values()) { if (from == Wrapper.VOID || from == Wrapper.OBJECT || to == Wrapper.VOID || to == Wrapper.OBJECT) { continue; } Object value = RANDOM_VALUES.get(from); for (TestConversionMode mode : TestConversionMode.values()) { if (from == to) { testConversion(mode, from.primitiveType(), to.wrapperType(), value, value, false, null); } else { testConversion(mode, from.primitiveType(), to.wrapperType(), value, null, true, ClassCastException.class); } if (from != Wrapper.BOOLEAN && from != Wrapper.CHAR) { testConversion(mode, from.primitiveType(), Number.class, value, value, false, null); } else { testConversion(mode, from.primitiveType(), Number.class, value, null, true, ClassCastException.class); } testConversion(mode, from.primitiveType(), Object.class, value, value, false, null); } } } }
Example #23
Source File: ExplicitCastArgumentsTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
/** * Tests that primitive is successfully converted to wrapper reference * types, to the Number type (if possible) and to the Object type. */ public static void testPrim2Ref() { for (Wrapper from : Wrapper.values()) { for (Wrapper to : Wrapper.values()) { if (from == Wrapper.VOID || from == Wrapper.OBJECT || to == Wrapper.VOID || to == Wrapper.OBJECT) { continue; } Object value = RANDOM_VALUES.get(from); for (TestConversionMode mode : TestConversionMode.values()) { if (from == to) { testConversion(mode, from.primitiveType(), to.wrapperType(), value, value, false, null); } else { testConversion(mode, from.primitiveType(), to.wrapperType(), value, null, true, ClassCastException.class); } if (from != Wrapper.BOOLEAN && from != Wrapper.CHAR) { testConversion(mode, from.primitiveType(), Number.class, value, value, false, null); } else { testConversion(mode, from.primitiveType(), Number.class, value, null, true, ClassCastException.class); } testConversion(mode, from.primitiveType(), Object.class, value, value, false, null); } } } }
Example #24
Source File: InvokerBytecodeGenerator.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
/** * Emit a boxing call. * * @param wrapper primitive type class to box. */ private void emitBoxing(Wrapper wrapper) { String owner = "java/lang/" + wrapper.wrapperType().getSimpleName(); String name = "valueOf"; String desc = "(" + wrapper.basicTypeChar() + ")L" + owner + ";"; mv.visitMethodInsn(Opcodes.INVOKESTATIC, owner, name, desc, false); }
Example #25
Source File: TypeConvertingMethodAdapter.java From hottub with GNU General Public License v2.0 | 5 votes |
/** * Convert types by unboxing. The source type is known to be a primitive wrapper. * @param sname A primitive wrapper corresponding to wrapped reference source type * @param wt A primitive wrapper being converted to */ void unbox(String sname, Wrapper wt) { visitMethodInsn(Opcodes.INVOKEVIRTUAL, sname, unboxMethod(wt), unboxingDescriptor(wt), false); }
Example #26
Source File: InvokerBytecodeGenerator.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
/** * Emit a boxing call. * * @param type primitive type class to box. */ private void emitBoxing(Class<?> type) { Wrapper wrapper = Wrapper.forPrimitiveType(type); String owner = "java/lang/" + wrapper.wrapperType().getSimpleName(); String name = "valueOf"; String desc = "(" + wrapper.basicTypeChar() + ")L" + owner + ";"; mv.visitMethodInsn(Opcodes.INVOKESTATIC, owner, name, desc); }
Example #27
Source File: LambdaForm.java From JDKSourceCode1.8 with MIT License | 5 votes |
private static boolean checkInt(Class<?> type, Object x) { assert(x instanceof Integer); if (type == int.class) return true; Wrapper w = Wrapper.forBasicType(type); assert(w.isSubwordOrInt()); Object x1 = Wrapper.INT.wrap(w.wrap(x)); return x.equals(x1); }
Example #28
Source File: MethodHandles.java From JDKSourceCode1.8 with MIT License | 5 votes |
/** * Produces a method handle which returns its sole argument when invoked. * @param type the type of the sole parameter and return value of the desired method handle * @return a unary method handle which accepts and returns the given type * @throws NullPointerException if the argument is null * @throws IllegalArgumentException if the given type is {@code void.class} */ public static MethodHandle identity(Class<?> type) { Wrapper btw = (type.isPrimitive() ? Wrapper.forPrimitiveType(type) : Wrapper.OBJECT); int pos = btw.ordinal(); MethodHandle ident = IDENTITY_MHS[pos]; if (ident == null) { ident = setCachedMethodHandle(IDENTITY_MHS, pos, makeIdentity(btw.primitiveType())); } if (ident.type().returnType() == type) return ident; // something like identity(Foo.class); do not bother to intern these assert(btw == Wrapper.OBJECT); return makeIdentity(type); }
Example #29
Source File: MethodHandles.java From hottub with GNU General Public License v2.0 | 5 votes |
private static BoundMethodHandle insertArgumentPrimitive(BoundMethodHandle result, int pos, Class<?> ptype, Object value) { Wrapper w = Wrapper.forPrimitiveType(ptype); // perform unboxing and/or primitive conversion value = w.convert(value, ptype); switch (w) { case INT: return result.bindArgumentI(pos, (int)value); case LONG: return result.bindArgumentJ(pos, (long)value); case FLOAT: return result.bindArgumentF(pos, (float)value); case DOUBLE: return result.bindArgumentD(pos, (double)value); default: return result.bindArgumentI(pos, ValueConversions.widenSubword(value)); } }
Example #30
Source File: InvokerBytecodeGenerator.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
void emitNewArray(Name name) throws InternalError { Class<?> rtype = name.function.methodType().returnType(); if (name.arguments.length == 0) { // The array will be a constant. Object emptyArray; try { emptyArray = name.function.resolvedHandle.invoke(); } catch (Throwable ex) { throw newInternalError(ex); } assert(java.lang.reflect.Array.getLength(emptyArray) == 0); assert(emptyArray.getClass() == rtype); // exact typing mv.visitLdcInsn(constantPlaceholder(emptyArray)); emitReferenceCast(rtype, emptyArray); return; } Class<?> arrayElementType = rtype.getComponentType(); assert(arrayElementType != null); emitIconstInsn(name.arguments.length); int xas = Opcodes.AASTORE; if (!arrayElementType.isPrimitive()) { mv.visitTypeInsn(Opcodes.ANEWARRAY, getInternalName(arrayElementType)); } else { byte tc = arrayTypeCode(Wrapper.forPrimitiveType(arrayElementType)); xas = arrayInsnOpcode(tc, xas); mv.visitIntInsn(Opcodes.NEWARRAY, tc); } // store arguments for (int i = 0; i < name.arguments.length; i++) { mv.visitInsn(Opcodes.DUP); emitIconstInsn(i); emitPushArgument(name, i); mv.visitInsn(xas); } // the array is left on the stack assertStaticType(rtype, name); }