Example usage for org.objectweb.asm Opcodes ALOAD

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

Introduction

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

Prototype

int ALOAD

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

Click Source Link

Usage

From source file:de.javanarior.vo.generator.ByteCodeGenerator.java

License:Apache License

/**
 * Generate a implementation of a value object.
 *
 * @param valueType/*w  ww  .  j a  v  a 2 s  . com*/
 *            - value object type
 * @param technicalType
 *            - to which the value object is mapped
 * @param wrapperClass
 *            - abstract wrapper class, correspond to the technical type
 * @return class name and byte code in a container
 */
/* CHECKSTYLE:OFF */
public static ByteCodeContainer generate(Class<?> valueType, Class<? extends Comparable<?>> technicalType,
        @SuppressWarnings("rawtypes") Class<? extends AbstractValue> wrapperClass) {
    /* CHECKSTYLE:ON */

    if (!isInterface(valueType)) {
        throw new IllegalArgumentException("Could not generate implementation for class " + valueType.getName()
                + ". Please provide interface");
    }

    ClassWriter classWriter = new ClassWriter(0);
    MethodVisitor methodVisitor;

    classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, implementationClassName(valueType),
            "L" + parentClassName(wrapperClass) + "<" + addTypeDiscriptor(valueType) + ">;"
                    + addTypeDiscriptor(valueType),
            parentClassName(wrapperClass), implementedInterfaces(valueType));

    methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR, methodDescriptor(technicalType),
            null, null);
    methodVisitor.visitCode();
    Label label0 = new Label();
    methodVisitor.visitLabel(label0);
    /* CHECKSTYLE:OFF */
    methodVisitor.visitLineNumber(8, label0);
    /* CHECKSTYLE:ON */
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(getILOADOpCode(technicalType), 1);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, parentClassName(wrapperClass), CONSTRUCTOR,
            methodDescriptor(technicalType), false);
    Label label1 = new Label();
    methodVisitor.visitLabel(label1);
    /* CHECKSTYLE:OFF */
    methodVisitor.visitLineNumber(9, label1);
    /* CHECKSTYLE:ON */
    methodVisitor.visitInsn(Opcodes.RETURN);
    Label label2 = new Label();
    methodVisitor.visitLabel(label2);
    methodVisitor.visitLocalVariable("this", addTypeSignature(implementationClassName(valueType)), null, label0,
            label2, 0);
    methodVisitor.visitLocalVariable("value", getType(technicalType), null, label0, label2, 1);
    int stackSize = getStackSize(Type.getDescriptor(technicalType));
    methodVisitor.visitMaxs(stackSize, stackSize);
    methodVisitor.visitEnd();
    classWriter.visitEnd();

    return new ByteCodeContainer(binaryClassName(valueType), classWriter.toByteArray());
}

From source file:de.kandid.model.Emitter.java

License:Apache License

/**
 * Assembles a class that implements the given interface by generating the byte code.
 * @param interfaze the interface to implement
 * @return the class// ww w .  j a  v a2s. c  o  m
 */
