Search in sources :

Example 11 with TryCatchBlockNode

use of org.objectweb.asm.tree.TryCatchBlockNode 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 12 with TryCatchBlockNode

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

the class TryWithResourcesJavac11Filter method filter.

public void filter(final MethodNode methodNode, final IFilterContext context, final IFilterOutput output) {
    if (methodNode.tryCatchBlocks.isEmpty()) {
        return;
    }
    final Matcher matcher = new Matcher();
    for (TryCatchBlockNode t : methodNode.tryCatchBlocks) {
        if ("java/lang/Throwable".equals(t.type)) {
            matcher.match(t.handler, output, true);
            matcher.match(t.handler, output, false);
        }
    }
}
Also used : TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode)

Example 13 with TryCatchBlockNode

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

the class TryWithResourcesEcjFilter method filter.

public void filter(final String className, final String superClassName, final MethodNode methodNode, final IFilterOutput output) {
    if (methodNode.tryCatchBlocks.isEmpty()) {
        return;
    }
    final Matcher matcher = new Matcher(output);
    for (TryCatchBlockNode t : methodNode.tryCatchBlocks) {
        if (t.type == null) {
            matcher.start(t.handler);
            if (!matcher.matchEcj()) {
                matcher.start(t.handler);
                matcher.matchEcjNoFlowOut();
            }
        }
    }
}
Also used : TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode)

Example 14 with TryCatchBlockNode

use of org.objectweb.asm.tree.TryCatchBlockNode in project soot by Sable.

the class AsmMethodSource method emitTraps.

