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 sy
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:
How to find unneccesary dependencies in a maven multi-project?
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:
final JarFile artifactJarFile = new JarFile(artifactFile);
final Enumeration jarEntries = artifactJarFile
.entries();
while (jarEntries.hasMoreElements()) {
final JarEntry jarEntry = jarEntries.nextElement();
if (jarEntry.getName().endsWith(".class")) {
InputStream is = null;
CtClass ctClass = null;
try {
is = artifactJarFile.getInputStream(jarEntry);
ctClass = classPool.makeClass(is);
} catch (IOException ioex1) {
throw new MojoExecutionException(
"Could not load class from JAR entry ["
+ artifactFile.getAbsolutePath()
+ "/" + jarEntry.getName() + "].");
} finally {
try {
if (is != null)
is.close();
} catch (IOException ignored) {
// Ignore
}
}
// ...
}
}
Finding out referenced classes is then just:
final Collection referencedClassNames = ctClass.getRefClasses();
Overall my experience with Javassist for the very similar task was very positive. I hope this helps.