Example usage for org.objectweb.asm.tree AbstractInsnNode getOpcode

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getOpcode

Introduction

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

Prototype

public int getOpcode() 

Source Link

Document

Returns the opcode of this instruction.

Usage

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

/**
 * Removes goto instructions that go to the label immediately following
 * them.//  w w  w  . j  a  v a2 s  .c o  m
 * @return true iff changes were made
 */
private boolean removeUnnecessaryGotos() {
    InsnList insns = methodNode.instructions;
    for (int i = 0; i < insns.size() - 1; ++i) {
        AbstractInsnNode first = insns.get(i);
        if (first.getOpcode() != Opcodes.GOTO)
            continue;
        AbstractInsnNode second = insns.get(i + 1);
        if (!(second instanceof LabelNode))
            continue;
        if (((JumpInsnNode) first).label != second)
            continue;
        insns.remove(first);
        return true;
    }
    return false;
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

public static void main(String[] args) {
    Module m = new Module();
    Klass k = m.getKlass(Module.class);
    Method ar = k.getMethods("getArrayKlass").iterator().next();
    ar.resolve();//from ww  w .j a  v a2s. c  o  m
    MethodNode mn = unresolve(ar);
    for (int i = 0; i < mn.instructions.size(); ++i) {
        AbstractInsnNode insn = mn.instructions.get(i);
        System.out.format("%d: %d %s%n", i, insn.getOpcode(), insn);
    }
}

From source file:edu.ubc.mirrors.holograms.FrameVerifier.java

License:Open Source License

@Override
public FrameValue newOperation(AbstractInsnNode insn) throws AnalyzerException {
    BasicValue result = simplerVerifier.newOperation(insn);
    if (insn.getOpcode() == Opcodes.NEW) {
        return new FrameValue(result, insn);
    } else {//from ww  w . j av a  2  s.c o  m
        return FrameValue.fromBasicValue(result);
    }
}

From source file:edu.ubc.mirrors.holograms.FrameVerifier.java

License:Open Source License

@Override
public FrameValue naryOperation(AbstractInsnNode insn, List<? extends FrameValue> values)
        throws AnalyzerException {
    if (insn.getOpcode() == Opcodes.INVOKESPECIAL) {
        MethodInsnNode methodNode = (MethodInsnNode) insn;
        if (methodNode.name.charAt(0) == '<') {
            FrameValue target = values.get(0);
            Type targetType = target.getBasicValue().getType();
            Type ownerType = Type.getObjectType(methodNode.owner);
            BasicValue owner = new BasicValue(ownerType);
            FrameValue expected = new FrameValue(owner, insn);
            if (!target.isUninitialized()) {
                throw new AnalyzerException(insn, "Argument 0", expected, target);
            }/* w w w.  j av  a 2 s .  c om*/
            if (!targetType.equals(ownerType) && !(target.isUninitializedThis()
                    && simplerVerifier.getSuperClass(targetType).equals(ownerType))) {
                throw new AnalyzerException(insn, "Argument 0", expected, target);
            }
            FrameValue result = FrameValue.fromBasicValue(target.getBasicValue());
            int locals = currentFrame.getLocals();
            for (int local = 0; local < locals; local++) {
                if (currentFrame.getLocal(local) == target) {
                    currentFrame.setLocal(local, result);
                }
            }
            Stack<FrameValue> stack = new Stack<FrameValue>();
            while (currentFrame.getStackSize() > 0) {
                FrameValue value = currentFrame.pop();
                if (value == target) {
                    value = result;
                }
                stack.push(value);
            }
            while (!stack.empty()) {
                currentFrame.push(stack.pop());
            }
        }
    }

    List<BasicValue> basicValues = new ArrayList<BasicValue>(values.size());
    for (FrameValue value : values) {
        basicValues.add(value.getBasicValue());
    }

    return FrameValue.fromBasicValue(simplerVerifier.naryOperation(insn, basicValues));
}

From source file:edu.ubc.mirrors.holograms.FrameVerifier.java

License:Open Source License

@Override
public FrameValue copyOperation(AbstractInsnNode insn, FrameValue value) throws AnalyzerException {
    if (value.isUninitialized()) {
        int opcode = insn.getOpcode();
        if (opcode == ALOAD || opcode == ASTORE || (DUP <= opcode && opcode <= SWAP)) {
            return value;
        } else {/*from w  w  w .j a  va 2 s .  c om*/
            throw new AnalyzerException(insn, "value");
        }
    }

    return FrameValue.fromBasicValue(simplerVerifier.copyOperation(insn, value.getBasicValue()));
}

From source file:edu.ubc.mirrors.holograms.FrameVerifier.java

License:Open Source License

@Override
public FrameValue binaryOperation(AbstractInsnNode insn, FrameValue value1, FrameValue value2)
        throws AnalyzerException {
    BasicValue basicValue1;//from w  w w . java  2s.  c  o m
    if (insn.getOpcode() == PUTFIELD) {
        basicValue1 = value1.getBasicValue();
    } else {
        basicValue1 = checkUninitialized(insn, "value 1", value1);
    }

    return FrameValue.fromBasicValue(
            simplerVerifier.binaryOperation(insn, basicValue1, checkUninitialized(insn, "value 2", value2)));
}

From source file:gemlite.core.internal.measurement.MeasureHelper.java

License:Apache License

public final static void instrumentCheckPoint(String className, MethodNode mn) {
    if (LogUtil.getCoreLog().isTraceEnabled())
        LogUtil.getCoreLog().trace("Found check point, class:" + className + " method:" + mn.name);

    InsnList insn = mn.instructions;/*from   w  w w  .  ja  va 2s  . c  o m*/
    List<AbstractInsnNode> returnIndex = new ArrayList<>();
    // return
    int localVarCount = mn.localVariables.size();
    for (int i = 0; i < insn.size(); i++) {
        AbstractInsnNode insnNode = (AbstractInsnNode) insn.get(i);
        switch (insnNode.getOpcode()) {
        case Opcodes.ARETURN:
        case Opcodes.IRETURN:
        case Opcodes.DRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
            returnIndex.add(insnNode.getPrevious());
            break;
        case Opcodes.RETURN:
            returnIndex.add(insnNode);
            break;
        }
    }
    // 
    insn.insert(new VarInsnNode(Opcodes.LSTORE, localVarCount + 2));
    insn.insert(
            new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false));
    // ?
    for (AbstractInsnNode insnNode : returnIndex) {
        insn.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System",
                "currentTimeMillis", "()J", false));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LSTORE, localVarCount + 4));

        insn.insertBefore(insnNode, new LdcInsnNode(className));
        insn.insertBefore(insnNode, new LdcInsnNode(mn.name));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 2));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 4));
        insn.insertBefore(insnNode,
                new MethodInsnNode(Opcodes.INVOKESTATIC, "gemlite/core/internal/measurement/MeasureHelper",
                        "recordCheckPoint", "(Ljava/lang/String;Ljava/lang/String;JJ)V", false));
    }
}

