sun.invoke.util.ValueConversions Java Examples

The following examples show how to use sun.invoke.util.ValueConversions. 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: BoundMethodHandle.java    From openjdk-8 with 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 #2
Source File: ValueConversionsTest.java    From jdk8u_jdk with 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 #3
Source File: ValueConversionsTest.java    From jdk8u-dev-jdk with 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 #4
Source File: BoundMethodHandle.java    From dragonwell8_jdk with 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 #5
Source File: ValueConversionsTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@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 #6
Source File: ValueConversionsTest.java    From dragonwell8_jdk with 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 #7
Source File: ValueConversionsTest.java    From hottub with 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 #8
Source File: ValueConversionsTest.java    From hottub with 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 #9
Source File: ValueConversionsTest.java    From TencentKona-8 with 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 #10
Source File: ValueConversionsTest.java    From TencentKona-8 with 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 #11
Source File: BoundMethodHandle.java    From Java8CN with 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 #12
Source File: ValueConversionsTest.java    From jdk8u_jdk with 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 #13
Source File: BoundMethodHandle.java    From jdk8u60 with 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 #14
Source File: ValueConversionsTest.java    From jdk8u-jdk with 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 #15
Source File: BoundMethodHandle.java    From jdk1.8-source-analysis with 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 #16
Source File: ValueConversionsTest.java    From openjdk-jdk8u-backup with 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 #17
Source File: BoundMethodHandle.java    From openjdk-jdk8u-backup with 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 #18
Source File: BoundMethodHandle.java    From Bytecoder with 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 #19
Source File: ValueConversionsTest.java    From openjdk-jdk8u with 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 #20
Source File: BoundMethodHandle.java    From jdk-1.7-annotated with 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 #21
Source File: ValueConversionsTest.java    From openjdk-jdk9 with 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 #22
Source File: BoundMethodHandle.java    From openjdk-jdk8u with 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 #23
Source File: ValueConversionsTest.java    From openjdk-jdk9 with 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 #24
Source File: ValueConversionsTest.java    From jdk8u-jdk with 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 #25
Source File: BoundMethodHandle.java    From jdk8u-jdk with 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 #26
Source File: ValueConversionsTest.java    From jdk8u-jdk with 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 File: MethodHandleImpl.java    From jdk1.8-source-analysis with 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
Source File: DirectMethodHandle.java    From openjdk-jdk9 with GNU General Public License v2.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 #29
Source File: ValueConversionsTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable {
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    boolean testSingleCase = (tval != 0);
    final long tvalInit = tval;
    MethodHandle conv = ValueConversions.convertPrimitive(src, dst);
    MethodType convType = MethodType.methodType(dst.primitiveType(), src.primitiveType());
    assertEquals(convType, conv.type());
    MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class));
    for (;;) {
        long n = tval;
        Object testValue = src.wrap(n);
        Object expResult = dst.cast(testValue, dst.primitiveType());
        Object result;
        switch (src) {
            case INT:     result = converter.invokeExact((int)n); break;
            case LONG:    result = converter.invokeExact(/*long*/n); break;
            case FLOAT:   result = converter.invokeExact((float)n); break;
            case DOUBLE:  result = converter.invokeExact((double)n); break;
            case CHAR:    result = converter.invokeExact((char)n); break;
            case BYTE:    result = converter.invokeExact((byte)n); break;
            case SHORT:   result = converter.invokeExact((short)n); break;
            case BOOLEAN: result = converter.invokeExact((n & 1) != 0); break;
            default:  throw new AssertionError();
        }
        assertEquals("(src,dst,n,testValue)="+Arrays.asList(src,dst,"0x"+Long.toHexString(n),testValue),
                     expResult, result);
        if (testSingleCase)  break;
        // next test value:
        tval = nextTestValue(tval);
        if (tval == tvalInit)  break;  // repeat
    }
}
 
Example #30
Source File: ValueConversionsTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testBox() throws Throwable {
    //System.out.println("box");
    for (Wrapper w : Wrapper.values()) {
        if (w == Wrapper.VOID)  continue;  // skip this; no unboxed form
        //System.out.println(w);
        for (int n = -5; n < 10; n++) {
            Object box = w.wrap(n);
            MethodHandle boxer = ValueConversions.box(w.primitiveType());
            Object expResult = box;
            Object result = null;
            switch (w) {
                case INT:     result = boxer.invokeExact(/*int*/n); break;
                case LONG:    result = boxer.invokeExact((long)n); break;
                case FLOAT:   result = boxer.invokeExact((float)n); break;
                case DOUBLE:  result = boxer.invokeExact((double)n); break;
                case CHAR:    result = boxer.invokeExact((char)n); break;
                case BYTE:    result = boxer.invokeExact((byte)n); break;
                case SHORT:   result = boxer.invokeExact((short)n); break;
                case OBJECT:  result = boxer.invokeExact((Object)n); break;
                case BOOLEAN: result = boxer.invokeExact((n & 1) != 0); break;
            }
            assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),
                         expResult, result);
        }
    }
}