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:br.usp.each.saeg.badua.core.internal.instr.MethodInstrumenter.java

License:Open Source License

@Override
public void visitEnd() {
    if (next == null)
        return;//  w ww.j a v a 2  s.c  om

    final MethodNode original = new MethodNode(api, access, name, desc, signature, exceptions);
    final CodeSizeEvaluator sizeEval = new CodeSizeEvaluator(null);

    // 1. create a copy of the unmodified MethodNode
    accept(original);
    // 2. transform
    transform();
    // 3. evaluate new size
    accept(sizeEval);

    // size is fine (lower than 65535 bytes)
    if (sizeEval.getMaxSize() < 0xFFFF)
        accept(next);

    // size overflow
    else {
        sizeOverflow();
        original.accept(next);
    }
}

From source file:com.googlecode.d2j.dex.ExDex2Asm.java

License:Apache License

@Override
public void convertCode(DexMethodNode methodNode, MethodVisitor mv) {
    if (!AsmBridge.isMethodWriter(mv)) {
        throw new RuntimeException("We use a MethodWriter tricky here!");
    }/*w  w w.  j ava2  s . c  o m*/
    MethodNode mn = new MethodNode(Opcodes.ASM4, methodNode.access, methodNode.method.getName(),
            methodNode.method.getDesc(), null, null);
    try {
        super.convertCode(methodNode, mn);
    } catch (Exception ex) {
        if (exceptionHandler == null) {
            throw new DexException(ex, "fail convert code for %s", methodNode.method);
        } else {
            mn.instructions.clear();
            mn.tryCatchBlocks.clear();
            exceptionHandler.handleMethodTranslateException(methodNode.method, methodNode, mn, ex);
        }
    }
    // code convert ok, copy to MethodWriter and check for Size
    mn.accept(mv);
    try {
        AsmBridge.sizeOfMethodWriter(mv);
    } catch (Exception ex) {
        mn.instructions.clear();
        mn.tryCatchBlocks.clear();
        exceptionHandler.handleMethodTranslateException(methodNode.method, methodNode, mn, ex);
        AsmBridge.replaceMethodWriter(mv, mn);
    }
}

From source file:com.hea3ven.hardmodetweaks.core.ClassTransformerHardModeTweaks.java

License:Open Source License

private MethodNode createNewGetWorldTimeMethod(String methodName, boolean obfuscated) {
    // > long getWorldTime() {
    // >     return TimeTweaksManager.getWorldTime(this);
    // > }//from w  ww.j ava2s  . c  o m
    MethodNode getWorldTimeMethod = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, methodName, "()J", null,
            null);
    getWorldTimeMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    getWorldTimeMethod.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hea3ven/hardmodetweaks/TimeTweaksManager",
                    "getWorldTime", "(L" + WORLD_INFO.getPath(obfuscated) + ";)J"));
    getWorldTimeMethod.instructions.add(new InsnNode(Opcodes.LRETURN));
    return getWorldTimeMethod;
}

From source file:com.hea3ven.hardmodetweaks.core.ClassTransformerHardModeTweaks.java

License:Open Source License

private MethodNode createNewSetWorldTimeMethod(String methodName, boolean obfuscated) {
    // > void setWorldTime(long time) {
    // >     TimeTweaksManager.setWorldTime(this, time);
    // > }/*from  w  ww . j  av a 2s . c  o m*/
    MethodNode setWorldTimeMethod = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, methodName, "(J)V", null,
            null);
    setWorldTimeMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    setWorldTimeMethod.instructions.add(new VarInsnNode(Opcodes.LLOAD, 1));
    setWorldTimeMethod.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hea3ven/hardmodetweaks/TimeTweaksManager",
                    "setWorldTime", "(L" + WORLD_INFO.getPath(obfuscated) + ";J)V"));
    setWorldTimeMethod.instructions.add(new InsnNode(Opcodes.RETURN));
    return setWorldTimeMethod;
}

From source file:com.hea3ven.hardmodetweaks.core.ClassTransformerHardModeTweaks.java

License:Open Source License

private MethodNode createNewCalcCelAngleMethod(String methodName, boolean obfuscated) {
    // > float calculateCelestialAngle(long time, float off) {
    // >     return TimeTweaksManager.calculateCelestialAngle(time, off);
    // > }//from ww  w.  j ava2 s . c o  m
    MethodNode getWorldTimeMethod = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, methodName, "(JF)F", null,
            null);
    getWorldTimeMethod.instructions.add(new VarInsnNode(Opcodes.LLOAD, 1));
    getWorldTimeMethod.instructions.add(new VarInsnNode(Opcodes.FLOAD, 3));
    getWorldTimeMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            "com/hea3ven/hardmodetweaks/TimeTweaksManager", "calculateCelestialAngle", "(JF)F"));
    getWorldTimeMethod.instructions.add(new InsnNode(Opcodes.FRETURN));
    return getWorldTimeMethod;
}

From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java

License:MIT License

/**
 * Generate a new method "boolean name()", which returns a constant value.
 *
 * @param clazz Class to add method to//from ww  w. j  av a 2 s  . c  om
 * @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(ASMHelper.pushIntConstant(retval ? 1 : 0));
    code.add(new InsnNode(Opcodes.IRETURN));

    clazz.methods.add(method);
}

From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java

License:MIT License

/**
 * Generate a new method "int name()", which returns a constant value.
 *
 * @param clazz Class to add method to//from  w  ww .jav  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;

    code.add(ASMHelper.pushIntConstant(retval));
    code.add(new InsnNode(Opcodes.IRETURN));

    clazz.methods.add(method);
}

From source file:com.thomas15v.packetlib.codegenerator.asm.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   www . j ava2s.  c  o m
 */
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);

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

    clazz.methods.add(method);
}

From source file:com.thomas15v.packetlib.codegenerator.asm.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/*from   w w  w. ja  v a2  s .  c  o  m*/
 * @param argtype Argument type
 */
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);

    ASMHelper.populateSelfForwardingMethod(method, forwardname, rettype, argtype);

    clazz.methods.add(method);
}

From source file:com.thomas15v.packetlib.codegenerator.asm.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. co  m
 * @param fowardtype Forward type
 */
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);

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

    clazz.methods.add(method);
}