Example usage for org.objectweb.asm Opcodes IFEQ

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

Introduction

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

Prototype

int IFEQ

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

Click Source Link

Usage

From source file:org.spongepowered.common.event.filter.delegate.ExcludeSubtypeFilterDelegate.java

License:MIT License

@Override
protected int getJumpOp() {
    return Opcodes.IFEQ;
}

From source file:org.spongepowered.despector.emitter.bytecode.statement.BytecodeIfEmitter.java

License:Open Source License

private void emitInverse(BytecodeEmitterContext ctx, Condition cond) {
    MethodVisitor mv = ctx.getMethodVisitor();
    if (cond instanceof CompareCondition) {
        CompareCondition cmp = (CompareCondition) cond;
        ctx.emitInstruction(cmp.getLeft(), null);
        ctx.emitInstruction(cmp.getRight(), null);
        switch (cmp.getOperator()) {
        case NOT_EQUAL:
            mv.visitJumpInsn(Opcodes.IF_ACMPEQ, this.end);
            break;
        case EQUAL:
            mv.visitJumpInsn(Opcodes.IF_ACMPNE, this.end);
            break;
        default:// w  w  w . ja v a2s  .  co m
            throw new IllegalStateException("Unsupported compare operator: " + cmp.getOperator().name());
        }
    } else if (cond instanceof BooleanCondition) {
        BooleanCondition bool = (BooleanCondition) cond;
        ctx.emitInstruction(bool.getConditionValue(), null);
        if (bool.isInverse()) {
            mv.visitJumpInsn(Opcodes.IFNE, this.end);
        } else {
            mv.visitJumpInsn(Opcodes.IFEQ, this.end);
        }
    } else {
        throw new IllegalStateException();
    }
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void generateTransition(MethodVisitor mv, DfaState source, DfaState target) {
    if (source.isTerminal() && source != target) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ICONST_M1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");
    }//  w ww .j a v a 2s  .co m
    mv.visitIntInsn(Opcodes.SIPUSH, target.getIndex());
    mv.visitVarInsn(Opcodes.ISTORE, 5);
    if (target.isTerminal() && source != target) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitIntInsn(Opcodes.SIPUSH, target.getDomains()[0]);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");
    }

    debug(mv, "DFA: " + source.getIndex() + " -> " + target.getIndex() + " "
            + Arrays.toString(target.getDomains()));
    if (target.isTerminal()) {
        Label noReluctant = new Label();
        mv.visitVarInsn(Opcodes.ILOAD, 4);
        mv.visitJumpInsn(Opcodes.IFEQ, noReluctant);
        mv.visitIincInsn(2, 1);
        debug(mv, "DFA reached terminal state");
        mv.visitJumpInsn(Opcodes.GOTO, saveLabel);
        mv.visitLabel(noReluctant);
    }
    if (source.getIndex() + 1 == target.getIndex()) {
        mv.visitIincInsn(2, 1);
        generateLengthGuard(mv);
        mv.visitJumpInsn(Opcodes.GOTO, stateLabels[target.getIndex()]);
    } else {
        mv.visitJumpInsn(Opcodes.GOTO, continueLabel);
    }
}

From source file:pl.asie.foamfix.coremod.patches.ReturnIfBooleanTruePatch.java

License:Open Source License

public ReturnIfBooleanTruePatch(String optionName, String... methods) {
    this.optionName = optionName;
    this.methods = ImmutableSet.copyOf(methods);

    list = new InsnList();
    Label l = new Label();
    LabelNode ln = new LabelNode(l);
    list.add(new FieldInsnNode(Opcodes.GETSTATIC, "pl/asie/foamfix/shared/FoamFixShared", "config",
            "Lpl/asie/foamfix/shared/FoamFixConfig;"));
    list.add(new FieldInsnNode(Opcodes.GETFIELD, "pl/asie/foamfix/shared/FoamFixConfig", optionName, "Z"));
    list.add(new JumpInsnNode(Opcodes.IFEQ, ln));
    list.add(new InsnNode(Opcodes.RETURN));
    list.add(ln);/*from ww  w .j  a va2s .  com*/
    list.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));
}

From source file:pl.clareo.coroutines.core.ClassTransformer.java

License:Apache License

