I studied the .class file internal structure to obtain the class name including packages from the content of the .class file. The structure is very well documented is the specification here: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html , but it shows little bit troublesome to parse the file in pure Java.
Finally I found the Apache Common BCEL (http://commons.apache.org/proper/commons-bcel/manual.html) project. It is much wider that I need and contains also ability to obtain the full class name from class using 3 lines of code:
FileInputStream input = new FileInputStream(new File("MyCustom.class"));
ClassParser parser = new ClassParser(input,classname);
JavaClass javaClass = parser.parse();
String fullClassname = javaClass.getClassName();
JavaClass javaClass = parser.parse();
String fullClassname = javaClass.getClassName();
Really easy isn't it?
Žádné komentáře:
Okomentovat