本文整理汇总了Java中sun.invoke.util.Wrapper类的典型用法代码示例。如果您正苦于以下问题:Java Wrapper类的具体用法?Java Wrapper怎么用?Java Wrapper使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Wrapper类属于sun.invoke.util包,在下文中一共展示了Wrapper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: wrapperOrNullFromDescriptor
import sun.invoke.util.Wrapper; //导入依赖的package包/类
private Wrapper wrapperOrNullFromDescriptor(String desc) {
if (!desc.startsWith(WRAPPER_PREFIX)) {
// Not a class type (array or method), so not a boxed type
// or not in the right package
return null;
}
// Pare it down to the simple class name
String cname = desc.substring(WRAPPER_PREFIX.length(), desc.length() - 1);
// Hash to a Wrapper
Wrapper w = FROM_WRAPPER_NAME[hashWrapperName(cname)];
if (w == null || w.wrapperSimpleName().equals(cname)) {
return w;
} else {
return null;
}
}
示例2: emitImplicitConversion
import sun.invoke.util.Wrapper; //导入依赖的package包/类
/**
* Emit an implicit conversion for an argument which must be of the given pclass.
* This is usually a no-op, except when pclass is a subword type or a reference other than Object or an interface.
*
* @param ptype type of value present on stack
* @param pclass type of value required on stack
* @param arg compile-time representation of value on stack (Node, constant) or null if none
*/
private void emitImplicitConversion(BasicType ptype, Class<?> pclass, Object arg) {
assert(basicType(pclass) == ptype); // boxing/unboxing handled by caller
if (pclass == ptype.basicTypeClass() && ptype != L_TYPE)
return; // nothing to do
switch (ptype) {
case L_TYPE:
if (VerifyType.isNullConversion(Object.class, pclass, false)) {
if (PROFILE_LEVEL > 0)
emitReferenceCast(Object.class, arg);
return;
}
emitReferenceCast(pclass, arg);
return;
case I_TYPE:
if (!VerifyType.isNullConversion(int.class, pclass, false))
emitPrimCast(ptype.basicTypeWrapper(), Wrapper.forPrimitiveType(pclass));
return;
}
throw newInternalError("bad implicit conversion: tc="+ptype+": "+pclass);
}
示例3: constant
import sun.invoke.util.Wrapper; //导入依赖的package包/类
/**
* Produces a method handle of the requested return type which returns the given
* constant value every time it is invoked.
* <p>
* Before the method handle is returned, the passed-in value is converted to the requested type.
* If the requested type is primitive, widening primitive conversions are attempted,
* else reference conversions are attempted.
* <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}.
* @param type the return type of the desired method handle
* @param value the value to return
* @return a method handle of the given return type and no arguments, which always returns the given value
* @throws NullPointerException if the {@code type} argument is null
* @throws ClassCastException if the value cannot be converted to the required return type
* @throws IllegalArgumentException if the given type is {@code void.class}
*/
public static
MethodHandle constant(Class<?> type, Object value) {
if (type.isPrimitive()) {
if (type == void.class)
throw newIllegalArgumentException("void type");
Wrapper w = Wrapper.forPrimitiveType(type);
value = w.convert(value, type);
if (w.zero().equals(value))
return zero(w, type);
return insertArguments(identity(type), 0, value);
} else {
if (value == null)
return zero(Wrapper.OBJECT, type);
return identity(type).bindTo(value);
}
}
示例4: emitPushArgument
import sun.invoke.util.Wrapper; //导入依赖的package包/类
private void emitPushArgument(Class<?> ptype, Object arg) {
BasicType bptype = basicType(ptype);
if (arg instanceof Name) {
Name n = (Name) arg;
emitLoadInsn(n.type, n.index());
emitImplicitConversion(n.type, ptype, n);
} else if ((arg == null || arg instanceof String) && bptype == L_TYPE) {
emitConst(arg);
} else {
if (Wrapper.isWrapperType(arg.getClass()) && bptype != L_TYPE) {
emitConst(arg);
} else {
mv.visitLdcInsn(constantPlaceholder(arg));
emitImplicitConversion(L_TYPE, ptype, arg);
}
}
}
示例5: testBox
import sun.invoke.util.Wrapper; //导入依赖的package包/类
@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);
}
}
}
示例6: toArrayString
import sun.invoke.util.Wrapper; //导入依赖的package包/类
private static String toArrayString(Object a) {
if (a == null) return "null";
Class<?> elemType = a.getClass().getComponentType();
if (elemType == null) return a.toString();
if (elemType.isPrimitive()) {
switch (Wrapper.forPrimitiveType(elemType)) {
case INT: return Arrays.toString((int[])a);
case BYTE: return Arrays.toString((byte[])a);
case BOOLEAN: return Arrays.toString((boolean[])a);
case SHORT: return Arrays.toString((short[])a);
case CHAR: return Arrays.toString((char[])a);
case FLOAT: return Arrays.toString((float[])a);
case LONG: return Arrays.toString((long[])a);
case DOUBLE: return Arrays.toString((double[])a);
}
}
return Arrays.toString((Object[])a);
}
示例7: testNullRef2Prim
import sun.invoke.util.Wrapper; //导入依赖的package包/类
/**
* Tests that null wrapper reference is successfully converted to primitive
* types. Converted result should be zero for a primitive. Bug 8060483.
*/
public static void testNullRef2Prim() {
for (Wrapper from : Wrapper.values()) {
for (Wrapper to : Wrapper.values()) {
if (from == Wrapper.VOID || to == Wrapper.VOID) {
continue;
}
// MHs.eCA javadoc:
// If T0 is a reference and T1 a primitive, and if the reference
// is null at runtime, a zero value is introduced.
for (TestConversionMode mode : TestConversionMode.values()) {
testConversion(mode, from.wrapperType(),
to.primitiveType(), null, to.zero(), false, null);
}
}
}
}
示例8: testRef2Prim
import sun.invoke.util.Wrapper; //导入依赖的package包/类
/**
* Tests that non-null wrapper reference is successfully converted to
* primitive types.
*/
public static void testRef2Prim() {
for (Wrapper from : Wrapper.values()) {
for (Wrapper to : Wrapper.values()) {
if (from == Wrapper.VOID || to == Wrapper.VOID
|| to == Wrapper.OBJECT) {
continue;
}
Object value = RANDOM_VALUES.get(from);
for (TestConversionMode mode : TestConversionMode.values()) {
if (from != Wrapper.OBJECT) {
Object convValue = to.wrap(value);
testConversion(mode, from.wrapperType(),
to.primitiveType(), value, convValue, false, null);
} else {
testConversion(mode, from.wrapperType(),
to.primitiveType(), value, null,
true, ClassCastException.class);
}
}
}
}
}
示例9: testPrim2Prim
import sun.invoke.util.Wrapper; //导入依赖的package包/类
/**
* Tests that primitive is successfully converted to other primitive type.
*/
public static void testPrim2Prim() {
for (Wrapper from : Wrapper.values()) {
for (Wrapper to : Wrapper.values()) {
if (from == Wrapper.VOID || to == Wrapper.VOID
|| from == Wrapper.OBJECT || to == Wrapper.OBJECT) {
continue;
}
Object value = RANDOM_VALUES.get(from);
Object convValue = to.wrap(value);
for (TestConversionMode mode : TestConversionMode.values()) {
testConversion(mode, from.primitiveType(),
to.primitiveType(), value, convValue, false, null);
}
}
}
}
示例10: testConvert
import sun.invoke.util.Wrapper; //导入依赖的package包/类
@Test
public void testConvert() throws Throwable {
for (long tval = 0, ctr = 0;;) {
if (++ctr > 99999) throw new AssertionError("too many test values");
// prints 3776 test patterns (3776 = 8*59*8)
tval = nextTestValue(tval);
if (tval == 0) {
break; // repeat
}
}
for (Wrapper src : Wrapper.values()) {
for (Wrapper dst : Wrapper.values()) {
testConvert(src, dst, 0);
}
}
}
示例11: identity
import sun.invoke.util.Wrapper; //导入依赖的package包/类
/**
* Produces a method handle which returns its sole argument when invoked.
* @param type the type of the sole parameter and return value of the desired method handle
* @return a unary method handle which accepts and returns the given type
* @throws NullPointerException if the argument is null
* @throws IllegalArgumentException if the given type is {@code void.class}
*/
public static
MethodHandle identity(Class<?> type) {
Wrapper btw = (type.isPrimitive() ? Wrapper.forPrimitiveType(type) : Wrapper.OBJECT);
int pos = btw.ordinal();
MethodHandle ident = IDENTITY_MHS[pos];
if (ident == null) {
ident = setCachedMethodHandle(IDENTITY_MHS, pos, makeIdentity(btw.primitiveType()));
}
if (ident.type().returnType() == type)
return ident;
// something like identity(Foo.class); do not bother to intern these
assert (btw == Wrapper.OBJECT);
return makeIdentity(type);
}
示例12: widen
import sun.invoke.util.Wrapper; //导入依赖的package包/类
void widen(Wrapper ws, Wrapper wt) {
if (ws != wt) {
int opcode = wideningOpcodes[ws.ordinal()][wt.ordinal()];
if (opcode != Opcodes.NOP) {
visitInsn(opcode);
}
}
}
示例13: testReturnAny2Void
import sun.invoke.util.Wrapper; //导入依赖的package包/类
/**
* Tests that non-null any return is successfully converted to non-type
* void.
*/
public static void testReturnAny2Void() {
for (Wrapper from : Wrapper.values()) {
testConversion(TestConversionMode.RETURN_VALUE, from.wrapperType(),
void.class, RANDOM_VALUES.get(from),
null, false, null);
testConversion(TestConversionMode.RETURN_VALUE, from.primitiveType(),
void.class, RANDOM_VALUES.get(from),
null, false, null);
}
}
示例14: toWrapper
import sun.invoke.util.Wrapper; //导入依赖的package包/类
private Wrapper toWrapper(String desc) {
char first = desc.charAt(0);
if (first == '[' || first == '(') {
first = 'L';
}
return Wrapper.forBasicType(first);
}
示例15: ftypeKind
import sun.invoke.util.Wrapper; //导入依赖的package包/类
static int ftypeKind(Class<?> ftype) {
if (ftype.isPrimitive())
return Wrapper.forPrimitiveType(ftype).ordinal();
else if (VerifyType.isNullReferenceConversion(Object.class, ftype))
return FT_UNCHECKED_REF;
else
return FT_CHECKED_REF;
}