Java Code Examples for sun.invoke.util.ValueConversions

The following examples show how to use sun.invoke.util.ValueConversions. These examples are extracted from open source projects. 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 Project: jdk1.8-source-analysis   Source File: BoundMethodHandle.java    License: Apache License 2.0 6 votes vote down vote up
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case L_TYPE:
            return bindSingle(type, form, x);  // Use known fast path.
        case I_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
        case J_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
        case F_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor().invokeBasic(type, form, (float) x);
        case D_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor().invokeBasic(type, form, (double) x);
        default : throw newInternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw newInternalError(t);
    }
}
 
Example 2
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case L_TYPE:
            return bindSingle(type, form, x);  // Use known fast path.
        case I_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
        case J_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
        case F_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor().invokeBasic(type, form, (float) x);
        case D_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor().invokeBasic(type, form, (double) x);
        default : throw newInternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw newInternalError(t);
    }
}
 
Example 3
@Test
public void testCast() throws Throwable {
    //System.out.println("cast");
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast(dst);
        assertEquals(caster.type(), ValueConversions.identity().type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast = dst.isAssignableFrom(src);
            //System.out.println("obj="+obj+" <: dst="+dst+(canCast ? " (OK)" : " (will fail)"));
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
Example 4
@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 5
@Test
public void testCast() throws Throwable {
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast().bindTo(dst);
        assertEquals(caster.type(), MethodHandles.identity(Object.class).type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast = dst.isAssignableFrom(src);
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
Example 6
@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 7
@Test
public void testCast() throws Throwable {
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast().bindTo(dst);
        assertEquals(caster.type(), MethodHandles.identity(Object.class).type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast = dst.isAssignableFrom(src);
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
Example 8
@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 9
Source Project: jdk8u60   Source File: BoundMethodHandle.java    License: GNU General Public License v2.0 6 votes vote down vote up
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case L_TYPE:
            return bindSingle(type, form, x);  // Use known fast path.
        case I_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
        case J_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
        case F_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor().invokeBasic(type, form, (float) x);
        case D_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor().invokeBasic(type, form, (double) x);
        default : throw newInternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw newInternalError(t);
    }
}
 
Example 10
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case L_TYPE:
            return bindSingle(type, form, x);  // Use known fast path.
        case I_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
        case J_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
        case F_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor().invokeBasic(type, form, (float) x);
        case D_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor().invokeBasic(type, form, (double) x);
        default : throw newInternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw newInternalError(t);
    }
}
 
Example 11
@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 12
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case L_TYPE:
            return bindSingle(type, form, x);  // Use known fast path.
        case I_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
        case J_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
        case F_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor().invokeBasic(type, form, (float) x);
        case D_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor().invokeBasic(type, form, (double) x);
        default : throw newInternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw newInternalError(t);
    }
}
 
Example 13
Source Project: openjdk-8   Source File: BoundMethodHandle.java    License: GNU General Public License v2.0 6 votes vote down vote up
static MethodHandle bindSingle(MethodType type, LambdaForm form, char xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case 'L':
            if (true)  return bindSingle(type, form, x);  // Use known fast path.
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('L').constructor[0].invokeBasic(type, form, x);
        case 'I':
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('I').constructor[0].invokeBasic(type, form, ValueConversions.widenSubword(x));
        case 'J':
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('J').constructor[0].invokeBasic(type, form, (long) x);
        case 'F':
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('F').constructor[0].invokeBasic(type, form, (float) x);
        case 'D':
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('D').constructor[0].invokeBasic(type, form, (double) x);
        default : throw new InternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw newInternalError(t);
    }
}
 