private void emitTraps() {
    Chain<Trap> traps = body.getTraps();
    SootClass throwable = Scene.v().getSootClass("java.lang.Throwable");
    Map<LabelNode, Iterator<UnitBox>> handlers = new HashMap<LabelNode, Iterator<UnitBox>>(tryCatchBlocks.size());
    for (TryCatchBlockNode tc : tryCatchBlocks) {
        UnitBox start = Jimple.v().newStmtBox(null);
        UnitBox end = Jimple.v().newStmtBox(null);
        Iterator<UnitBox> hitr = handlers.get(tc.handler);
        if (hitr == null) {
            hitr = trapHandlers.get(tc.handler).iterator();
            handlers.put(tc.handler, hitr);
        }
        UnitBox handler = hitr.next();
        SootClass cls = tc.type == null ? throwable : Scene.v().getSootClass(AsmUtil.toQualifiedName(tc.type));
        Trap trap = Jimple.v().newTrap(cls, start, end, handler);
        traps.add(trap);
        labels.put(tc.start, start);
        labels.put(tc.end, end);
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) UnitBox(soot.UnitBox) HashMap(java.util.HashMap) Iterator(java.util.Iterator) Trap(soot.Trap) SootClass(soot.SootClass)

Example 15 with TryCatchBlockNode

use of org.objectweb.asm.tree.TryCatchBlockNode in project jphp by jphp-compiler.

the class TryCatchCompiler method write.

@Override
public void write(TryStmtToken token) {
    if (token.getBody() == null || token.getBody().getInstructions().isEmpty()) {
        if (token.getFinally() != null)
            expr.write(BodyStmtToken.class, token.getFinally());
        return;
    }
    expr.writeDefineVariables(token.getLocal());
    LabelNode tryStart = expr.writeLabel(node, token.getMeta().getStartLine());
    LabelNode tryEnd = new LabelNode();
    LabelNode catchStart = new LabelNode();
    LabelNode catchEnd = new LabelNode();
    LabelNode returnLabel = new LabelNode();
    method.node.tryCatchBlocks.add(0, new TryCatchBlockNode(tryStart, tryEnd, catchStart, Type.getInternalName(BaseBaseException.class)));
    if (token.getFinally() != null) {
        method.getTryStack().push(new MethodStmtCompiler.TryCatchItem(token, returnLabel));
    }
    expr.write(BodyStmtToken.class, token.getBody());
    if (token.getFinally() != null) {
        method.getTryStack().pop();
    }
    add(tryEnd);
    add(new JumpInsnNode(GOTO, catchEnd));
    add(catchStart);
    LocalVariable exception = method.addLocalVariable("~catch~" + method.nextStatementIndex(BaseException.class), catchStart, BaseBaseException.class);
    exception.setEndLabel(catchEnd);
    expr.makeVarStore(exception);
    LabelNode nextCatch = null;
    int i = 0, size = token.getCatches().size();
    LocalVariable local = null;
    LabelNode catchFail = new LabelNode();
    for (CatchStmtToken _catch : token.getCatches()) {
        if (nextCatch != null) {
            add(nextCatch);
        }
        if (i == size - 1) {
            nextCatch = catchFail;
        } else {
            nextCatch = new LabelNode();
        }
        local = method.getLocalVariable(_catch.getVariable().getName());
        List<FulledNameToken> catchExceptions = _catch.getExceptions();
        LabelNode bodyLabel = new LabelNode();
        LabelNode nextCatchLocal = new LabelNode();
        int j = 0;
        for (FulledNameToken catchException : catchExceptions) {
            if (j > 0) {
                add(nextCatchLocal);
                nextCatchLocal = new LabelNode();
            }
            expr.writePushEnv();
            expr.writeVarLoad(exception);
            expr.writePushConstString(catchException.toName());
            expr.writePushConstString(catchException.toName().toLowerCase());
            expr.writeSysDynamicCall(Environment.class, "__throwCatch", Memory.class, BaseBaseException.class, String.class, String.class);
            expr.writeVarAssign(local, _catch.getVariable(), true, false);
            expr.writePopBoolean();
            add(new JumpInsnNode(IFEQ, j == catchExceptions.size() - 1 ? nextCatch : nextCatchLocal));
            expr.stackPop();
            if (catchExceptions.size() > 1) {
                add(new JumpInsnNode(GOTO, bodyLabel));
            }
            j++;
        }
        if (catchExceptions.size() > 1) {
            add(bodyLabel);
        }
        expr.write(BodyStmtToken.class, _catch.getBody());
        add(new JumpInsnNode(GOTO, catchEnd));
        i++;
    }
    add(catchFail);
    if (token.getFinally() != null) {
        expr.write(BodyStmtToken.class, token.getFinally());
    }
    /*if (method.getTryStack().empty()) {
            expr.writePushEnv();
            expr.writeVarLoad(exception);
            expr.writeSysDynamicCall(Environment.class, "__throwFailedCatch", void.class, BaseException.class);
        } else {*/
    expr.makeVarLoad(exception);
    add(new InsnNode(ATHROW));
    // }
    add(catchEnd);
    if (token.getFinally() != null) {
        LabelNode skip = new LabelNode();
        add(new JumpInsnNode(GOTO, skip));
        // finally for return
        add(returnLabel);
        expr.write(BodyStmtToken.class, token.getFinally());
        if (method.getTryStack().empty()) {
            // all finally blocks are done
            LocalVariable retVar = method.getOrAddLocalVariable("~result~", null, Memory.class);
            expr.writeVarLoad(retVar);
            add(new InsnNode(ARETURN));
            expr.stackPop();
        } else {
            // goto next finally block
            add(new JumpInsnNode(GOTO, method.getTryStack().peek().getReturnLabel()));
        }
        add(skip);
        // other finally
        expr.write(BodyStmtToken.class, token.getFinally());
    }
    expr.writeUndefineVariables(token.getLocal());
    method.prevStatementIndex(BaseBaseException.class);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) MethodStmtCompiler(org.develnext.jphp.core.compiler.jvm.statement.MethodStmtCompiler) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) CatchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken) FulledNameToken(org.develnext.jphp.core.tokenizer.token.expr.value.FulledNameToken)

Aggregations

TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)27 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)11 InsnNode (org.objectweb.asm.tree.InsnNode)5 LabelNode (org.objectweb.asm.tree.LabelNode)5 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)5 MethodNode (org.objectweb.asm.tree.MethodNode)5 Label (org.objectweb.asm.Label)4 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)4 Frame (org.objectweb.asm.tree.analysis.Frame)4 ArrayList (java.util.ArrayList)3 Type (org.objectweb.asm.Type)3 FrameNode (org.objectweb.asm.tree.FrameNode)3 InsnList (org.objectweb.asm.tree.InsnList)3 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)3 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Analyzer (org.objectweb.asm.tree.analysis.Analyzer)2 Textifier (org.objectweb.asm.util.Textifier)2 TraceMethodVisitor (org.objectweb.asm.util.TraceMethodVisitor)2