I have a .class file that is being generated using ASM core API in the following way:
public void createEmptyClassWithinPackage(String packageName, String className){
/* Creating ClassWriter object that creates class in bytecode representation
* Flag 0 - no need in computation of stack size and generation of frames */
ClassWriter classWriter = new ClassWriter(0);
ClassVisitor classVisitor = new ClassVisitor(ASM4, classWriter) { };
// Visiting class declaration: version, access, name, generic, super, interface
classVisitor.visit(V9, ACC_PUBLIC, packageName + className, null,
Type.getInternalName(Object.class), null);
// Indicate that generation of class is done and get byte array
classVisitor.visitEnd();
classWriter.visitEnd();
byte[] bytesOfClassToWrite = classWriter.toByteArray();
checkClassPackage(packageName);
writeClassToPackage(packageName, className, bytesOfClassToWrite);
}
Methods at the end just check the directory and write the .class file into it. Later when I want to add something to the class I am using:
public void writeEmptyConstructor(String packageName, String className){
/* Read generated class form package and generate empty constructor */
byte[] bytesOfClassToRead = readClassFromPackage(packageName, className);
ClassReader classReader = new ClassReader(bytesOfClassToRead);
ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES);
ClassVisitor classVisitor = new ClassVisitor(ASM4, classWriter) { };
classReader.accept(classVisitor, 0);
generateDefaultConstructor(classVisitor);
// Indicate that generation of constructor is done and get byte array
classVisitor.visitEnd();
classWriter.visitEnd();
byte[] bytesOfClassToWrite = classWriter.toByteArray();
writeClassToPackage(packageName, className, bytesOfClassToWrite);
}
The problem appears when I try to read Class using ClassLoader:
File file = new File("result/classes/TestClass.class");
try(FileInputStream fileInputStream = new FileInputStream(file)) {
byte[] classByteArray = new byte[(int) file.length()];
GeneratedClassLoader generatedClassLoader = new GeneratedClassLoader();
Class c = generatedClassLoader.defineClass("result.classes.TestClass", classByteArray);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
The code above gives an error:
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 0 in class file result/classes/TestClass
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:878)
at callers.GeneratedClassLoader.defineClass(GeneratedClassLoader.java:5)
at Main.main(Main.java:28)
But when I try to use ClassLoader without writing byte array on a disc it works perfectly fine:
public void createEmptyClassWithinPackage(String packageName, String className){
/* Creating ClassWriter object that creates class in bytecode representation
* Flag 0 - no need in computation of stack size and generation of frames */
ClassWriter classWriter = new ClassWriter(0);
ClassVisitor classVisitor = new ClassVisitor(ASM4, classWriter) { };
// Visiting class declaration: version, access, name, generic, super, interface
classVisitor.visit(V9, ACC_PUBLIC, packageName + className, null,
Type.getInternalName(Object.class), null);
// Indicate that generation of class is done and get byte array
classVisitor.visitEnd();
classWriter.visitEnd();
byte[] bytesOfClassToWrite = classWriter.toByteArray();
GeneratedClassLoader generatedClassLoader = new GeneratedClassLoader();
Class c = generatedClassLoader.defineClass((packageName + className).replace("/", ".")
, bytesOfClassToWrite);
// checkClassPackage(packageName);
// writeClassToPackage(packageName, className, bytesOfClassToWrite);
}
My question is: What happens to magic value when I read class from a disc? May it be that I should not use ClassLoader and use something else to read .class files as a byte array from the disc?
The solution was simple, I forgot to write:
fileInputStream.read(classByteArray);
I will not delete this post, hope someone will find it useful.