Create a BCEL JavaClass object from an arbitrary .class file

I play with BCEL. I do not use it to generate bytecode, but instead I try to check the structure of existing compiled classes.

I need to specify an arbitrary .class file anywhere on my hard drive and load a JavaClass object based on what. Ideally, I would like not to add this class to my classpath.

+4
source share
3 answers

An easy way is to create a ClassParser with a file name and calling parse (). Alternatively, you can use SyntheticRepository and provide a class path (this is not your class path, IYSWIM).

+10
source
new ClassParser(classfilebytearrayhere).parse() 
+2
source

An existing .class can be loaded by a class into a java lang class object. It can then be converted to an intermediate javaclass BCEL 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); 
+2
source

Source: https://habr.com/ru/post/1277414/


All Articles