Search in sources :

Example 1 with CatchIntermediate

use of org.candle.decompiler.intermediate.code.CatchIntermediate in project candle-decompiler by bradsdavis.

the class IntermediateTryCatch method generateCatch.

private void generateCatch(TryIntermediate tryIntermediate, CodeExceptionGen ceg) {
    LOG.debug("CEG: " + ceg);
    //convert the node to catch blocks...
    AbstractIntermediate catchDeclaration = igc.getOrderedIntermediate().ceiling(new NullIntermediate(ceg.getHandlerPC()));
    LOG.debug("Catch Declaration:" + catchDeclaration);
    if (catchDeclaration instanceof StatementIntermediate) {
        StatementIntermediate declarationStatement = (StatementIntermediate) catchDeclaration;
        if (declarationStatement.getExpression() instanceof Declaration) {
            Declaration declaration = (Declaration) declarationStatement.getExpression();
            //now, we can convert this into a catch block.
            CatchIntermediate catchIntermediate = new CatchIntermediate(declarationStatement.getInstruction(), ceg, declaration.getVariable());
            igc.getGraph().addVertex(catchIntermediate);
            //redirect statement to catch.
            igc.redirectPredecessors(declarationStatement, catchIntermediate);
            igc.redirectSuccessors(declarationStatement, catchIntermediate);
            //now, we just need to remove the statement.
            igc.getGraph().removeVertex(declarationStatement);
            //populate the bounds..
            //add the link between try and catch.
            igc.getGraph().addEdge(tryIntermediate, catchIntermediate);
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) CatchIntermediate(org.candle.decompiler.intermediate.code.CatchIntermediate) Declaration(org.candle.decompiler.intermediate.expression.Declaration) NullIntermediate(org.candle.decompiler.intermediate.graph.context.NullIntermediate)

Example 2 with CatchIntermediate

use of org.candle.decompiler.intermediate.code.CatchIntermediate in project candle-decompiler by bradsdavis.

the class IntermediateGraphContext method getCatchClauses.

public List<CatchIntermediate> getCatchClauses(TryIntermediate tryIntermediate) {
    //of all the successors, get the catch...
    List<AbstractIntermediate> candidates = Graphs.successorListOf(graph, tryIntermediate);
    List<CatchIntermediate> catchClauses = new ArrayList<CatchIntermediate>();
    for (AbstractIntermediate candidate : candidates) {
        if (candidate instanceof CatchIntermediate) {
            catchClauses.add((CatchIntermediate) candidate);
        }
    }
    return catchClauses;
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) CatchIntermediate(org.candle.decompiler.intermediate.code.CatchIntermediate) ArrayList(java.util.ArrayList)

Example 3 with CatchIntermediate

use of org.candle.decompiler.intermediate.code.CatchIntermediate in project candle-decompiler by bradsdavis.

the class RetractDuplicateFinally method visitFinallyIntermediate.

@Override
public void visitFinallyIntermediate(FinallyIntermediate line) {
    //TODO: Remove the highest finally statements first; won't fit this visitor
    //pattern.
    //get the bounds of the finally... associate the try.
    //finally is part of the try if the try + catch bounds has:
    //Try[0,13]
    //Catch[19,50]
    //Catch[56,65]
    //Finally | Handler[56, 65]| Handler[0, 42] |  | Range[66, 76]
    //in the case above, finally should match because lower bound == finally lower handler bound.
    //and upper bound of Catch matches upper bound of Finally Handler.
    //first, match the Try block that applies...
    //get lowest bound.
    InstructionHandle min = getLowestBound(line.getCodeExceptions());
    InstructionHandle max = getHighestBound(line.getCodeExceptions());
    LOG.debug("Finally Range: " + min.getPosition() + " -> " + max.getPosition());
    TryIntermediate matched = matchTryBlock(min, max);
    if (matched != null) {
        final Set<Integer> offsets = collectOffsets(line);
        //ok, now we need to eliminate finally blocks from this try and all of the catches.  thanks Java!
        List<CatchIntermediate> catchClauses = igc.getCatchClauses(matched);
        //for each catch clause...
        for (CatchIntermediate catchClause : catchClauses) {
            processCatch(catchClause, line, offsets);
        }
        processTry(matched, line, offsets);
    }
    //now, add the edge between end of FINALLY and the next statement.
    InstructionHandle finallyEnd = line.getBlockRange().getEnd();
    //get the next.. then search for next node in graph.
    AbstractIntermediate finallyLast = igc.findNextNode(finallyEnd);
    AbstractIntermediate afterFinally = igc.findNextNode(finallyEnd.getNext());
    igc.redirectPredecessors(finallyLast, afterFinally);
    igc.getGraph().removeVertex(finallyLast);
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) CatchIntermediate(org.candle.decompiler.intermediate.code.CatchIntermediate) TryIntermediate(org.candle.decompiler.intermediate.code.TryIntermediate) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 4 with CatchIntermediate

use of org.candle.decompiler.intermediate.code.CatchIntermediate in project candle-decompiler by bradsdavis.

the class IntermediateEdgeAttributeProvider method getComponentAttributes.

@Override
public Map<String, String> getComponentAttributes(IntermediateEdge edge) {
    Map<String, String> attributes = new HashMap<String, String>();
    AbstractIntermediate source = (AbstractIntermediate) edge.getSource();
    AbstractIntermediate target = (AbstractIntermediate) edge.getTarget();
    if (source instanceof TryIntermediate && (target instanceof CatchIntermediate || target instanceof FinallyIntermediate)) {
        attributes.put("style", "dashed");
    }
    return attributes;
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) HashMap(java.util.HashMap) CatchIntermediate(org.candle.decompiler.intermediate.code.CatchIntermediate) FinallyIntermediate(org.candle.decompiler.intermediate.code.FinallyIntermediate) TryIntermediate(org.candle.decompiler.intermediate.code.TryIntermediate)

Example 5 with CatchIntermediate

use of org.candle.decompiler.intermediate.code.CatchIntermediate in project candle-decompiler by bradsdavis.

the class CatchUpperRangeVisitor method processLastCatch.

public void processLastCatch(CatchIntermediate line) {
    TryIntermediate tryBlock = (TryIntermediate) igc.getSinglePredecessor(line);
    if (igc.getFinallyClause(tryBlock) != null) {
        return;
    }
    //now, we are going to look for the block jump.
    InstructionHandle ih = tryBlock.getBlockRange().getEnd();
    //see about a goto.
    GoToIntermediate gotoHandle = (GoToIntermediate) igc.findNextNode(ih.getNext());
    TreeSet<AbstractIntermediate> ordered = new TreeSet<AbstractIntermediate>(new IntermediateComparator());
    //no finally clause...
    ordered.addAll(igc.getCatchClauses(tryBlock));
    AbstractIntermediate target = igc.getTarget(gotoHandle);
    //now, look backwards and find the non-GOTO statement.
    List<AbstractIntermediate> candidates = Graphs.predecessorListOf(igc.getGraph(), target);
    Set<AbstractIntermediate> elements = new HashSet<AbstractIntermediate>();
    for (AbstractIntermediate candidate : candidates) {
        if (!(candidate instanceof GoToIntermediate)) {
            elements.add(candidate);
        }
    }
    for (AbstractIntermediate element : elements) {
        LOG.debug("Element: " + element + " Position: " + element.getInstruction().getPosition());
    }
    if (elements.size() == 1) {
        if (ordered.last() instanceof CatchIntermediate) {
            CatchIntermediate ci = (CatchIntermediate) ordered.last();
            ci.getBlockRange().setEnd(elements.iterator().next().getInstruction());
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) GoToIntermediate(org.candle.decompiler.intermediate.code.GoToIntermediate) TreeSet(java.util.TreeSet) CatchIntermediate(org.candle.decompiler.intermediate.code.CatchIntermediate) TryIntermediate(org.candle.decompiler.intermediate.code.TryIntermediate) IntermediateComparator(org.candle.decompiler.intermediate.code.IntermediateComparator) InstructionHandle(org.apache.bcel.generic.InstructionHandle) HashSet(java.util.HashSet)

Aggregations

AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)6 CatchIntermediate (org.candle.decompiler.intermediate.code.CatchIntermediate)6 TryIntermediate (org.candle.decompiler.intermediate.code.TryIntermediate)3 InstructionHandle (org.apache.bcel.generic.InstructionHandle)2 FinallyIntermediate (org.candle.decompiler.intermediate.code.FinallyIntermediate)2 IntermediateComparator (org.candle.decompiler.intermediate.code.IntermediateComparator)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 TryBlock (org.candle.decompiler.ast.tcf.TryBlock)1 GoToIntermediate (org.candle.decompiler.intermediate.code.GoToIntermediate)1 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)1 Declaration (org.candle.decompiler.intermediate.expression.Declaration)1 NullIntermediate (org.candle.decompiler.intermediate.graph.context.NullIntermediate)1