private static InsnList loggingInstructions(String ownerName, String loggerField, Level level,
        Object... messages) {/*from   w w w .jav  a  2 s .c o  m*/
    InsnList insn = new InsnList();
    insn.add(new FieldInsnNode(Opcodes.GETSTATIC, ownerName, loggerField, "Ljava/util/logging/Logger;"));
    insn.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/util/logging/Level", level.getName(),
            "Ljava/util/logging/Level;"));
    // stack: * *
    insn.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/util/logging/Logger", "isLoggable",
            "(Ljava/util/logging/Level;)Z"));
    // stack: *
    LabelNode exitBranch = new LabelNode();
    insn.add(new JumpInsnNode(Opcodes.IFEQ, exitBranch));
    // stack:
    insn.add(new FieldInsnNode(Opcodes.GETSTATIC, ownerName, loggerField, "Ljava/util/logging/Logger;"));
    insn.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/util/logging/Level", level.getName(),
            "Ljava/util/logging/Level;"));
    // stack: * *
    insn.add(new TypeInsnNode(Opcodes.NEW, "java/lang/StringBuilder"));
    insn.add(new InsnNode(Opcodes.DUP));
    // stack: * * * *
    String message0 = messages[0].toString();
    insn.add(new LdcInsnNode(message0));
    // stack: * * * * *
    insn.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>",
            "(Ljava/lang/String;)V"));
    // stack: * * *
    for (int m = 1; m < messages.length; m++) {
        Object message = messages[m];
        if (message instanceof Number) {
            insn.add(new VarInsnNode(Opcodes.ALOAD, ((Number) message).intValue()));
        } else {
            insn.add(new LdcInsnNode(message.toString()));
        }
        // stack: * * * *
        insn.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                "(Ljava/lang/Object;)Ljava/lang/StringBuilder;"));
        // stack: * * *
    }
    insn.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString",
            "()Ljava/lang/String;"));
    insn.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/util/logging/Logger", "log",
            "(Ljava/util/logging/Level;Ljava/lang/String;)V"));
    // stack:
    insn.add(exitBranch);
    return insn;
}

From source file:serianalyzer.JVMImpl.java

License:Open Source License

/**
 * @param opcode//from   w w w. j  av a2 s.c o  m
 * @param label
 * @param s
 * @return
 */
static boolean handleJVMJump(int opcode, Label label, JVMStackState s) {
    boolean tainted;
    switch (opcode) {
    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
    case Opcodes.IF_ICMPLE:
    case Opcodes.IF_ACMPEQ:
    case Opcodes.IF_ACMPNE:
        BaseType o1 = s.pop();
        BaseType o2 = s.pop();
        tainted = !(o1 != null) || !(o2 != null) || o1.isTainted() || o2.isTainted();
        break;
    case Opcodes.IFEQ:
    case Opcodes.IFNE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
    case Opcodes.IFLE:
    case Opcodes.IFNULL:
    case Opcodes.IFNONNULL:
        BaseType c = s.pop();
        tainted = (c == null || c.isTainted());
        break;

    case Opcodes.JSR:
        s.push(new BasicConstant(Type.INT_TYPE, label));
        tainted = false;
        break;
    case Opcodes.GOTO:
        tainted = false;
        break;
    default:
        log.warn("Unsupported opcode " + opcode); //$NON-NLS-1$
        tainted = true;
    }
    return tainted;
}

From source file:sg.atom.core.actor.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Writes the bean constructor to the given ClassWriter.
 *
 * @param beanClass the original bean class to extend
 * @param bcd the descriptor of the bean
 * @param classNameInternal the internal name of the new class
 * @param cw the ClassWriter to write to
 * @param snippetWriter if not null, this will be invoked to add a snippet
 * after the invocation of the super constructor
 *///from   w ww  .j  a  va  2s.  co  m
