java-callgraph

Remove extra code, formatting

5/27/2011 6:57:27 PM

Details

diff --git a/src/main/java/gr/gousiosg/callgraph/ClassVisitor.java b/src/main/java/gr/gousiosg/callgraph/ClassVisitor.java
index 347230a..3d58be9 100644
--- a/src/main/java/gr/gousiosg/callgraph/ClassVisitor.java
+++ b/src/main/java/gr/gousiosg/callgraph/ClassVisitor.java
@@ -6,14 +6,18 @@ import org.apache.bcel.classfile.Method;
 import org.apache.bcel.generic.ConstantPoolGen;
 import org.apache.bcel.generic.MethodGen;
 
+/**
+ * The simplest of class visitors, invokes the method visitor class for each
+ * method found.
+ */
 public class ClassVisitor extends EmptyVisitor {
 
-    JavaClass visitedClass;
-    private ConstantPoolGen cp;
+    JavaClass clazz;
+    private ConstantPoolGen constants;
 
     public ClassVisitor(JavaClass jc) {
-        visitedClass = jc;
-        cp = new ConstantPoolGen(visitedClass.getConstantPool());
+        clazz = jc;
+        constants = new ConstantPoolGen(clazz.getConstantPool());
     }
 
     public void visitJavaClass(JavaClass jc) {
@@ -23,12 +27,12 @@ public class ClassVisitor extends EmptyVisitor {
     }
 
     public void visitMethod(Method method) {
-        MethodGen mg = new MethodGen(method, visitedClass.getClassName(), cp);
-        MethodVisitor visitor = new MethodVisitor(mg, visitedClass);
+        MethodGen mg = new MethodGen(method, clazz.getClassName(), constants);
+        MethodVisitor visitor = new MethodVisitor(mg, clazz);
         visitor.start(); 
     }
     
     public void start() {
-        visitJavaClass(visitedClass);
+        visitJavaClass(clazz);
     }
 }
diff --git a/src/main/java/gr/gousiosg/callgraph/MethodVisitor.java b/src/main/java/gr/gousiosg/callgraph/MethodVisitor.java
index 1d55bf2..a504e1d 100644
--- a/src/main/java/gr/gousiosg/callgraph/MethodVisitor.java
+++ b/src/main/java/gr/gousiosg/callgraph/MethodVisitor.java
@@ -1,48 +1,47 @@
 package gr.gousiosg.callgraph;
 
 import org.apache.bcel.classfile.JavaClass;
-import org.apache.bcel.generic.ArrayInstruction;
-import org.apache.bcel.generic.CHECKCAST;
-import org.apache.bcel.generic.CodeExceptionGen;
 import org.apache.bcel.generic.ConstantPoolGen;
 import org.apache.bcel.generic.ConstantPushInstruction;
 import org.apache.bcel.generic.EmptyVisitor;
-import org.apache.bcel.generic.FieldInstruction;
-import org.apache.bcel.generic.INSTANCEOF;
 import org.apache.bcel.generic.Instruction;
 import org.apache.bcel.generic.InstructionConstants;
 import org.apache.bcel.generic.InstructionHandle;
 import org.apache.bcel.generic.InvokeInstruction;
-import org.apache.bcel.generic.LocalVariableInstruction;
 import org.apache.bcel.generic.MethodGen;
 import org.apache.bcel.generic.ReturnInstruction;
-import org.apache.bcel.generic.Type;
 
+/**
+ * The simplest of method visitors, prints any invoked method
+ * signature for all method invocations.
+ * 
+ * Class copied with modifications from CJKM: http://www.spinellis.gr/sw/ckjm/
+ */
 public class MethodVisitor extends EmptyVisitor {
 
     JavaClass visitedClass;
     private MethodGen mg;
     private ConstantPoolGen cp;
+    private String format;
 
     public MethodVisitor(MethodGen m, JavaClass jc) {
         visitedClass = jc;
         mg = m;
         cp = mg.getConstantPool();
+        format = visitedClass.getClassName() + ":" + mg.getName() + "->" + "%s:%s";
     }
 
     public void start() {
-        if (!mg.isAbstract() && !mg.isNative()) {
-            for (InstructionHandle ih = mg.getInstructionList().getStart(); ih != null; ih = ih.getNext()) {
-                Instruction i = ih.getInstruction();
-
-                if (!visitInstruction(i))
-                    i.accept(this);
-            }
-            updateExceptionHandlers();
+        if (mg.isAbstract() || mg.isNative())
+            return;
+        for (InstructionHandle ih = mg.getInstructionList().getStart(); ih != null; ih = ih.getNext()) {
+            Instruction i = ih.getInstruction();
+            
+            if (!visitInstruction(i))
+                i.accept(this);
         }
     }
 
-    /** Visit a single instruction. */
     private boolean visitInstruction(Instruction i) {
         short opcode = i.getOpcode();
 
@@ -50,58 +49,8 @@ public class MethodVisitor extends EmptyVisitor {
                 && !(i instanceof ConstantPushInstruction) && !(i instanceof ReturnInstruction));
     }
 
-    /** Local variable use. */
-    public void visitLocalVariableInstruction(LocalVariableInstruction i) {
-        //if (i.getOpcode() != Constants.IINC)
-            //cv.registerCoupling(i.getType(cp));
-    }
-
-    /** Array use. */
-    public void visitArrayInstruction(ArrayInstruction i) {
-        //cv.registerCoupling(i.getType(cp));
-    }
-
-    /** Field access. */
-    public void visitFieldInstruction(FieldInstruction i) {
-        //cv.registerFieldAccess(i.getClassName(cp), i.getFieldName(cp));
-        //cv.registerCoupling(i.getFieldType(cp));
-    }
-
     /** Method invocation. */
     public void visitInvokeInstruction(InvokeInstruction i) {
-        System.out.println(visitedClass.getClassName() + ":" + mg.getName() + "->" +  i.getReferenceType(cp) + ":"+ i.getMethodName(cp));
-        //Type[] argTypes = i.getArgumentTypes(cp);
-        //for (int j = 0; j < argTypes.length; j++)
-        //    cv.registerCoupling(argTypes[j]);
-        //cv.registerCoupling(i.getReturnType(cp));
-        /* Measuring decision: measure overloaded methods separately */
-        //cv.registerMethodInvocation(i.getClassName(cp), i.getMethodName(cp),
-        //        argTypes);
-    }
-
-    /** Visit an instanceof instruction. */
-    public void visitINSTANCEOF(INSTANCEOF i) {
-        
-    }
-
-    /** Visit checklast instruction. */
-    public void visitCHECKCAST(CHECKCAST i) {
-       
-    }
-
-    /** Visit return instruction. */
-    public void visitReturnInstruction(ReturnInstruction i) {
-    }
-
-    /** Visit the method's exception handlers. */
-    private void updateExceptionHandlers() {
-        CodeExceptionGen[] handlers = mg.getExceptionHandlers();
-
-        /* Measuring decision: couple exceptions */
-        for (int i = 0; i < handlers.length; i++) {
-            Type t = handlers[i].getCatchType();
-            //if (t != null)
-                //cv.registerCoupling(t);
-        }
+        System.out.println(String.format(format,i.getReferenceType(cp),i.getMethodName(cp))); 
     }
 }