|
| 1 | +package gr.gousiosg.callgraph; |
| 2 | + |
| 3 | +import org.apache.bcel.classfile.JavaClass; |
| 4 | +import org.apache.bcel.generic.ArrayInstruction; |
| 5 | +import org.apache.bcel.generic.CHECKCAST; |
| 6 | +import org.apache.bcel.generic.CodeExceptionGen; |
| 7 | +import org.apache.bcel.generic.ConstantPoolGen; |
| 8 | +import org.apache.bcel.generic.ConstantPushInstruction; |
| 9 | +import org.apache.bcel.generic.EmptyVisitor; |
| 10 | +import org.apache.bcel.generic.FieldInstruction; |
| 11 | +import org.apache.bcel.generic.INSTANCEOF; |
| 12 | +import org.apache.bcel.generic.Instruction; |
| 13 | +import org.apache.bcel.generic.InstructionConstants; |
| 14 | +import org.apache.bcel.generic.InstructionHandle; |
| 15 | +import org.apache.bcel.generic.InvokeInstruction; |
| 16 | +import org.apache.bcel.generic.LocalVariableInstruction; |
| 17 | +import org.apache.bcel.generic.MethodGen; |
| 18 | +import org.apache.bcel.generic.ReturnInstruction; |
| 19 | +import org.apache.bcel.generic.Type; |
| 20 | + |
| 21 | +public class MethodVisitor extends EmptyVisitor { |
| 22 | + |
| 23 | + JavaClass visitedClass; |
| 24 | + private MethodGen mg; |
| 25 | + private ConstantPoolGen cp; |
| 26 | + |
| 27 | + public MethodVisitor(MethodGen m, JavaClass jc) { |
| 28 | + visitedClass = jc; |
| 29 | + mg = m; |
| 30 | + cp = mg.getConstantPool(); |
| 31 | + } |
| 32 | + |
| 33 | + public void start() { |
| 34 | + if (!mg.isAbstract() && !mg.isNative()) { |
| 35 | + for (InstructionHandle ih = mg.getInstructionList().getStart(); ih != null; ih = ih.getNext()) { |
| 36 | + Instruction i = ih.getInstruction(); |
| 37 | + |
| 38 | + if (!visitInstruction(i)) |
| 39 | + i.accept(this); |
| 40 | + } |
| 41 | + updateExceptionHandlers(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** Visit a single instruction. */ |
| 46 | + private boolean visitInstruction(Instruction i) { |
| 47 | + short opcode = i.getOpcode(); |
| 48 | + |
| 49 | + return ((InstructionConstants.INSTRUCTIONS[opcode] != null) |
| 50 | + && !(i instanceof ConstantPushInstruction) && !(i instanceof ReturnInstruction)); |
| 51 | + } |
| 52 | + |
| 53 | + /** Local variable use. */ |
| 54 | + public void visitLocalVariableInstruction(LocalVariableInstruction i) { |
| 55 | + //if (i.getOpcode() != Constants.IINC) |
| 56 | + //cv.registerCoupling(i.getType(cp)); |
| 57 | + } |
| 58 | + |
| 59 | + /** Array use. */ |
| 60 | + public void visitArrayInstruction(ArrayInstruction i) { |
| 61 | + //cv.registerCoupling(i.getType(cp)); |
| 62 | + } |
| 63 | + |
| 64 | + /** Field access. */ |
| 65 | + public void visitFieldInstruction(FieldInstruction i) { |
| 66 | + //cv.registerFieldAccess(i.getClassName(cp), i.getFieldName(cp)); |
| 67 | + //cv.registerCoupling(i.getFieldType(cp)); |
| 68 | + } |
| 69 | + |
| 70 | + /** Method invocation. */ |
| 71 | + public void visitInvokeInstruction(InvokeInstruction i) { |
| 72 | + System.out.println(visitedClass.getClassName() + ":" + mg.getName() + "->" + i.getReferenceType(cp) + ":"+ i.getMethodName(cp)); |
| 73 | + //Type[] argTypes = i.getArgumentTypes(cp); |
| 74 | + //for (int j = 0; j < argTypes.length; j++) |
| 75 | + // cv.registerCoupling(argTypes[j]); |
| 76 | + //cv.registerCoupling(i.getReturnType(cp)); |
| 77 | + /* Measuring decision: measure overloaded methods separately */ |
| 78 | + //cv.registerMethodInvocation(i.getClassName(cp), i.getMethodName(cp), |
| 79 | + // argTypes); |
| 80 | + } |
| 81 | + |
| 82 | + /** Visit an instanceof instruction. */ |
| 83 | + public void visitINSTANCEOF(INSTANCEOF i) { |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + /** Visit checklast instruction. */ |
| 88 | + public void visitCHECKCAST(CHECKCAST i) { |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + /** Visit return instruction. */ |
| 93 | + public void visitReturnInstruction(ReturnInstruction i) { |
| 94 | + } |
| 95 | + |
| 96 | + /** Visit the method's exception handlers. */ |
| 97 | + private void updateExceptionHandlers() { |
| 98 | + CodeExceptionGen[] handlers = mg.getExceptionHandlers(); |
| 99 | + |
| 100 | + /* Measuring decision: couple exceptions */ |
| 101 | + for (int i = 0; i < handlers.length; i++) { |
| 102 | + Type t = handlers[i].getCatchType(); |
| 103 | + //if (t != null) |
| 104 | + //cv.registerCoupling(t); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments