Java Code Examples for sun.invoke.util.Wrapper#isWrapperType()

The following examples show how to use sun.invoke.util.Wrapper#isWrapperType() . 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: InvokerBytecodeGenerator.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
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 2
Source File: InvokerBytecodeGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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 dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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 jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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 5
Source File: InvokerBytecodeGenerator.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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 6
Source File: InvokerBytecodeGenerator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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 7
Source File: InvokerBytecodeGenerator.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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 8
Source File: InvokerBytecodeGenerator.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
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 9
Source File: InvokerBytecodeGenerator.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void emitPushArgument(Name name, int paramIndex) {
    Object arg = name.arguments[paramIndex];
    char ptype = name.function.parameterType(paramIndex);
    MethodType mtype = name.function.methodType();
    if (arg instanceof Name) {
        Name n = (Name) arg;
        emitLoadInsn(n.type, n.index());
        emitImplicitConversion(n.type, mtype.parameterType(paramIndex));
    } else if ((arg == null || arg instanceof String) && ptype == 'L') {
        emitConst(arg);
    } else {
        if (Wrapper.isWrapperType(arg.getClass()) && ptype != 'L') {
            emitConst(arg);
        } else {
            mv.visitLdcInsn(constantPlaceholder(arg));
            emitImplicitConversion('L', mtype.parameterType(paramIndex));
        }
    }
}
 
Example 10
Source File: InvokerBytecodeGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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 11
Source File: InvokerBytecodeGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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 12
Source File: InvokerBytecodeGenerator.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private void emitPushArgument(Name name, int paramIndex) {
    Object arg = name.arguments[paramIndex];
    char ptype = name.function.parameterType(paramIndex);
    MethodType mtype = name.function.methodType();
    if (arg instanceof Name) {
        Name n = (Name) arg;
        emitLoadInsn(n.type, n.index());
        emitImplicitConversion(n.type, mtype.parameterType(paramIndex));
    } else if ((arg == null || arg instanceof String) && ptype == 'L') {
        emitConst(arg);
    } else {
        if (Wrapper.isWrapperType(arg.getClass()) && ptype != 'L') {
            emitConst(arg);
        } else {
            mv.visitLdcInsn(constantPlaceholder(arg));
            emitImplicitConversion('L', mtype.parameterType(paramIndex));
        }
    }
}
 
Example 13
Source File: MethodType.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || src == Object.class || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}
 
Example 14
Source File: MethodType.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || src == Object.class || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}
 
Example 15
Source File: MethodType.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || src == Object.class || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}
 
Example 16
Source File: MethodType.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || src == Object.class || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}
 
Example 17
Source File: MethodType.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}
 
Example 18
Source File: MethodType.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || src == Object.class || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}
 
Example 19
Source File: MethodType.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || src == Object.class || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}
 
Example 20
Source File: MethodType.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || src == Object.class || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}