private static Class<? extends Emitter<?>> makeClass(Class<?> interfaze) {
    String nameClass = _nameEmitter + '$' + interfaze.getName().replace('.', '$');
    String nameInterface = Type.getInternalName(interfaze);
    // ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, nameClass, null, _nameEmitter, new String[] { name(interfaze) });

    // Generate default construcotor
    MethodVisitor cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitMethodInsn(INVOKESPECIAL, _nameEmitter, "<init>", "()V");
    cv.visitInsn(RETURN);
    cv.visitMaxs(1, 1);

    // Generate methods
    Method[] methods = interfaze.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        final Method m = methods[i];
        if (m.getReturnType() != void.class)
            throw new IllegalArgumentException("Method " + m.toGenericString() + " must not return a value");
        final String descMethod = Type.getMethodDescriptor(m);
        final MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED, m.getName(), descMethod, null,
                null);
        final Type[] argTypes = Type.getArgumentTypes(m);

        // for (int i = 0; i < _end; i += 2)
        //    ((Listener) _listeners[i]).send(...);

        int localStart = 1; // give one for "this"
        for (Type at : argTypes)
            localStart += at.getSize();
        Label entry = new Label();
        Label exit = new Label();
        mw.visitLabel(entry);

        // _isFiring = true;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_1);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        // setup local variables: i, _listeners, _end
        mw.visitLocalVariable("i", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitIntInsn(Opcodes.ISTORE, localStart);

        mw.visitLocalVariable("listeners", _descObjectArray, null, entry, exit, localStart + 1);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_listeners", _descObjectArray);
        mw.visitIntInsn(Opcodes.ASTORE, localStart + 1);

        mw.visitLocalVariable("end", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart + 2);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_end", Type.INT_TYPE.getDescriptor());
        mw.visitIntInsn(Opcodes.ISTORE, localStart + 2);

        final Label condition = new Label();
        mw.visitJumpInsn(GOTO, condition);

        final Label loop = new Label();
        mw.visitLabel(loop);

        //((Listener) _listeners[i]).doSomething()
        mw.visitIntInsn(Opcodes.ALOAD, localStart + 1);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitInsn(Opcodes.AALOAD);
        mw.visitTypeInsn(CHECKCAST, nameInterface);
        int offs = 1; // give one for "this"
        for (Type at : argTypes) {
            mw.visitVarInsn(at.getOpcode(ILOAD), offs);
            offs += at.getSize();
        }
        mw.visitMethodInsn(INVOKEINTERFACE, nameInterface, m.getName(), descMethod);

        // i += 2
        mw.visitIincInsn(localStart, 2);

        // if (i < end) goto loop
        mw.visitLabel(condition);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitIntInsn(Opcodes.ILOAD, localStart + 2);
        mw.visitJumpInsn(Opcodes.IF_ICMPLT, loop);

        // _isFiring = false;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        mw.visitLabel(exit);
        mw.visitInsn(RETURN);
        mw.visitMaxs(localStart + 2, localStart + 3);
        mw.visitEnd();
    }
    cw.visitEnd();
    return _loader.loadClass(cw, nameClass.replace('/', '.'));
}

From source file:de.sanandrew.core.manpack.transformer.TransformBadPotionsATN.java

License:Creative Commons License

