JBoss Community

ClassFile write(java.io.DataOutputStream out) generates empty file!!

created by Carlos Bruguera in Javassist - View the full discussion

Hello all, I'm working in a compiler that is generating bytecode, and I wish to only use those javassist classes that work with low-level bytecode (Bytecode, ClassFile, MethodInfo, FieldInfo, etc.). I'm not interested in high level runtime translation of any java code (so I barely need to use CtClass and similar).

 

The thing is I'm creating a ClassFile object, adding a MethodInfo structure, based on an existent bytecode object (mainCode) which I know is well built. But at the end I'm trying to write the file, and it creates it, but as a 0 byte .class file.

 

Is there anything that I might be missing?.. Here's my piece of code:

 

 

ClassFile initClass = getOrCreateClass("InitClass");
declaring = initClass;
 
MethodInfo mainMethod = new MethodInfo(initClass.getConstPool(), "main", Descriptor.ofMethod(CtClass.voidType, new CtClass[0]));
mainMethod.setCodeAttribute(mainCode.toCodeAttribute());
mainMethod.setAccessFlags(AccessFlag.PUBLIC);
 
try{
    initClass.addMethod(mainMethod);
}catch(DuplicateMemberException e){
    Errors.add("Definicion duplicada para \"Accion Principal\"");
}
 
writeFile(initClass);

 

The method getOrCreateClass is successfully instantiating a new ClassFile object and returning it.

 

...Any ideas?

Reply to this message by going to Community

Start a new discussion in Javassist at Community