Example usage for org.objectweb.asm.tree MethodNode visitInsn

List of usage examples for org.objectweb.asm.tree MethodNode visitInsn

Introduction

In this page you can find the example usage for org.objectweb.asm.tree MethodNode visitInsn.

Prototype

@Override
    public void visitInsn(final int opcode) 

Source Link

Usage

From source file:org.lenskit.data.entities.AbstractBeanEntityBuilder.java

License:Open Source License

private static MethodNode generateClearer(Class<? extends AbstractBeanEntityBuilder> type, Method smethod,
        Method cmethod) {// w  w w.  j  ava  2 s . c om
    MethodNode clearer = new MethodNode();
    clearer.access = ACC_PUBLIC;
    clearer.desc = getMethodDescriptor(VOID_TYPE, getType(AbstractBeanEntityBuilder.class));
    clearer.name = "clear";
    clearer.exceptions = Collections.emptyList();
    clearer.maxLocals = 2;
    clearer.maxStack = 1;
    if (cmethod != null) {
        // load target object
        clearer.visitVarInsn(ALOAD, 1);
        // cast to target object type
        clearer.visitTypeInsn(CHECKCAST, getInternalName(type));
        // call clearer method
        clearer.visitMethodInsn(INVOKEVIRTUAL, getInternalName(type), cmethod.getName(),
                getMethodDescriptor(cmethod), false);
        clearer.visitInsn(RETURN);
    } else if (!smethod.getParameterTypes()[0].isPrimitive()) {
        // load target object & cast to type
        clearer.visitVarInsn(ALOAD, 1);
        clearer.visitTypeInsn(CHECKCAST, getInternalName(type));
        // load null and call setter
        clearer.visitInsn(ACONST_NULL);
        clearer.maxStack = 2;
        clearer.visitMethodInsn(INVOKEVIRTUAL, getInternalName(type), smethod.getName(),
                getMethodDescriptor(smethod), false);
        clearer.visitInsn(RETURN);
    } else {
        // no clearer and primitive method, throw unsupported operation exception
        clearer.maxStack = 2;
        clearer.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException");
        clearer.visitInsn(DUP);
        clearer.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "()V",
                false);
        clearer.visitInsn(ATHROW);
    }
    return clearer;
}