Search in sources :

Example 91 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.

the class FinallyTest method getTagsWithGotos.

private Set<String> getTagsWithGotos() throws IOException {
    final Set<String> gotoTags = new HashSet<String>();
    byte[] b = TargetLoader.getClassDataAsBytes(FinallyTarget.class);
    final ClassNode classNode = new ClassNode();
    InstrSupport.classReaderFor(b).accept(classNode, 0);
    for (final MethodNode m : classNode.methods) {
        if ("main".equals(m.name)) {
            // skip it
            continue;
        }
        int lineNumber = -1;
        for (AbstractInsnNode i : m.instructions) {
            if (AbstractInsnNode.LINE == i.getType()) {
                lineNumber = ((LineNumberNode) i).line;
            }
            if (Opcodes.GOTO == i.getOpcode()) {
                String tag = tags.get(Integer.valueOf(lineNumber));
                if (tag == null) {
                    throw new AssertionError("No tag at line " + lineNumber);
                }
                gotoTags.add(tag);
            }
        }
    }
    return gotoTags;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) HashSet(java.util.HashSet)

Example 92 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.

the class MethodAnalyzer method accept.

@Override
public void accept(final MethodNode methodNode, final MethodVisitor methodVisitor) {
    methodVisitor.visitCode();
    for (final TryCatchBlockNode n : methodNode.tryCatchBlocks) {
        n.accept(methodVisitor);
    }
    for (final AbstractInsnNode i : methodNode.instructions) {
        currentNode = i;
        i.accept(methodVisitor);
    }
    methodVisitor.visitEnd();
}
Also used : TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 93 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Minechem by iopleke.

the class MinechemTransformer method replaceBytes.

private byte[] replaceBytes(Method method, CodeBlock codeBlock, byte[] data) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(data);
    classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
    MethodNode methodNode = getMethodByName(classNode, method);
    AbstractInsnNode pos = null;
    boolean delete = false;
    boolean done = false;
    int start = codeBlock.getLinesAfterStart();
    int end = codeBlock.getLinesAfterEnd() + 1;
    for (Iterator<AbstractInsnNode> itr = methodNode.instructions.iterator(); itr.hasNext(); ) {
        AbstractInsnNode node = itr.next();
        if (node instanceof LineNumberNode) {
            LineNumberNode lineNode = (LineNumberNode) node;
            if (lineNode.line >= codeBlock.getStartLine()) {
                delete = true;
            }
            if (lineNode.line >= codeBlock.getEndLine()) {
                done = true;
            }
        }
        if (delete) {
            if (done) {
                if (end-- > 0) {
                    methodNode.instructions.remove(node);
                    continue;
                }
                pos = node;
                break;
            }
            if (--start < 0) {
                methodNode.instructions.remove(node);
            }
        }
    }
    methodNode.instructions.insertBefore(pos, codeBlock.getInsnList());
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) LineNumberNode(org.objectweb.asm.tree.LineNumberNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) ClassWriter(org.objectweb.asm.ClassWriter)

Example 94 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project Minechem by iopleke.

the class MinechemTransformer method findInstructionNode.