private byte[] transformPotion(byte[] bytes) {
    ClassNode cn = ASMHelper.createClassNode(bytes);

    if (ASMHelper.hasClassMethodName(cn, ASMNames.M_isBadEffect)) {
        return bytes;
    }/* w  ww  .j a  va 2 s. c  o m*/

    MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, ASMNames.M_isBadEffect, "()Z", null, null);
    method.visitCode();
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/potion/Potion", ASMNames.F_isBadEffect, "Z");
    method.visitInsn(Opcodes.IRETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", "Lnet/minecraft/potion/Potion;", null, l0, l1, 0);
    method.visitMaxs(0, 0);
    method.visitEnd();

    cn.methods.add(method);

    bytes = ASMHelper.createBytes(cn, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformELBAttackingPlayer.java

License:Creative Commons License

private static byte[] transformAccessors(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    int complete = 0;
    for (MethodNode method : clazz.methods) {
        switch (method.name) {
        case "getELAttackingPlayer": {
            method.instructions.clear();
            method.visitCode();//w w  w  .ja v a 2 s  .  co m
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_getAttackingPlayer", "()Lnet/minecraft/entity/player/EntityPlayer;", false);
            method.visitInsn(Opcodes.ARETURN);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        case "setELAttackingPlayer": {
            method.instructions.clear();
            method.visitCode();
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 1);
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_setAttackingPlayer", "(Lnet/minecraft/entity/player/EntityPlayer;)V", false);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitInsn(Opcodes.RETURN);
            Label l2 = new Label();
            method.visitLabel(l2);
            method.visitLocalVariable("player", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l2, 0);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 1);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        case "getELRecentlyHit": {
            method.instructions.clear();
            method.visitCode();
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_getRecentlyHit", "()I", false);
            method.visitInsn(Opcodes.IRETURN);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        case "setELRecentlyHit": {
            method.instructions.clear();
            method.visitCode();
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 1);
            method.visitVarInsn(Opcodes.ILOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_setRecentlyHit", "(I)V", false);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitInsn(Opcodes.RETURN);
            Label l2 = new Label();
            method.visitLabel(l2);
            method.visitLocalVariable("hit", "I", null, l0, l2, 0);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 1);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        }

        if (complete >= 4) {
            break;
        }
    }

    bytes = ASMHelper.createBytes(clazz, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformELBAttackingPlayer.java

License:Creative Commons License

private static byte[] transformAttackingPlayer(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    /** ADD GETTER FOR ATTACKING PLAYER **/
    {/*from www . j  a v  a 2  s  .  c om*/
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_getAttackingPlayer",
                "()Lnet/minecraft/entity/player/EntityPlayer;", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase",
                ASMNames.F_attackingPlayer, "Lnet/minecraft/entity/player/EntityPlayer;");
        method.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    /** ADD SETTER FOR ATTACKING PLAYER **/
    {
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_setAttackingPlayer",
                "(Lnet/minecraft/entity/player/EntityPlayer;)V", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitVarInsn(Opcodes.ALOAD, 1);
        method.visitFieldInsn(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase",
                ASMNames.F_attackingPlayer, "Lnet/minecraft/entity/player/EntityPlayer;");
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        method.visitLabel(l2);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 0);
        method.visitLocalVariable("player", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l2, 1);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    /** ADD GETTER FOR RECENTLY HIT **/
    {
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_getRecentlyHit", "()I", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_recentlyHit,
                "I");
        method.visitInsn(Opcodes.IRETURN);
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    /** ADD SETTER FOR RECENTLY HIT **/
    {
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_setRecentlyHit", "(I)V", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitVarInsn(Opcodes.ILOAD, 1);
        method.visitFieldInsn(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_recentlyHit,
                "I");
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        method.visitLabel(l2);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 0);
        method.visitLocalVariable("hit", "I", null, l0, l2, 1);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformEnderman.java

License:Creative Commons License

private static byte[] transformEnderman(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);
    MethodNode method = ASMHelper.findMethod(clazz, ASMNames.MD_ENDERMAN_SHOULD_ATTACK_PLAYER);

    InsnList needle = new InsnList();
    needle.add(new VarInsnNode(Opcodes.ALOAD, 1));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_PLAYER_INVENTORY));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_INVPLAYER_ARMOR_INVENTORY));
    needle.add(new InsnNode(Opcodes.ICONST_3));
    needle.add(new InsnNode(Opcodes.AALOAD));
    needle.add(new VarInsnNode(Opcodes.ASTORE, 2));

    AbstractInsnNode insertPt = ASMHelper.findFirstNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_SAPUTILS_EVENT_BUS));
    newInstr.add(new TypeInsnNode(Opcodes.NEW, ASMNames.CL_ENDER_FACING_EVENT));
    newInstr.add(new InsnNode(Opcodes.DUP));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 1));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_ENDERFACINGEVENT_INIT, false));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_EVENT_BUS_POST, false));
    LabelNode l1 = new LabelNode();
    newInstr.add(new JumpInsnNode(Opcodes.IFEQ, l1));
    newInstr.add(new InsnNode(Opcodes.ICONST_0));
    newInstr.add(new InsnNode(Opcodes.IRETURN));
    newInstr.add(l1);/*from  ww w  .  ja  v  a  2s  .c  o m*/

    method.instructions.insertBefore(insertPt, newInstr);

    return ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
}

From source file:de.sanandrew.core.manpack.transformer.TransformEntityCollision.java

License:Creative Commons License

private static byte[] transformEntity(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_ENTITY_GET_BOUNDING_BOX);
    method.visitCode();/*from  w ww  .ja v a  2  s.  c om*/
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitVarInsn(Opcodes.ALOAD, 2);
    method.visitInsn(Opcodes.ARETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY, null, l0, l1, 0);
    method.visitLocalVariable("entity", ASMNames.CL_T_ENTITY, null, l0, l1, 1);
    method.visitLocalVariable("oldAABB", ASMNames.CL_T_AXIS_ALIGNED_BB, null, l0, l1, 2);
    method.visitMaxs(1, 2);
    method.visitEnd();

    clazz.methods.add(method);

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformEntityCollision.java

License:Creative Commons License

