Search in sources :

Example 1 with FinallyIntermediate

use of org.candle.decompiler.intermediate.code.FinallyIntermediate 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 2 with FinallyIntermediate

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

the class BlockVisitor method visitTryIntermediate.

@Override
public void visitTryIntermediate(TryIntermediate line) {
    if (seen.contains(line)) {
        //do nothing.
        return;
    } else {
        seen.add(line);
    }
    //set current block...
    TryBlock tryBlock = new TryBlock(line);
    //add it as a child of current..
    this.current.addChild(tryBlock);
    //set the current...
    this.current = tryBlock;
    //now, get the nested blocks...
    List<AbstractIntermediate> successors = getUnseenSuccessors(line);
    AbstractIntermediate inner = null;
    List<AbstractIntermediate> catchBlocks = new LinkedList<AbstractIntermediate>();
    AbstractIntermediate finallyIntermediate = null;
    //find the non-catch/finally...
    for (AbstractIntermediate successor : successors) {
        if (successor instanceof CatchIntermediate) {
            catchBlocks.add(successor);
        } else if (successor instanceof FinallyIntermediate) {
            finallyIntermediate = successor;
        } else {
            if (inner != null) {
                throw new IllegalStateException("Inner direction already set.");
            }
            inner = successor;
        }
    }
    Collections.sort(catchBlocks, new IntermediateComparator());
    if (inner == null) {
        throw new IllegalStateException("Inner is not set.");
    }
    inner.accept(this);
    //set the current up.
    moveUp();
    for (AbstractIntermediate catchBlock : catchBlocks) {
        current = tryBlock;
        catchBlock.accept(this);
    }
    if (finallyIntermediate != null) {
        current = tryBlock;
        finallyIntermediate.accept(this);
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) CatchIntermediate(org.candle.decompiler.intermediate.code.CatchIntermediate) FinallyIntermediate(org.candle.decompiler.intermediate.code.FinallyIntermediate) TryBlock(org.candle.decompiler.ast.tcf.TryBlock) IntermediateComparator(org.candle.decompiler.intermediate.code.IntermediateComparator) LinkedList(java.util.LinkedList)

Example 3 with FinallyIntermediate

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

the class IntermediateTryCatch method process.

public void process() {
    Set<BlockRange> tryBlock = new TreeSet<BlockRange>(new BlockRangeComparator());
    Map<BlockRange, List<CodeExceptionGen>> tryRangeGen = new HashMap<BlockRange, List<CodeExceptionGen>>();
    Map<InstructionHandle, List<CodeExceptionGen>> tryRangeFinally = new HashMap<InstructionHandle, List<CodeExceptionGen>>();
    //create try statements...
    for (CodeExceptionGen ceg : method.getExceptionHandlers()) {
        InstructionHandle min = ceg.getStartPC();
        InstructionHandle max = ceg.getEndPC();
        BlockRange tryRange = new BlockRange();
        tryRange.setStart(min);
        tryRange.setEnd(max);
        AbstractIntermediate handle = igc.findNextNode(ceg.getHandlerPC());
        LOG.debug("RANGE: " + ceg);
        LOG.debug("Range: " + tryRange + " , Target: " + handle.getInstruction().getPosition() + " , Handle: " + handle.getInstruction());
        if (ceg.getCatchType() == null) {
            if (!tryRangeFinally.containsKey(ceg.getHandlerPC())) {
                tryRangeFinally.put(ceg.getHandlerPC(), new LinkedList<CodeExceptionGen>());
            }
            tryRangeFinally.get(ceg.getHandlerPC()).add(ceg);
            continue;
        }
        tryBlock.add(tryRange);
        if (!tryRangeGen.containsKey(tryRange)) {
            tryRangeGen.put(tryRange, new LinkedList<CodeExceptionGen>());
        }
        tryRangeGen.get(tryRange).add(ceg);
    }
    for (BlockRange tryRange : tryBlock) {
        //create try block... create each catch block... link the two together for graph sake.
        //look up block...
        InstructionHandle start = tryRange.getStart();
        TryIntermediate tryIntermediate = new TryIntermediate(start);
        tryIntermediate.getBlockRange().setStart(tryRange.getStart());
        tryIntermediate.getBlockRange().setEnd(tryRange.getEnd());
        igc.getGraph().addVertex(tryIntermediate);
        //add line between try and node.
        AbstractIntermediate tryFirst = igc.findNextNode(start);
        igc.redirectPredecessors(tryFirst, tryIntermediate);
        igc.getGraph().addEdge(tryIntermediate, tryFirst);
        if (tryRangeGen.containsKey(tryRange)) {
            //create catch statements...
            for (CodeExceptionGen ceg : tryRangeGen.get(tryRange)) {
                generateCatch(tryIntermediate, ceg);
            }
        }
    }
    //create a finally node for each handle of finally & link
    for (InstructionHandle finallyTargetHandle : tryRangeFinally.keySet()) {
        //get reference to target...
        AbstractIntermediate finallyTargetNode = igc.findNextNode(finallyTargetHandle);
        //change the instruction to a finally...
        FinallyIntermediate finallyIntermediate = new FinallyIntermediate(finallyTargetNode.getInstruction(), new HashSet<CodeExceptionGen>(tryRangeFinally.get(finallyTargetHandle)));
        igc.getGraph().addVertex(finallyIntermediate);
        //now, we need to redirect from the existing throws to finally.
        igc.redirectSuccessors(finallyTargetNode, finallyIntermediate);
        //retract existing.
        igc.getGraph().removeVertex(finallyTargetNode);
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) HashMap(java.util.HashMap) BlockRangeComparator(org.candle.decompiler.intermediate.code.BlockRangeComparator) TryIntermediate(org.candle.decompiler.intermediate.code.TryIntermediate) InstructionHandle(org.apache.bcel.generic.InstructionHandle) BlockRange(org.candle.decompiler.intermediate.code.BlockRange) TreeSet(java.util.TreeSet) FinallyIntermediate(org.candle.decompiler.intermediate.code.FinallyIntermediate) List(java.util.List) LinkedList(java.util.LinkedList) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

Aggregations

AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)3 FinallyIntermediate (org.candle.decompiler.intermediate.code.FinallyIntermediate)3 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 CatchIntermediate (org.candle.decompiler.intermediate.code.CatchIntermediate)2 TryIntermediate (org.candle.decompiler.intermediate.code.TryIntermediate)2 List (java.util.List)1 TreeSet (java.util.TreeSet)1 CodeExceptionGen (org.apache.bcel.generic.CodeExceptionGen)1 InstructionHandle (org.apache.bcel.generic.InstructionHandle)1 TryBlock (org.candle.decompiler.ast.tcf.TryBlock)1 BlockRange (org.candle.decompiler.intermediate.code.BlockRange)1 BlockRangeComparator (org.candle.decompiler.intermediate.code.BlockRangeComparator)1 IntermediateComparator (org.candle.decompiler.intermediate.code.IntermediateComparator)1