private static AbstractInsnNode findInstructionNode(InstructionNode instructionNode, MethodNode methodNode) {
    boolean close = false;
    AbstractInsnNode result = null;
    for (Iterator<AbstractInsnNode> itr = methodNode.instructions.iterator(); itr.hasNext(); ) {
        AbstractInsnNode node = itr.next();
        if (node instanceof MethodInsnNode) {
            if (close) {
                if (((MethodInsnNode) node).name.equals(instructionNode.getBefore())) {
                    return instructionNode.replace ? node : result;
                } else {
                    close = false;
                }
            }
            if (((MethodInsnNode) node).name.equals(instructionNode.getAfter())) {
                close = true;
                result = node;
            }
        }
    }
    return result == null ? methodNode.instructions.getLast().getPrevious() : result;
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 95 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project flink by apache.

the class ModifiedASMAnalyzer method newControlFlowEdge.

@Override
protected void newControlFlowEdge(int insn, int successor) {
    try {
        if (jumpModificationState == PRE_STATE) {
            jumpModificationState = MOD_STATE;
        } else if (jumpModificationState == MOD_STATE) {
            // for a later merge
            if (jumpModification == IFEQ_MOD) {
                final int top = accessField(Analyzer.class, "top").getInt(this);
                final int[] queue = (int[]) accessField(Analyzer.class, "queue").get(this);
                final int tmp = queue[top - 2];
                queue[top - 2] = queue[top - 1];
                queue[top - 1] = tmp;
                eventInsn = queue[top - 2] - 1;
                final InsnList insns = (InsnList) accessField(Analyzer.class, "insns").get(this);
                // if yes this is loop structure
                if (insns.get(eventInsn) instanceof JumpInsnNode) {
                    jumpModificationState = WAIT_FOR_INSN_STATE;
                } else // no loop -> end of modification
                {
                    jumpModificationState = DO_NOTHING;
                }
            } else // this modification changes the merge priority of certain frames (the expression part of the IF)
            if (jumpModification == IFNE_MOD) {
                final Frame[] frames = (Frame[]) accessField(Analyzer.class, "frames").get(this);
                final Field indexField = accessField(AbstractInsnNode.class, "index");
                final InsnList insns = (InsnList) accessField(Analyzer.class, "insns").get(this);
                final AbstractInsnNode gotoInsnn = insns.get(successor - 1);
                // check for a loop
                if (gotoInsnn instanceof JumpInsnNode) {
                    jumpModificationState = WAIT_FOR_INSN_STATE;
                    // sets a merge priority for all instructions (the expression of the IF)
                    // from the label the goto instruction points to until the evaluation with IFEQ
                    final int idx = indexField.getInt(accessField(JumpInsnNode.class, "label").get(gotoInsnn));
                    for (int i = idx; i <= insn; i++) {
                        ((ModifiedASMFrame) frames[i]).mergePriority = true;
                    }
                    eventInsn = idx - 2;
                } else {
                    jumpModificationState = DO_NOTHING;
                }
            }
        } else // wait for the goto instruction
        if (jumpModificationState == WAIT_FOR_INSN_STATE && insn == eventInsn) {
            jumpModificationState = DO_NOTHING;
            final Frame[] frames = (Frame[]) accessField(Analyzer.class, "frames").get(this);
            // this ensures that local variables are kept
            if (jumpModification == IFEQ_MOD) {
                interpreter.rightMergePriority = true;
                final Field top = accessField(Frame.class, "top");
                top.setInt(frames[eventInsn], top.getInt(frames[eventInsn + 1]));
                frames[eventInsn + 1].merge(frames[eventInsn], interpreter);
            } else // finally set a merge priority for the last instruction of the loop (before the IF expression)
            if (jumpModification == IFNE_MOD) {
                ((ModifiedASMFrame) frames[eventInsn + 1]).mergePriority = true;
            }
        }
    } catch (Exception e) {
        throw new CodeAnalyzerException("Unable to do jump modifications.", e);
    }
}
Also used : Field(java.lang.reflect.Field) Frame(org.objectweb.asm.tree.analysis.Frame) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Aggregations

AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)185 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)68 MethodNode (org.objectweb.asm.tree.MethodNode)54 InsnList (org.objectweb.asm.tree.InsnList)53 InsnNode (org.objectweb.asm.tree.InsnNode)41 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)38 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)37 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)35 ClassNode (org.objectweb.asm.tree.ClassNode)33 LabelNode (org.objectweb.asm.tree.LabelNode)27 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 Test (org.junit.Test)25 Label (org.objectweb.asm.Label)25 ClassReader (org.objectweb.asm.ClassReader)23 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)23 HashSet (java.util.HashSet)16 Type (org.objectweb.asm.Type)15 Frame (org.objectweb.asm.tree.analysis.Frame)13 ArrayList (java.util.ArrayList)12 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)11