private static byte[] transformWorld(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);
    MethodNode method = ASMHelper.findMethod(clazz, ASMNames.MD_WORLD_GET_COLLIDING_BB);

    InsnList needle = new InsnList();
    LabelNode ln = new LabelNode();
    needle.add(ln);//from w w  w .j a  v  a 2s. co  m
    needle.add(new LineNumberNode(-1, ln));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 1));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 2));
    needle.add(new VarInsnNode(Opcodes.DLOAD, -1));
    needle.add(new VarInsnNode(Opcodes.DLOAD, -1));
    needle.add(new VarInsnNode(Opcodes.DLOAD, -1));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_AABB_EXPAND, false));
    needle.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_GET_ENTITIES_EXCLUDE, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, -1));

    VarInsnNode insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList injectList = new InsnList();
    injectList.add(new LabelNode());
    injectList.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_SAPUTILS_EVENT_BUS));
    injectList.add(new TypeInsnNode(Opcodes.NEW, ASMNames.CL_COLLIDING_ENTITY_CHECK_EVENT));
    injectList.add(new InsnNode(Opcodes.DUP));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, insertPoint.var));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 1));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 2));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_COLLENTITYCHKEVT_INIT, false));
    injectList.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_EVENT_BUS_POST, false));
    injectList.add(new InsnNode(Opcodes.POP));

    method.instructions.insert(insertPoint, injectList);

    // insert entity-sensitive bounding box method

    needle = new InsnList();
    ln = new LabelNode();
    needle.add(ln);
    needle.add(new LineNumberNode(-1, ln));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 11));
    needle.add(new VarInsnNode(Opcodes.ILOAD, 12));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEINTERFACE, ASMNames.MD_LIST_GET, true));
    needle.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ENTITY));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ENTITY_GET_BOUNDING_BOX, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, -1));

    insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    injectList = new InsnList();
    injectList.add(new LabelNode());
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 11));
    injectList.add(new VarInsnNode(Opcodes.ILOAD, 12));
    injectList.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEINTERFACE, ASMNames.MD_LIST_GET, true));
    injectList.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ENTITY));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 1));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, insertPoint.var));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_ENTITY_GET_BOUNDING_BOX, false));
    injectList.add(new VarInsnNode(Opcodes.ASTORE, insertPoint.var));

    method.instructions.insert(insertPoint, injectList);

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformEntityThrowable.java

License:Creative Commons License

private static byte[] transformLqThrowable(byte[] bytes) {
    ClassNode classNode = ASMHelper.createClassNode(bytes);

    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_CAN_IMPACT_ON_LIQUID);

    method.visitCode();/*from w  w  w  . j  a  v  a2  s. c  o  m*/
    Label label1 = new Label();
    method.visitLabel(label1);
    method.visitInsn(Opcodes.ICONST_0);
    method.visitInsn(Opcodes.IRETURN);
    Label label2 = new Label();
    method.visitLabel(label2);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_THROWABLE, null, label1, label2, 0);
    method.visitMaxs(0, 0);
    method.visitEnd();
    classNode.methods.add(method);

    method = ASMHelper.findMethod(classNode, ASMNames.MD_THROWABLE_ON_UPDATE);

    InsnList needle = new InsnList();
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_MOTION_Z));
    needle.add(new InsnNode(Opcodes.DADD));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESTATIC, ASMNames.MD_VEC3_CREATE_VECTOR_HELPER, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, 2));
    needle.add(new LabelNode());
    needle.add(new LineNumberNode(-1, new LabelNode()));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_WORLD_OBJ));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 1));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 2));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_RAY_TRACE_BLOCKS, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, -1));

    VarInsnNode insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList injectList = new InsnList();
    injectList.add(new LabelNode());
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_WORLD_OBJ));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 1));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 2));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_CAN_IMPACT_ON_LIQUID, false));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_RAY_TRACE_BLOCKS_Z, false));
    injectList.add(new VarInsnNode(Opcodes.ASTORE, insertPoint.var));

    method.instructions.insert(insertPoint, injectList);

    return ASMHelper.createBytes(classNode, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
}

From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java

License:Creative Commons License

private static MethodNode injectMethodGetCustomArmorItem() {
    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PRIVATE, ASMNames.MD_SAP_GET_CUSTOM_ARMOR_ITEM);
    method.visitCode();//from   w  w  w . j a v  a 2 s .  c  om
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    ASMHelper.visitFieldInsn(method, Opcodes.GETFIELD, ASMNames.FD_HORSE_DATAWATCHER);
    method.visitIntInsn(Opcodes.BIPUSH, 23);
    ASMHelper.visitMethodInsn(method, Opcodes.INVOKEVIRTUAL, ASMNames.MD_DATAWATCHER_GET_OBJ_STACK, false);
    method.visitInsn(Opcodes.ARETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_HORSE, null, l0, l1, 0);
    method.visitMaxs(2, 1);
    method.visitEnd();

    return method;
}