Package javassist.bytecode
Class ClassFileWriter
java.lang.Object
javassist.bytecode.ClassFileWriter
A quick class-file writer. This is useful when a generated
class file is simple and the code generation should be fast.
Example:
ClassFileWriter cfw = new ClassFileWriter(ClassFile.JAVA_4, 0); ConstPoolWriter cpw = cfw.getConstPool(); FieldWriter fw = cfw.getFieldWriter(); fw.add(AccessFlag.PUBLIC, "value", "I", null); fw.add(AccessFlag.PUBLIC, "value2", "J", null); int thisClass = cpw.addClassInfo("sample/Test"); int superClass = cpw.addClassInfo("java/lang/Object"); MethodWriter mw = cfw.getMethodWriter(); mw.begin(AccessFlag.PUBLIC, MethodInfo.nameInit, "()V", null, null); mw.add(Opcode.ALOAD_0); mw.add(Opcode.INVOKESPECIAL); int signature = cpw.addNameAndTypeInfo(MethodInfo.nameInit, "()V"); mw.add16(cpw.addMethodrefInfo(superClass, signature)); mw.add(Opcode.RETURN); mw.codeEnd(1, 1); mw.end(null, null); mw.begin(AccessFlag.PUBLIC, "one", "()I", null, null); mw.add(Opcode.ICONST_1); mw.add(Opcode.IRETURN); mw.codeEnd(1, 1); mw.end(null, null); byte[] classfile = cfw.end(AccessFlag.PUBLIC, thisClass, superClass, null, null);
The code above generates the following class:
package sample; public class Test { public int value; public long value2; public Test() { super(); } public one() { return 1; } }
- Since:
- 3.13
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
This writes attributes.static final class
Constant Pool.static final class
Field.static final class
Method. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]
end
(int accessFlags, int thisClass, int superClass, int[] interfaces, ClassFileWriter.AttributeWriter aw) Ends writing and returns the contents of the class file.void
end
(DataOutputStream out, int accessFlags, int thisClass, int superClass, int[] interfaces, ClassFileWriter.AttributeWriter aw) Ends writing and writes the contents of the class file into the given output stream.Returns a constant pool.Returns a filed writer.Returns a method writer.
-
Constructor Details
-
ClassFileWriter
public ClassFileWriter(int major, int minor) Constructs a class file writer.- Parameters:
major
- the major version (ClassFile.JAVA_4
,ClassFile.JAVA_5
, ...).minor
- the minor version (0 for JDK 1.3 and later).
-
-
Method Details
-
getConstPool
Returns a constant pool. -
getFieldWriter
Returns a filed writer. -
getMethodWriter
Returns a method writer. -
end
public byte[] end(int accessFlags, int thisClass, int superClass, int[] interfaces, ClassFileWriter.AttributeWriter aw) Ends writing and returns the contents of the class file.- Parameters:
accessFlags
- access flags.thisClass
- this class. an index indicating itsCONSTANT_Class_info
.superClass
- super class. an index indicating itsCONSTANT_Class_info
.interfaces
- implemented interfaces. index numbers indicating theirClassInfo
. It may be null.aw
- attributes of the class file. May be null.- See Also:
-
end
public void end(DataOutputStream out, int accessFlags, int thisClass, int superClass, int[] interfaces, ClassFileWriter.AttributeWriter aw) throws IOException Ends writing and writes the contents of the class file into the given output stream.- Parameters:
accessFlags
- access flags.thisClass
- this class. an index indicating itsCONSTANT_Class_info
.superClass
- super class. an index indicating itsCONSTANT_Class_info
.interfaces
- implemented interfaces. index numbers indicating theirCONSTATNT_Class_info
. It may be null.aw
- attributes of the class file. May be null.- Throws:
IOException
- See Also:
-