Example usage for org.objectweb.asm Opcodes CHECKCAST

List of usage examples for org.objectweb.asm Opcodes CHECKCAST

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes CHECKCAST.

Prototype

int CHECKCAST

To view the source code for org.objectweb.asm Opcodes CHECKCAST.

Click Source Link

Usage

From source file:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList unboxShort() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "shortValue", "()S"));
    return il;//from   w w w.j a v  a  2  s .  c o  m
}

From source file:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList unboxInt() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "intValue", "()I"));
    return il;/* ww  w.  j a  v  a  2 s  .  com*/
}

From source file:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList unboxLong() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "longValue", "()J"));
    return il;/*from w  w  w  .ja  va2  s.com*/
}

From source file:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList unboxFloat() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "floatValue", "()F"));
    return il;//from w ww  .j  a  va  2 s  .c om
}

From source file:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList unboxDouble() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "doubleValue", "()D"));
    return il;//from w ww  .j  av a  2s  . c om
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionFieldGenerator.java

License:Apache License

private static void createGetMethod(ClassVisitor cw, Class entryClass, Field field) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "get", "(Ljava/lang/Object;)Ljava/lang/Object;", null,
            new String[] { "java/lang/IllegalArgumentException", "java/lang/IllegalAccessException" });
    mv.visitCode();/*from w w  w.ja  va2  s  .c  o  m*/
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(entryClass));
    mv.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(entryClass), field.getName(),
            Type.getDescriptor(field.getType()));
    boxIfNeeded(mv, field);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionFieldGenerator.java

License:Apache License

private static void createSetMethod(ClassVisitor cw, Class entryClass, Field field) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "set", "(Ljava/lang/Object;Ljava/lang/Object;)V",
            null, new String[] { "java/lang/IllegalArgumentException", "java/lang/IllegalAccessException" });
    mv.visitCode();/*from  ww w  .  ja va  2  s.com*/
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(entryClass));
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    castAndUnboxIfNeeded(mv, field);
    mv.visitFieldInsn(Opcodes.PUTFIELD, Type.getInternalName(entryClass), field.getName(),
            Type.getDescriptor(field.getType()));
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 3);
    mv.visitEnd();
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionFieldGenerator.java

License:Apache License

private static void castAndUnboxIfNeeded(MethodVisitor mv, Field field) {
    if (!field.getType().isPrimitive()) {
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(field.getType()));
        return;/*w  w w. j a  va  2  s .co  m*/
    }

    Type type = Type.getType(field.getType());
    switch (type.getSort()) {
    case Type.BOOLEAN:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Boolean");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
        break;
    case Type.INT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Integer");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
        break;
    case Type.SHORT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Short");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
        break;
    case Type.LONG:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Long");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
        break;
    case Type.FLOAT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Float");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
        break;
    case Type.DOUBLE:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Double");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
        break;
    case Type.BYTE:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Byte");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
        break;
    case Type.CHAR:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Character");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
        break;
    }
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionMethodGenerator.java

License:Apache License

/**
 * Creates the method invoking wrapper method.
 *//*from  ww  w. j a va2 s  .  c  om*/
private static void createMethod(Class clazz, String name, Method refMethod, ClassWriter cw, String methodName,
        String desc, boolean argsParams, boolean returnValue, Class... parameterTypes) {

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS, methodName, desc, null, null);

    boolean isStatic = Modifier.isStatic(refMethod.getModifiers());
    boolean isInteface = Modifier.isInterface(refMethod.getDeclaringClass().getModifiers());

    final int invokeCode;
    if (isStatic) {
        invokeCode = Opcodes.INVOKESTATIC;
    } else {
        invokeCode = isInteface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(clazz));
    }

    if (argsParams) {
        for (int i = 0; i < parameterTypes.length; ++i) {
            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitIntInsn(Opcodes.BIPUSH, i);
            mv.visitInsn(Opcodes.AALOAD);
            prepareParameter(mv, Type.getType(parameterTypes[i]));
        }
    } else {
        for (int i = 0; i < parameterTypes.length; ++i) {
            mv.visitVarInsn(Opcodes.ALOAD, i + 2);
            prepareParameter(mv, Type.getType(parameterTypes[i]));
        }
    }

    mv.visitMethodInsn(invokeCode, Type.getInternalName(clazz), name, Type.getMethodDescriptor(refMethod));

    if (returnValue) {
        prepareResult(mv, refMethod);
        mv.visitInsn(Opcodes.ARETURN);
    } else {
        mv.visitInsn(Opcodes.RETURN);
    }

    mv.visitMaxs(1, 1); // ignored since ClassWriter set as ClassWriter.COMPUTE_MAXS
    mv.visitEnd();
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionMethodGenerator.java

License:Apache License

/**
 * Unbox the input parameters//from   w w  w . java2 s  .co  m
 */
private static void prepareParameter(MethodVisitor mv, Type type) {

    switch (type.getSort()) {
    case Type.BOOLEAN:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Boolean");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
        break;
    case Type.BYTE:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Byte");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
        break;
    case Type.CHAR:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Character");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
        break;
    case Type.SHORT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Short");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
        break;
    case Type.INT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Integer");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
        break;
    case Type.LONG:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Long");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
        break;
    case Type.FLOAT:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Float");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
        break;
    case Type.DOUBLE:
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Double");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
        break;
    default:
        mv.visitTypeInsn(Opcodes.CHECKCAST, type.getInternalName());
        break;
    }
}