Example usage for org.objectweb.asm.util CheckClassAdapter visitMethod

List of usage examples for org.objectweb.asm.util CheckClassAdapter visitMethod

Introduction

In this page you can find the example usage for org.objectweb.asm.util CheckClassAdapter visitMethod.

Prototype

@Override
    public MethodVisitor visitMethod(final int access, final String name, final String descriptor,
            final String signature, final String[] exceptions) 

Source Link

Usage

From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.ByteCodeMethodVisitor.java

License:Open Source License

public void toAsm(CheckClassAdapter cca) {
    MethodVisitor mv = cca.visitMethod(access, name, desc, sig, exceptions);
    for (Insn i : insns) {
        i.toAsmWrapper(mv);//  ww w.ja v  a 2  s  .com
    }
}

From source file:org.spongepowered.mod.asm.util.ASMEventListenerFactory.java

License:MIT License

@SuppressWarnings("unchecked")
private static <T> Class<T> createClass(Class<T> interf, Method input, Method output) {

    String className = getClassName(interf, input, output);

    ClassWriter cwBase = new ClassWriter(0);
    CheckClassAdapter cw = new CheckClassAdapter(cwBase);

    MethodVisitor mv;// w w w  .  ja v  a 2  s.c o  m

    String classNameDesc = className.replace('.', '/');

    String interfaceInternalName = Type.getInternalName(interf);

    String inputName = input.getName();
    String inputMethodDescriptor = Type.getMethodDescriptor(input);

    String outputParameterTypeIntName = Type.getInternalName(output.getParameterTypes()[0]);
    String outputTargetTypeIntName = Type.getInternalName(output.getDeclaringClass());
    String outputMethodDescriptor = Type.getMethodDescriptor(output);
    String outputName = output.getName();

    boolean isOutputInterface = output.getDeclaringClass().isInterface();

    // A new class of the following form is created, with a unique name
    //
    // package org.spongepowered.mod.asm;
    // public class <className> extends java.lang.Object implements <interf>
    //
    //     private final Object target
    //
    //     public <className> (java.lang.Object target) {
    //         super();
    //         this.target = target;
    //         return;
    //     }
    //
    //     public void <inputMethod> (<inputMethodType event) {
    //         ((outputTargetType) this.target).outputMethod((outputParameteType) event);
    //         return
    //     }
    // }

    // package org.spongepowered.mod.asm;
    // public class <className> extends java.lang.Object implements <interf>
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, classNameDesc, null, "java/lang/Object",
            new String[] { interfaceInternalName });

    // private final Object target
    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "target", "Ljava/lang/Object;", null, null);

    // Constructor

    // public UniqueClass (java.lang.Object target) {
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null);
    mv.visitCode();

    // super();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);

    // this.target = target;
    mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this
    mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads target (from input)
    mv.visitFieldInsn(Opcodes.PUTFIELD, classNameDesc, "target", "Ljava/lang/Object;");

    // return;
    mv.visitInsn(Opcodes.RETURN);

    // }
    // 2 localvars due to inputs: this, target
    // 2 items on stack after double ALOAD
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    // Callback method

    // public void <inputMethod> (<inputMethodType event) {
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, inputName, inputMethodDescriptor, null, null);
    mv.visitCode();

    // push((casted) this.target)
    mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this
    mv.visitFieldInsn(Opcodes.GETFIELD, classNameDesc, "target", "Ljava/lang/Object;");
    mv.visitTypeInsn(Opcodes.CHECKCAST, outputTargetTypeIntName);

    // push((casted) event)
    mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads method parameter 0
    mv.visitTypeInsn(Opcodes.CHECKCAST, outputParameterTypeIntName);

    // ((outputTargetType) this.target).outputMethod((outputParameteType) event);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, outputTargetTypeIntName, outputName, outputMethodDescriptor,
            isOutputInterface);

    // return
    mv.visitInsn(Opcodes.RETURN);

    // }
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    cw.visitEnd();

    byte[] bytes = cwBase.toByteArray();

    return (Class<T>) loader.defineClass(className, bytes);
}

From source file:org.spongepowered.mod.asm.util.ASMEventListenerHolderFactory.java

License:MIT License

public static Class<?> getClass(Class<?> eventClass, EventPriority priority, boolean canceled) {
    ClassWriter cwRaw = new ClassWriter(0);
    CheckClassAdapter cw = new CheckClassAdapter(cwRaw);

    MethodVisitor mv;//from   w w  w  . j a va 2  s. c  o  m
    AnnotationVisitor av0;

    String className = getClassName(eventClass, priority, canceled);
    String classNameInternal = className.replace('.', '/');

    String invokeMethodDesc = "(" + Type.getDescriptor(eventClass) + ")V";

    String eventPriorityName = priority.name();

    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, classNameInternal, null,
            "org/spongepowered/mod/event/EventListenerHolder", null);

    cw.visitInnerClass("net/minecraftforge/event/world/BlockEvent$BreakEvent",
            "net/minecraftforge/event/world/BlockEvent", "BreakEvent", ACC_PUBLIC + ACC_STATIC);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "org/spongepowered/mod/event/EventListenerHolder", "<init>", "()V",
                false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "invoke", invokeMethodDesc, null, null);
        {
            av0 = mv.visitAnnotation("Lnet/minecraftforge/fml/common/eventhandler/SubscribeEvent;", true);
            av0.visitEnum("priority", "Lnet/minecraftforge/fml/common/eventhandler/EventPriority;",
                    eventPriorityName);
            av0.visit("receiveCanceled", canceled);
            av0.visitEnd();
        }
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, "org/spongepowered/mod/event/EventListenerHolder", "invoke",
                "(Ljava/lang/Object;)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    if (loaderAccess == null) {
        loaderAccess = new ClassLoaderAccess();
    }
    return loaderAccess.defineClass(className, cwRaw.toByteArray());
}