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

The following examples show how to use sun.invoke.util.Wrapper#isConvertibleFrom() . 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: ValueConversionsTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
Example 2
Source File: MethodType.java    From hottub 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 3
Source File: MethodType.java    From Java8CN 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 4
Source File: ValueConversionsTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
Example 5
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 6
Source File: ValueConversionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
Example 7
Source File: ValueConversionsTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst.primitiveType());
            else
                unboxer = ValueConversions.unbox(dst.primitiveType());
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case OBJECT:  result = (Object)  unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
                case VOID:    result = null;     unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
Example 8
Source File: MethodType.java    From openjdk-8-source 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 9
Source File: ValueConversionsTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
Example 10
Source File: MethodType.java    From openjdk-jdk8u-backup 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 11
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 12
Source File: MethodType.java    From openjdk-jdk8u 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 13
Source File: MethodType.java    From JDKSourceCode1.8 with MIT License 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: ValueConversionsTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
Example 15
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 16
Source File: ValueConversionsTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
Example 17
Source File: ValueConversionsTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
Example 18
Source File: ValueConversionsTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
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;
    }
}