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

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

Introduction

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

Prototype

public MethodNode(final int api, final int access, final String name, final String descriptor,
        final String signature, final String[] exceptions) 

Source Link

Document

Constructs a new MethodNode .

Usage

From source file:org.spongepowered.mod.asm.transformers.EventTransformer.java

License:MIT License

protected static MethodNode createGetSimpleNameMethod() {
    MethodNode methodNode = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, "getSimpleName",
            "()Ljava/lang/String;", null, null);
    methodNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    methodNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "getClass",
            "()Ljava/lang/Class;", false));
    methodNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getSimpleName",
            "()Ljava/lang/String;", false));
    methodNode.instructions.add(new InsnNode(Opcodes.ARETURN));
    methodNode.maxLocals = 1;//from w ww .jav  a 2 s .  com
    methodNode.maxStack = 1;
    return methodNode;
}

From source file:org.spongepowered.mod.asm.transformers.EventTransformer.java

License:MIT License

protected static MethodNode createIsCancellableMethod() {
    MethodNode methodNode = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, "isCancellable", "()Z", null,
            null);/*w w w  . j av a 2  s.c  o  m*/
    methodNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    methodNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL,
            "net/minecraftforge/fml/common/eventhandler/Event", "isCancelable", "()Z", false));
    methodNode.instructions.add(new InsnNode(Opcodes.IRETURN));
    methodNode.maxLocals = 1;
    methodNode.maxStack = 1;
    return methodNode;
}

From source file:org.spongepowered.mod.asm.transformers.EventTransformer.java

License:MIT License

protected static MethodNode createIsCancelledMethod() {
    MethodNode methodNode = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, "isCancelled", "()Z", null, null);
    methodNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    methodNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL,
            "net/minecraftforge/fml/common/eventhandler/Event", "isCanceled", "()Z", false));
    methodNode.instructions.add(new InsnNode(Opcodes.IRETURN));
    methodNode.maxLocals = 1;/*from ww w.j av  a  2  s .  co m*/
    methodNode.maxStack = 1;
    return methodNode;
}

From source file:org.spongepowered.mod.asm.transformers.EventTransformer.java

License:MIT License

protected static MethodNode createSetCancelledMethod() {
    MethodNode methodNode = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, "setCancelled", "(Z)V", null,
            null);//from w w  w.  j  a  v  a2s  . co  m
    methodNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    methodNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, 1));
    methodNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL,
            "net/minecraftforge/fml/common/eventhandler/Event", "setCanceled", "(Z)V", false));
    methodNode.instructions.add(new InsnNode(Opcodes.RETURN));
    methodNode.maxLocals = 1;
    methodNode.maxStack = 1;
    return methodNode;
}

From source file:org.spongepowered.mod.asm.transformers.PriorityTransformer.java

License:MIT License

private MethodNode createGetGameMethod() {
    MethodNode methodNode = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, "getGame",
            "()Lorg/spongepowered/api/Game;", null, null);
    methodNode.instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, "org/spongepowered/mod/SpongeMod",
            "instance", "Lorg/spongepowered/mod/SpongeMod;"));
    methodNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/spongepowered/mod/SpongeMod",
            "getGame", "()Lorg/spongepowered/mod/SpongeGame;", false));
    methodNode.instructions.add(new InsnNode(Opcodes.ARETURN));
    methodNode.maxLocals = 1;//from   w  w w . java2 s  .c om
    methodNode.maxStack = 1;
    return methodNode;
}

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

License:MIT License

/**
 * Generate a new method "boolean name()", which returns a constant value.
 *
 * @param clazz Class to add method to/*from w  w  w.jav a2s  .c o  m*/
 * @param name Name of method
 * @param retval Return value of method
 */
public static void generateBooleanMethodConst(ClassNode clazz, String name, boolean retval) {
    MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()Z",
            null, null);
    InsnList code = method.instructions;

    code.add(new InsnNode(retval ? Opcodes.ICONST_1 : Opcodes.ICONST_0));
    code.add(new InsnNode(Opcodes.IRETURN));

    clazz.methods.add(method);
}

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

License:MIT License

/**
 * Generate a new method "int name()", which returns a constant value.
 *
 * @param clazz Class to add method to/*ww w . ja v  a2  s . co m*/
 * @param name Name of method
 * @param retval Return value of method
 */
public static void generateIntegerMethodConst(ClassNode clazz, String name, short retval) {
    MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()I",
            null, null);
    InsnList code = method.instructions;

    // Probably doesn't make a huge difference, but use BIPUSH if the value is small enough.
    if (retval >= Byte.MIN_VALUE && retval <= Byte.MAX_VALUE) {
        code.add(new IntInsnNode(Opcodes.BIPUSH, retval));
    } else {
        code.add(new IntInsnNode(Opcodes.SIPUSH, retval));
    }
    code.add(new InsnNode(Opcodes.IRETURN));

    clazz.methods.add(method);
}

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

License:MIT License

/**
 * Generate a forwarding method of the form
 * "T name() { return this.forward(); }".
 *
 * @param clazz Class to generate new method on
 * @param name Name of method to generate
 * @param forwardname Name of method to call
 * @param rettype Return type of method/*from  w ww.  j a  v a2s  .c om*/
 */
public static void generateSelfForwardingMethod(ClassNode clazz, String name, String forwardname,
        Type rettype) {
    MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name,
            "()" + rettype.getDescriptor(), null, null);

    populateSelfForwardingMethod(method, forwardname, rettype, Type.getObjectType(clazz.name));

    clazz.methods.add(method);
}

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

License:MIT License

/**
 * Generate a forwarding method of the form
 * "static T name(S object) { return object.forward(); }".
 *
 * @param clazz Class to generate new method on
 * @param name Name of method to generate
 * @param forwardname Name of method to call
 * @param rettype Return type of method//  w ww  .j a  v a2  s  .com
 */
public static void generateStaticForwardingMethod(ClassNode clazz, String name, String forwardname,
        Type rettype, Type argtype) {
    MethodNode method = new MethodNode(Opcodes.ASM5,
            Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name,
            "()" + rettype.getDescriptor(), null, null);

    populateSelfForwardingMethod(method, forwardname, rettype, argtype);

    clazz.methods.add(method);
}

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

License:MIT License

/**
 * Generate a forwarding method of the form
 * "T name() { return Class.forward(this); }".
 *
 * @param clazz Class to generate new method on
 * @param name Name of method to generate
 * @param forwardname Name of method to call
 * @param rettype Return type of method//from  ww w  .  ja  v  a 2 s.c  om
 */
public static void generateForwardingToStaticMethod(ClassNode clazz, String name, String forwardname,
        Type rettype, Type fowardtype) {
    MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name,
            "()" + rettype.getDescriptor(), null, null);

    populateForwardingToStaticMethod(method, forwardname, rettype, Type.getObjectType(clazz.name), fowardtype);

    clazz.methods.add(method);
}