How to detect whether the class from the jar file is extending other class or if there are method calls to other class objects or other class objects are created ? and then system out which class extend which class and which class called methods from which class .
Im using Classparser to parser the jar . here is part of my code :
String jarfile = "C:\\Users\\OOOO\\Desktop\\Sample.Jar";
jar = new JarFile(jarfile);
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (!entry.getName().endsWith(".class")) {
continue;
}
ClassParser parser = new ClassParser(jarfile, entry.getName());
JavaClass javaClass = parser.parse();
Someone voted to close this question as "too broad". I'm not sure whether this is the appropriate close reason here, but it might be, because one could consider this question (which is a follow up to your previous question) as just asking others to do some work for you.
However, to answer the basic question of how to detect references between classes in a single JAR file with BCEL:
You can obtain the list of
JavaClass
objects from theJarFile
. For each of theseJavaClass
objects, you can inspect theMethod
objects and theirInstructionList
. Out of these instructions, you can select theInvokeInstruction
objects and examine them further to find out which method on which class is actually invoked there.The following program opens a JAR file (for obvious reasons, it's the
bcel-5.2.jar
- you'll need it anyhow...) and processes it in the way described above. For eachJavaClass
of the JAR file, it creates a map from all referencedJavaClass
objects to the list of theMethod
s that are invoked on these classes, and prints the information accordingly:Note, however, that this information might not be complete in every sense. For example, due to polymorphism, you might not always detect that a method is called on an object of a certain class, because it is "hidden" behind the polymorphic abstraction. For example, in a code snippet like
the call to
toString
actually happens on an instance ofMyClass
. But due to polymorphism, it can only be recognized as a call to this method on "someObject
".Disclaimer: This is, strictly speaking, not an answer to your question because it uses not BCEL but Javassist. Nevertheless you may find my experiences and code useful.
Few years ago I've written e Maven plugin (I called it Storyteller Maven Plugin) for this very purpose - to analyse JARs files for dependencies which are unnecessary or nor required.
Please see this question:
And my answer to it.
Although the plugin worked I have never released it back then. Now I've moved it to GitHub just to make it accessible for others.
You ask about parsing a JAR to analyze the code in
.class
files. Below are a couple of Javassist code snippets.Search a JAR file for classes and create a CtClass per entry:
Finding out referenced classes is then just:
Overall my experience with Javassist for the very similar task was very positive. I hope this helps.