Example 14
@Test
public void testCast() throws Throwable {
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast().bindTo(dst);
        assertEquals(caster.type(), MethodHandles.identity(Object.class).type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast = dst.isAssignableFrom(src);
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
Example 15
Source Project: Bytecoder   Source File: BoundMethodHandle.java    License: Apache License 2.0 6 votes vote down vote up
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case L_TYPE:
            return bindSingle(type, form, x);  // Use known fast path.
        case I_TYPE:
            return (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(I_TYPE_NUM).factory().invokeBasic(type, form, ValueConversions.widenSubword(x));
        case J_TYPE:
            return (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(J_TYPE_NUM).factory().invokeBasic(type, form, (long) x);
        case F_TYPE:
            return (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(F_TYPE_NUM).factory().invokeBasic(type, form, (float) x);
        case D_TYPE:
            return (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(D_TYPE_NUM).factory().invokeBasic(type, form, (double) x);
        default : throw newInternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw uncaughtException(t);
    }
}
 
Example 16
Source Project: jdk-1.7-annotated   Source File: BoundMethodHandle.java    License: Apache License 2.0 6 votes vote down vote up
static MethodHandle bindSingle(MethodType type, LambdaForm form, char xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case 'L':
            if (true)  return bindSingle(type, form, x);  // Use known fast path.
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('L').constructor[0].invokeBasic(type, form, x);
        case 'I':
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('I').constructor[0].invokeBasic(type, form, ValueConversions.widenSubword(x));
        case 'J':
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('J').constructor[0].invokeBasic(type, form, (long) x);
        case 'F':
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('F').constructor[0].invokeBasic(type, form, (float) x);
        case 'D':
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('D').constructor[0].invokeBasic(type, form, (double) x);
        default : throw new InternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw newInternalError(t);
    }
}
 
Example 17
@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 18
@Test
public void testCast() throws Throwable {
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast().bindTo(dst);
        assertEquals(caster.type(), MethodHandles.identity(Object.class).type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast = dst.isAssignableFrom(src);
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
Example 19
@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 20
Source Project: jdk8u-jdk   Source File: BoundMethodHandle.java    License: GNU General Public License v2.0 6 votes vote down vote up
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case L_TYPE:
            return bindSingle(type, form, x);  // Use known fast path.
        case I_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
        case J_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
        case F_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor().invokeBasic(type, form, (float) x);
        case D_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor().invokeBasic(type, form, (double) x);
        default : throw newInternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw newInternalError(t);
    }
}
 
Example 21
@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 22
@Test
public void testCast() throws Throwable {
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast().bindTo(dst);
        assertEquals(caster.type(), MethodHandles.identity(Object.class).type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast = dst.isAssignableFrom(src);
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
Example 23
Source Project: Java8CN   Source File: BoundMethodHandle.java    License: Apache License 2.0 6 votes vote down vote up
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    // for some type signatures, there exist pre-defined concrete BMH classes
    try {
        switch (xtype) {
        case L_TYPE:
            return bindSingle(type, form, x);  // Use known fast path.
        case I_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
        case J_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
        case F_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor().invokeBasic(type, form, (float) x);
        case D_TYPE:
            return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor().invokeBasic(type, form, (double) x);
        default : throw newInternalError("unexpected xtype: " + xtype);
        }
    } catch (Throwable t) {
        throw newInternalError(t);
    }
}
 
Example 24
@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 25
Source Project: hottub   Source File: ValueConversionsTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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 26
Source Project: hottub   Source File: ValueConversionsTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCast() throws Throwable {
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast().bindTo(dst);
        assertEquals(caster.type(), MethodHandles.identity(Object.class).type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast = dst.isAssignableFrom(src);
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
Example 27
Source Project: jdk1.8-source-analysis   Source File: MethodHandleImpl.java    License: Apache License 2.0 5 votes vote down vote up
static
MethodHandle makeGuardWithCatch(MethodHandle target,
                                Class<? extends Throwable> exType,
                                MethodHandle catcher) {
    MethodType type = target.type();
    LambdaForm form = makeGuardWithCatchForm(type.basicType());

    // Prepare auxiliary method handles used during LambdaForm interpreation.
    // Box arguments and wrap them into Object[]: ValueConversions.array().
    MethodType varargsType = type.changeReturnType(Object[].class);
    MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
    // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore().
    MethodHandle unboxResult;
    Class<?> rtype = type.returnType();
    if (rtype.isPrimitive()) {
        if (rtype == void.class) {
            unboxResult = ValueConversions.ignore();
        } else {
            Wrapper w = Wrapper.forPrimitiveType(type.returnType());
            unboxResult = ValueConversions.unboxExact(w);
        }
    } else {
        unboxResult = MethodHandles.identity(Object.class);
    }

    BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
    BoundMethodHandle mh;
    try {
        mh = (BoundMethodHandle)
                data.constructor().invokeBasic(type, form, (Object) target, (Object) exType, (Object) catcher,
                                               (Object) collectArgs, (Object) unboxResult);
    } catch (Throwable ex) {
        throw uncaughtException(ex);
    }
    assert(mh.type() == type);
    return mh;
}
 
Example 28
static
boolean shouldBeInitialized(MemberName member) {
    switch (member.getReferenceKind()) {
    case REF_invokeStatic:
    case REF_getStatic:
    case REF_putStatic:
    case REF_newInvokeSpecial:
        break;
    default:
        // No need to initialize the class on this kind of member.
        return false;
    }
    Class<?> cls = member.getDeclaringClass();
    if (cls == ValueConversions.class ||
        cls == MethodHandleImpl.class ||
        cls == Invokers.class) {
        // These guys have lots of <clinit> DMH creation but we know
        // the MHs will not be used until the system is booted.
        return false;
    }
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls) ||
        VerifyAccess.isSamePackage(ValueConversions.class, cls)) {
        // It is a system class.  It is probably in the process of
        // being initialized, but we will help it along just to be safe.
        if (UNSAFE.shouldBeInitialized(cls)) {
            UNSAFE.ensureClassInitialized(cls);
        }
        return false;
    }
    return UNSAFE.shouldBeInitialized(cls);
}
 
Example 29
Source Project: jdk1.8-source-analysis   Source File: DirectMethodHandle.java    License: Apache License 2.0 5 votes vote down vote up
static
boolean shouldBeInitialized(MemberName member) {
    switch (member.getReferenceKind()) {
    case REF_invokeStatic:
    case REF_getStatic:
    case REF_putStatic:
    case REF_newInvokeSpecial:
        break;
    default:
        // No need to initialize the class on this kind of member.
        return false;
    }
    Class<?> cls = member.getDeclaringClass();
    if (cls == ValueConversions.class ||
        cls == MethodHandleImpl.class ||
        cls == Invokers.class) {
        // These guys have lots of <clinit> DMH creation but we know
        // the MHs will not be used until the system is booted.
        return false;
    }
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls) ||
        VerifyAccess.isSamePackage(ValueConversions.class, cls)) {
        // It is a system class.  It is probably in the process of
        // being initialized, but we will help it along just to be safe.
        if (UNSAFE.shouldBeInitialized(cls)) {
            UNSAFE.ensureClassInitialized(cls);
        }
        return false;
    }
    return UNSAFE.shouldBeInitialized(cls);
}
 
Example 30
static
MethodHandle makeGuardWithCatch(MethodHandle target,
                                Class<? extends Throwable> exType,
                                MethodHandle catcher) {
    MethodType type = target.type();
    LambdaForm form = makeGuardWithCatchForm(type.basicType());

    // Prepare auxiliary method handles used during LambdaForm interpreation.
    // Box arguments and wrap them into Object[]: ValueConversions.array().
    MethodType varargsType = type.changeReturnType(Object[].class);
    MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
    // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore().
    MethodHandle unboxResult;
    Class<?> rtype = type.returnType();
    if (rtype.isPrimitive()) {
        if (rtype == void.class) {
            unboxResult = ValueConversions.ignore();
        } else {
            Wrapper w = Wrapper.forPrimitiveType(type.returnType());
            unboxResult = ValueConversions.unboxExact(w);
        }
    } else {
        unboxResult = MethodHandles.identity(Object.class);
    }

    BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
    BoundMethodHandle mh;
    try {
        mh = (BoundMethodHandle)
                data.constructor().invokeBasic(type, form, (Object) target, (Object) exType, (Object) catcher,
                                               (Object) collectArgs, (Object) unboxResult);
    } catch (Throwable ex) {
        throw uncaughtException(ex);
    }
    assert(mh.type() == type);
    return mh;
}