/** * Get CodeException object.<br> * This relies on that the instruction list has already been dumped to byte code or or that the * `setPositions' methods has been called for the instruction list. * * @param cp constant pool */ public CodeException getCodeException(ConstantPoolGen cp) { return new CodeException( start_pc.getPosition(), end_pc.getPosition() + end_pc.getInstruction().getLength(), handler_pc.getPosition(), (catch_type == null) ? 0 : cp.addClass(catch_type)); }
/** * Get LocalVariable object. * * <p>This relies on that the instruction list has already been dumped to byte code or or that the * `setPositions' methods has been called for the instruction list. * * <p>Note that for local variables whose scope end at the last instruction of the method's code, * the JVM specification is ambiguous: both a start_pc+length ending at the last instruction and * start_pc+length ending at first index beyond the end of the code are valid. * * @param il instruction list (byte code) which this variable belongs to * @param cp constant pool */ public LocalVariable getLocalVariable(ConstantPoolGen cp) { int start_pc = start.getPosition(); int length = end.getPosition() - start_pc; if (length > 0) length += end.getInstruction().getLength(); int name_index = cp.addUtf8(name); int signature_index = cp.addUtf8(type.getSignature()); return new LocalVariable( start_pc, length, name_index, signature_index, index, cp.getConstantPool()); }
CFG createCFG(String className) throws ClassNotFoundException { CFG cfg = new CFG(); JavaClass jc = Repository.lookupClass(className); ClassGen cg = new ClassGen(jc); ConstantPoolGen cpg = cg.getConstantPool(); for (Method m : cg.getMethods()) { MethodGen mg = new MethodGen(m, cg.getClassName(), cpg); InstructionList il = mg.getInstructionList(); InstructionHandle[] handles = il.getInstructionHandles(); int prev = 0; for (InstructionHandle ih : handles) { int position = ih.getPosition(); cfg.addNode(position, m, jc); Instruction inst = ih.getInstruction(); boolean br = inst.getName().contains("if") || inst.getName().contains("goto"); boolean ret = inst.getName().contains("return"); boolean stat = inst.getName().contains("invokestatic"); int len = inst.getLength(); if (stat) { int index = inst.toString(true).indexOf(" "); String name = inst.toString(true).substring(index + 1); int tar = Integer.valueOf(name); INVOKESTATIC inv = new INVOKESTATIC(tar); name = inv.getMethodName(cpg); Method m2 = null; Method[] tm = cg.getMethods(); for (int i = 0; i < tm.length; i++) { if (tm[i].getName().equals(name)) { m2 = tm[i]; } } cfg.addEdge(position, m, jc, 0, m2, jc); cfg.addEdge(-1, m2, jc, position + len, m, jc); } if (!ret && !stat) { cfg.addEdge(position, position + len, m, jc); } if (br) { cfg.addEdge(position, position + len, m, jc); IF_ICMPGE comp = new IF_ICMPGE(ih); String name = comp.getTarget().toString(false); int index = name.indexOf(">"); name = name.substring(index + 2); int tar = Integer.valueOf(name); cfg.addEdge(position, tar, m, jc); } if (ret) { cfg.addEdge(position, -1, m, jc); } prev = position; } System.out.println(cfg.toString()); } return cfg; }
/** * @param target branch target * @return the offset to `target' relative to this instruction */ protected int getTargetOffset(InstructionHandle target) { if (target == null) throw new ClassGenException("Target of " + super.toString(true) + " is invalid null handle"); int t = target.getPosition(); if (t < 0) throw new ClassGenException( "Invalid branch target position offset for " + super.toString(true) + ":" + t + ":" + target); return t - position; }
/** * Get LineNumber attribute . * * <p>This relies on that the instruction list has already been dumped to byte code or or that the * `setPositions' methods has been called for the instruction list. */ public LineNumber getLineNumber() { return new LineNumber(ih.getPosition(), src_line); }