public static void writeConstructor(Class<?> beanClass, BeanClassDescriptor bcd, String classNameInternal,
        ClassWriter cw, SnippetWriter snippetWriter) {
    String classNameDescriptor = "L" + classNameInternal + ";";

    int localPropertySize = 0;
    ArrayList<PropertyDescriptor> localVarProperties = new ArrayList<PropertyDescriptor>();
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        if (pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null)) {
            localVarProperties.add(pd);
            localPropertySize += Type.getType(pd.getPropertyClass()).getSize();
        }
    }

    final int locVarThis = 0;
    final int locVarController = 1;
    final int locVarProps = 2;
    final int locVarPropertiesOffset = 3;
    final int locVarP = 3 + localPropertySize;
    final int locVarK = 4 + localPropertySize;
    final int locVarV = 5 + localPropertySize;

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)V", null, null);
    mv.visitCode();
    Label lTry = new Label();
    Label lCatch = new Label();
    mv.visitTryCatchBlock(lTry, lCatch, lCatch, "java/lang/ClassCastException");

    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(beanClass), "<init>", "()V");

    if (snippetWriter != null) {
        snippetWriter.write(mv);
    }

    Label lPropertyInit = new Label();
    mv.visitLabel(lPropertyInit);
    // load default values into the local variables for each property that must be set
    int varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        if (pd.getDefaultValue() != null) {
            mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(pd.getDefaultValue().getDeclaringClass()),
                    pd.getDefaultValue().getName(), Type.getDescriptor(pd.getDefaultValue().getType()));
        } else {
            GenerationUtils.generateLoadDefault(mv, pd.getPropertyClass());
        }
        mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), locVarPropertiesOffset + varCount);
        varCount += pt.getSize();
    }

    // loop through the props argument's list
    mv.visitVarInsn(Opcodes.ALOAD, locVarProps);
    mv.visitVarInsn(Opcodes.ASTORE, locVarP);
    Label lWhile = new Label();
    Label lEndWhile = new Label();
    Label lWhileBody = new Label();
    mv.visitLabel(lWhile);
    mv.visitJumpInsn(Opcodes.GOTO, lEndWhile);
    mv.visitLabel(lWhileBody);

    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getKey",
            "()Ljava/lang/String;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarK);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getValue",
            "()Ljava/lang/Object;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarV);

    mv.visitLabel(lTry);
    // write an if for each property
    Label lEndIf = new Label();
    varCount = 0;
    int ifCount = 0;
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        boolean usesLocal = pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null);
        Class<?> propClass = pd.getPropertyClass();
        Type pt = Type.getType(propClass);
        mv.visitVarInsn(Opcodes.ALOAD, locVarK);
        mv.visitLdcInsn(pd.getName());
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
        Label lElse = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, lElse);

        if (!usesLocal) {
            mv.visitVarInsn(Opcodes.ALOAD, locVarThis); // for setter invocation, load 'this'
        }
        if (propClass.isPrimitive()) {
            mv.visitLdcInsn(pd.getName());
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(BeanHelper.class),
                    String.format("get%s%sFromPropValue",
                            propClass.getName().substring(0, 1).toUpperCase(Locale.US),
                            propClass.getName().substring(1)),
                    "(Ljava/lang/String;Ljava/lang/Object;)" + pt.getDescriptor());
        } else if (!propClass.equals(Object.class)) {
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);
            mv.visitTypeInsn(Opcodes.CHECKCAST, pt.getInternalName());
        } else {
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);
        }

        if (!usesLocal) {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(),
                    Type.getMethodDescriptor(pd.getSetter()));
        } else {
            mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), varCount + locVarPropertiesOffset);
        }

        mv.visitJumpInsn(Opcodes.GOTO, lEndIf);
        mv.visitLabel(lElse);

        ifCount++;
        if (usesLocal) {
            varCount += pt.getSize();
        }
    }

    // else (==> if not prop matched) throw IllegalArgumentException
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("Unknown property \"%s\".");
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ALOAD, locVarK);
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            "(Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(lCatch);
    mv.visitInsn(Opcodes.POP); // pop the exception object (not needed)
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("Incompatible type for property \"%s\". Got %s.");
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ALOAD, locVarK);
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitVarInsn(Opcodes.ALOAD, locVarV);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            "(Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(lEndIf);
    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "tail",
            "()Lorg/actorsguildframework/Props;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarP);

    mv.visitLabel(lEndWhile);
    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitJumpInsn(Opcodes.IFNONNULL, lWhileBody);

    // write local variables back into properties 
    varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
        if (pd.getPropertySource() == PropertySource.ABSTRACT_METHOD) {
            mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount);
            mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal,
                    String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()), pt.getDescriptor());
        } else if (pd.getPropertySource() == PropertySource.USER_WRITTEN) {
            mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(),
                    Type.getMethodDescriptor(pd.getSetter()));
        } else {
            throw new RuntimeException("Internal error");
        }
        varCount += pt.getSize();
    }

    // if bean is thread-safe, publish all writes now
    if (bcd.isThreadSafe()) {
        mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
        mv.visitInsn(Opcodes.DUP);
        mv.visitInsn(Opcodes.MONITORENTER);
        mv.visitInsn(Opcodes.MONITOREXIT);
    }

    mv.visitInsn(Opcodes.RETURN);
    Label lEnd = new Label();
    mv.visitLabel(lEnd);

    mv.visitLocalVariable("this", classNameDescriptor, null, lBegin, lEnd, locVarThis);
    mv.visitLocalVariable("controller", "Lorg/actorsguildframework/internal/Controller;", null, lBegin, lEnd,
            locVarController);
    mv.visitLocalVariable("props", "Lorg/actorsguildframework/Props;", null, lBegin, lEnd, locVarProps);
    varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        mv.visitLocalVariable("__" + pd.getName(), pt.getDescriptor(),
                GenericTypeHelper.getSignature(pd.getPropertyType()), lPropertyInit, lEnd,
                locVarPropertiesOffset + varCount);
        varCount += pt.getSize();
    }
    mv.visitLocalVariable("p", "Lorg/actorsguildframework/Props;", null, lPropertyInit, lEnd, locVarP);
    mv.visitLocalVariable("k", "Ljava/lang/String;", null, lWhile, lEndWhile, locVarK);
    mv.visitLocalVariable("v", "Ljava/lang/Object;", null, lWhile, lEndWhile, locVarV);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:v6.java.preverifier.MethodRewriter.java

