Skip to content

Commit f3ad01c

Browse files
committed
Remove asm, import bcel, call graph produced from static referenced
class
1 parent fa79212 commit f3ad01c

5 files changed

Lines changed: 153 additions & 56 deletions

File tree

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
<scope>compile</scope>
2424
</dependency>
2525
<dependency>
26-
<groupId>asm</groupId>
27-
<artifactId>asm-all</artifactId>
28-
<version>3.3</version>
26+
<groupId>org.apache.bcel</groupId>
27+
<artifactId>bcel</artifactId>
28+
<version>5.2</version>
2929
<type>jar</type>
3030
<scope>compile</scope>
3131
</dependency>

src/main/java/gr/gousiosg/callgraph/ClassPrinter.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package gr.gousiosg.callgraph;
2+
3+
import org.apache.bcel.classfile.EmptyVisitor;
4+
import org.apache.bcel.classfile.JavaClass;
5+
import org.apache.bcel.classfile.Method;
6+
import org.apache.bcel.generic.ConstantPoolGen;
7+
import org.apache.bcel.generic.MethodGen;
8+
9+
public class ClassVisitor extends EmptyVisitor {
10+
11+
JavaClass visitedClass;
12+
private ConstantPoolGen cp;
13+
14+
public ClassVisitor(JavaClass jc) {
15+
visitedClass = jc;
16+
cp = new ConstantPoolGen(visitedClass.getConstantPool());
17+
}
18+
19+
public void visitJavaClass(JavaClass jc) {
20+
Method[] methods = jc.getMethods();
21+
for (int i = 0; i < methods.length; i++)
22+
methods[i].accept(this);
23+
}
24+
25+
public void visitMethod(Method method) {
26+
MethodGen mg = new MethodGen(method, visitedClass.getClassName(), cp);
27+
MethodVisitor visitor = new MethodVisitor(mg, visitedClass);
28+
visitor.start();
29+
}
30+
31+
public void start() {
32+
visitJavaClass(visitedClass);
33+
}
34+
}

src/main/java/gr/gousiosg/callgraph/JCallGraph.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import java.io.IOException;
55

6-
import org.objectweb.asm.ClassReader;
6+
import org.apache.bcel.classfile.ClassParser;
7+
78

89
/**
910
* Constructs a callgraph out of a JAR archive. Can combine multiple archives
@@ -15,12 +16,14 @@
1516
public class JCallGraph {
1617

1718
public static void main(String[] args) {
19+
ClassParser cp;
1820
try {
19-
ClassPrinter cp = new ClassPrinter();
20-
ClassReader cr = new ClassReader("java.lang.Thread");
21-
cr.accept(cp, 0);
21+
cp = new ClassParser("/Volumes/Files/Developer/java-callgraph/target/classes/gr/gousiosg/callgraph/ClassVisitor.class");
22+
ClassVisitor visitor = new ClassVisitor(cp.parse());
23+
visitor.start();
2224
} catch (IOException e) {
23-
25+
// TODO Auto-generated catch block
26+
e.printStackTrace();
2427
}
25-
}
28+
}
2629
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)