Question

I'm playing around with BCEL. I'm not using it to generate bytecode, but instead I'm trying to inspect the structure of existing compiled classes.

I need to be able to point to an arbitrary .class file anywhere on my hard drive and load a JavaClass object based on that. Ideally I'd like to avoid having to add the given class to my classpath.

Was it helpful?

Solution

The straightforward way is to create a ClassParser with the file name and call parse(). Alternatively you can use SyntheticRepository and supply a classpath (that is not your classpath, IYSWIM).

OTHER TIPS

The existing .class can be class loaded to java lang class object. Then it can be converted into BCEL intermediate javaclass structure. The following code may help:-

Class<?> javaClass1 = null;
javaClass1 = ucl.loadClass("com.sample.Customer");
org.apache.bcel.classfile.JavaClass javaClazz1=org.apache.bcel.Repository.lookupClass(javaClass1);
new ClassParser(classfilebytearrayhere).parse()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top