Skip to content

Instantly share code, notes, and snippets.

@dehowell
Created March 29, 2017 23:15
Show Gist options
  • Save dehowell/72c1a410b4c4870ee36441336b34588e to your computer and use it in GitHub Desktop.
Save dehowell/72c1a410b4c4870ee36441336b34588e to your computer and use it in GitHub Desktop.
Visit Classes Used
import org.apache.bcel.Repository;
import org.apache.bcel.classfile.Constant;
import org.apache.bcel.classfile.ConstantClass;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.classfile.JavaClass;
/**
* Prototype code to walk the graph of referenced classes, for use hunting down dead code.
*/
public class Analyzer {
public static void main(String[] args) throws Exception {
JavaClass clazz = Repository.lookupClass("Foo");
Analyzer analyzer = new Analyzer();
analyzer.traverse(clazz);
}
public void traverse(JavaClass node) throws Exception {
ConstantPool pool = node.getConstantPool();
for (Constant constant : pool.getConstantPool()) {
if (constant instanceof ConstantClass) {
String className = pool.constantToString(constant);
if (className != node.getClassName() && !className.startsWith("java")) {
System.out.println(className);
traverse(Repository.lookupClass(className));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment