I have a .class file which can be class loaded to class object (`java lang`). I need to convert the class object to a BCEL intermediate Java class (org.apache.bcel.classfile.JavaClass
) object. How do I do this?

- 6,840
- 14
- 73
- 130

- 1,134
- 4
- 14
- 29
-
Welcome to Stack Overflow! I edited to improve your post's formatting. However, there are some additional problems. We need to see your own attempts at solving this first. We will only point you in the right direction if you've started coding, but we rarely will give you code outright. Post your relevant code, and any error messages you're getting. Check the Java documentation and a search engine. If someone else finds the answer in those resources, you'll get an "RTD" or "LMGTFY" reply and a LOT of downvotes. – CodeMouse92 Oct 06 '15 at 14:11
2 Answers
The following code helps:-
public void get(){
Class<?> javaClass1 = null;
javaClass1 = ucl.loadClass("com.sample.Customer");
org.apache.bcel.classfile.JavaClass javaClazz1=org.apache.bcel.Repository.lookupClass(javaClass1);
}

- 1,134
- 4
- 14
- 29
The short answer is that you can't but there may be another way to solve your problem.
BCEL needs to read the class in from a class file and cannot generate a JavaClass from a Class instance. This is because BCEL needs the bytecode and not just the class metadata (which is what is in a Class instance). There is no way using standard JVM instructions to get bytecode from a Class instance. Instead, BCEL parses it from class files.
That being said, if your Class represents a class file on the disk somewhere (in a jar or just a .class file lying around) you can often get access to the class file from the Class instance, although somewhat indirectly. This question can get you started (Find where java class is loaded from)