License:Open Source License

/**
 * Visit the specified instruction and do the right thing.
 * /*from  w ww  .ja va 2s.  co m*/
 * @param method
 * @param region
 * @param insnNode
 * @throws AnalyzerException
 */
private void visitInstruction(MethodNode method, Region region, AbstractInsnNode insnNode)
        throws AnalyzerException {
    int opcode = insnNode.getOpcode();
    switch (opcode) {
    case Opcodes.JSR:
        visitJumpToSubroutine(method, region, (JumpInsnNode) insnNode);
        break;

    case Opcodes.IFEQ:
    case Opcodes.IFNE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
    case Opcodes.IFLE:
    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
    case Opcodes.IF_ICMPLE:
    case Opcodes.IF_ACMPEQ:
    case Opcodes.IF_ACMPNE:
    case Opcodes.GOTO:
    case Opcodes.IFNULL:
    case Opcodes.IFNONNULL:
        visitJump(method, region, (JumpInsnNode) insnNode);
        break;

    case Opcodes.LOOKUPSWITCH:
        visitLookupSwitch(method, region, (LookupSwitchInsnNode) insnNode);
        break;

    case Opcodes.TABLESWITCH:
        visitTableSwitch(method, region, (TableSwitchInsnNode) insnNode);
        break;

    default:
        insnNode.accept(method);
    }
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformBlockPistonBase(byte[] basicClass) {
    log("Transforming BlockPistonBase");
    MethodSignature sig1 = new MethodSignature("doMove", "func_176319_a", "a",
            "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;Z)Z");
    MethodSignature sig2 = new MethodSignature("canPush", "func_185646_a", "a",
            "(Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;ZLnet/minecraft/util/EnumFacing;)Z");

    byte[] transClass = transform(basicClass, Pair.of(sig1, combine((AbstractInsnNode node) -> { // Filter
        return node.getOpcode() == Opcodes.ASTORE && ((VarInsnNode) node).var == 11;
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 2));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 6));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 8));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 11));
        newInstructions.add(new VarInsnNode(Opcodes.ILOAD, 4));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "breakStuffWithSpikes",
                "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Ljava/util/List;Ljava/util/List;Lnet/minecraft/util/EnumFacing;Z)Z"));

        // recalculate the list and array sizes
        LabelNode label = new LabelNode();
        newInstructions.add(new JumpInsnNode(Opcodes.IFEQ, label));

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 6));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/util/List", "size", "()I"));
        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 8));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/util/List", "size", "()I"));
        newInstructions.add(new InsnNode(Opcodes.IADD));
        newInstructions.add(new VarInsnNode(Opcodes.ISTORE, 9));
        newInstructions.add(new VarInsnNode(Opcodes.ILOAD, 9));

        AbstractInsnNode newNode = node.getPrevious();
        while (true) {
            if (newNode.getOpcode() == Opcodes.ANEWARRAY) {
                newInstructions.add(new TypeInsnNode(Opcodes.ANEWARRAY, ((TypeInsnNode) newNode).desc));
                break;
            }/*  w  w  w.  j  ava 2  s . c om*/
            newNode = newNode.getPrevious();
        }

        newInstructions.add(new VarInsnNode(Opcodes.ASTORE, 10));
        newInstructions.add(label);

        method.instructions.insert(node, newInstructions);
        return true;
    })));

    transClass = transform(transClass, Pair.of(sig2, combine((AbstractInsnNode node) -> { // Filter
        return node.getOpcode() == Opcodes.INVOKEVIRTUAL
                && ((MethodInsnNode) node).name.equals("hasTileEntity");
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "shouldPistonMoveTE",
                "(ZLnet/minecraft/block/state/IBlockState;)Z"));

        method.instructions.insert(node, newInstructions);
        return true;
    })));

    return transClass;
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformWorldServer(byte[] basicClass) {
    log("Transforming WorldServer");
    MethodSignature sig = new MethodSignature("areAllPlayersAsleep", "func_73056_e", "g", "()Z");

    return transform(basicClass, Pair.of(sig, combine((AbstractInsnNode node) -> { // Filter
        return true;
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();

        newInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, "isEveryoneAsleep",
                "(Lnet/minecraft/world/World;)I"));
        newInstructions.add(new InsnNode(Opcodes.DUP));
        LabelNode label = new LabelNode();
        newInstructions.add(new JumpInsnNode(Opcodes.IFEQ, label));
        newInstructions.add(new InsnNode(Opcodes.ICONST_1));
        newInstructions.add(new InsnNode(Opcodes.ISUB));
        newInstructions.add(new InsnNode(Opcodes.IRETURN));
        newInstructions.add(label);/*from ww w . java  2 s. c  o  m*/

        method.instructions.insertBefore(node, newInstructions);
        return true;
    })));
}