From source file:hellfirepvp.astralsorcery.core.ClassPatch.java

License:Open Source License

@Nonnull
public AbstractInsnNode findFirstInstructionAfter(MethodNode mn, int startingIndex, int opCode) {
    for (int i = startingIndex; i < mn.instructions.size(); i++) {
        AbstractInsnNode ain = mn.instructions.get(i);
        if (ain.getOpcode() == opCode)
            return ain;
    }/* w  w w.  j  a  v a  2 s  .  c  o m*/
    throw new ASMTransformationException("Couldn't find Instruction with opcode " + opCode);
}

From source file:hellfirepvp.astralsorcery.core.patch.helper.PatchBlockModify.java

License:Open Source License

@Override
public void patch(ClassNode cn) {
    MethodNode mn = getMethod(cn, "setBlockState", "func_177436_a",
            "(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;)Lnet/minecraft/block/state/IBlockState;");
    for (int i = 0; i < mn.instructions.size(); i++) {
        AbstractInsnNode aNode = mn.instructions.get(i);
        if (aNode.getOpcode() == Opcodes.ARETURN) {
            AbstractInsnNode prev = aNode.getPrevious();
            if (prev instanceof VarInsnNode && prev.getOpcode() == Opcodes.ALOAD
                    && ((VarInsnNode) prev).var == 8) {
                mn.instructions.insertBefore(prev,
                        new FieldInsnNode(Opcodes.GETSTATIC, "net/minecraftforge/common/MinecraftForge",
                                "EVENT_BUS", "Lnet/minecraftforge/fml/common/eventhandler/EventBus;"));
                mn.instructions.insertBefore(prev, new TypeInsnNode(Opcodes.NEW,
                        "hellfirepvp/astralsorcery/common/event/BlockModifyEvent"));
                mn.instructions.insertBefore(prev, new InsnNode(Opcodes.DUP));
                mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 0)); //Chunk
                mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 1)); //Pos
                mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 8)); //OldState
                mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 2)); //NewState
                mn.instructions.insertBefore(prev, new MethodInsnNode(Opcodes.INVOKESPECIAL,
                        "hellfirepvp/astralsorcery/common/event/BlockModifyEvent", "<init>",
                        "(Lnet/minecraft/world/chunk/Chunk;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/block/state/IBlockState;)V",
                        false));/* w w w. j a v  a 2s  .  c  o  m*/
                mn.instructions.insertBefore(prev,
                        new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                                "net/minecraftforge/fml/common/eventhandler/EventBus", "post",
                                "(Lnet/minecraftforge/fml/common/eventhandler/Event;)Z", false));
                mn.instructions.insertBefore(prev, new InsnNode(Opcodes.POP));
                return;
            }
        }
    }
    throw new ASMTransformationException(
            "Could not find the expected return statement in the setBlockState method!");
}

From source file:ht.misc.injectsocks.InjectSockstTransformerImpl.java

License:Apache License

public void printClassByteCode(byte code[]) {
    ClassReader cr = new ClassReader(code);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);// ww  w.j a  v a2  s.c  om

    @SuppressWarnings("unchecked")
    List<MethodNode> methods = (List<MethodNode>) cn.methods;
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        InsnList instructions = method.instructions;
        if (instructions.size() <= 0)
            continue;

        System.out.println("Method: " + method.name + " ");
        for (int j = 0; j < instructions.size(); ++j) {
            AbstractInsnNode insn = (AbstractInsnNode) instructions.get(j);
            System.out.println(
                    "\tInsn: opc=" + OpcodeUtil.getOpcode(insn.getOpcode()) + ", type=" + insn.getType());
            if (insn.getType() == AbstractInsnNode.METHOD_INSN) {
                MethodInsnNode min = (MethodInsnNode) insn;
                System.out.printf("\t\towner=%s, name=%s, desc=%s\n", min.owner, min.name, min.desc);
            }
        }
    }
}