Search in sources :

Example 6 with CodeExceptionGen

use of org.apache.bcel.generic.CodeExceptionGen in project candle-decompiler by bradsdavis.

the class ExceptionEdgeEnhancer method process.

@Override
public void process() {
    Set<Integer> positions = new HashSet<Integer>();
    for (CodeExceptionGen ceg : exceptions) {
        int t1 = ceg.getStartPC().getPosition();
        int t2 = ceg.getHandlerPC().getPosition();
        positions.add(t1);
        positions.add(t2);
    }
    Map<Integer, InstructionHandle> ivc = new HashMap<Integer, InstructionHandle>();
    for (InstructionHandle iv : igc.getGraph().vertexSet()) {
        if (positions.contains(iv.getPosition())) {
            ivc.put(iv.getPosition(), iv);
        }
    }
    //now, we will map the CEG.
    for (CodeExceptionGen ceg : exceptions) {
        int t1 = ceg.getStartPC().getPosition();
        int t2 = ceg.getHandlerPC().getPosition();
        InstructionHandle source = ivc.get(t1);
        InstructionHandle target = ivc.get(t2);
        IntermediateEdge ie = new IntermediateEdge();
        addExceptionHandle(ie, ceg);
        ie.setType(EdgeType.EXCEPTION);
        igc.getGraph().addEdge(source, target, ie);
    }
}
Also used : HashMap(java.util.HashMap) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle) HashSet(java.util.HashSet)

Example 7 with CodeExceptionGen

use of org.apache.bcel.generic.CodeExceptionGen 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)

Example 8 with CodeExceptionGen

use of org.apache.bcel.generic.CodeExceptionGen in project jop by jop-devel.

the class FindUsedConstants method find.

private void find(Method method) {
    MethodGen mg = new MethodGen(method, clazz.getClassName(), cpool);
    InstructionList il = mg.getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    // find instructions that access the constant pool
    // collect all indices to constants in ClassInfo
    String cpInstr = "CPInstruction";
    for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) it.next();
        InstructionHandle first = match[0];
        CPInstruction ii = (CPInstruction) first.getInstruction();
        int idx = ii.getIndex();
        Constant co = cpool.getConstant(idx);
        int len = 1;
        switch(co.getTag()) {
            case Constants.CONSTANT_Long:
            case Constants.CONSTANT_Double:
                len = 2;
                break;
        }
        // we don't need the field references in the cpool anymore
        if (co.getTag() != Constants.CONSTANT_Fieldref) {
            getCli().addUsedConst(idx, len);
        }
    // also modify the index!
    //			Constant cnst = cpool.getConstant(ii.getIndex());
    //			int newIndex = addConstant(cnst);
    //System.out.println(ii+" -> "+newIndex);
    //			ii.setIndex(newIndex);			
    }
    il.dispose();
    CodeExceptionGen[] et = mg.getExceptionHandlers();
    for (int i = 0; i < et.length; i++) {
        ObjectType ctype = et[i].getCatchType();
        if (ctype != null) {
            getCli().addUsedConst(cpool.lookupClass(ctype.getClassName()), 1);
        }
    }
}
Also used : InstructionList(org.apache.bcel.generic.InstructionList) Constant(org.apache.bcel.classfile.Constant) InstructionFinder(org.apache.bcel.util.InstructionFinder) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) CPInstruction(org.apache.bcel.generic.CPInstruction) ObjectType(org.apache.bcel.generic.ObjectType) Iterator(java.util.Iterator) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

Example 9 with CodeExceptionGen

use of org.apache.bcel.generic.CodeExceptionGen in project jop by jop-devel.

the class InstructionInterpreter method interpret.

public void interpret(boolean initialize) {
    InstructionList il = methodInfo.getCode().getInstructionList(true, false);
    InstructionHandle entry = il.getStart();
    Map<InstructionHandle, T> start = new HashMap<InstructionHandle, T>();
    start.put(entry, initialize ? analysis.initial(entry) : analysis.bottom());
    // start at exception handler entries too?
    if (startAtExceptionHandlers) {
        for (CodeExceptionGen eg : methodInfo.getCode().getExceptionHandlers()) {
            InstructionHandle ih = eg.getHandlerPC();
            start.put(ih, initialize ? analysis.initial(eg) : analysis.bottom());
        }
    }
    interpret(il, start, initialize);
}
Also used : RET(org.apache.bcel.generic.RET) HashMap(java.util.HashMap) InstructionList(org.apache.bcel.generic.InstructionList) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 10 with CodeExceptionGen

use of org.apache.bcel.generic.CodeExceptionGen in project jop by jop-devel.

the class MethodCode method getStartingExceptionRanges.

/**
     * Get a list of all exception ranges which start at this instruction.
     * @param ih the instruction to check
     * @return a list of all exception ranges with this instruction as start instruction, or an empty list.
     */
public List<CodeExceptionGen> getStartingExceptionRanges(InstructionHandle ih) {
    List<CodeExceptionGen> list = new ArrayList<CodeExceptionGen>();
    InstructionTargeter[] targeters = ih.getTargeters();
    if (targeters == null)
        return list;
    for (InstructionTargeter t : targeters) {
        if (t instanceof CodeExceptionGen) {
            CodeExceptionGen ceg = (CodeExceptionGen) t;
            if (ceg.getStartPC().equals(ih)) {
                list.add(ceg);
            }
        }
    }
    return list;
}
Also used : InstructionTargeter(org.apache.bcel.generic.InstructionTargeter) ArrayList(java.util.ArrayList) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

Aggregations

CodeExceptionGen (org.apache.bcel.generic.CodeExceptionGen)12 InstructionHandle (org.apache.bcel.generic.InstructionHandle)6 HashMap (java.util.HashMap)4 InstructionTargeter (org.apache.bcel.generic.InstructionTargeter)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 List (java.util.List)2 InstructionList (org.apache.bcel.generic.InstructionList)2 RET (org.apache.bcel.generic.RET)2 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)2 MethodCode (com.jopdesign.common.MethodCode)1 StringWriter (java.io.StringWriter)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 TreeSet (java.util.TreeSet)1 Constant (org.apache.bcel.classfile.Constant)1 CPInstruction (org.apache.bcel.generic.CPInstruction)1 LineNumberGen (org.apache.bcel.generic.LineNumberGen)1 LocalVariableGen (org.apache.bcel.generic.LocalVariableGen)1 MethodGen (org.apache.bcel.generic.MethodGen)1