|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjavassist.bytecode.ClassFileWriter
public class 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; }
}
| Nested Class Summary | |
|---|---|
static interface |
ClassFileWriter.AttributeWriter
This writes attributes. |
static class |
ClassFileWriter.ConstPoolWriter
Constant Pool. |
static class |
ClassFileWriter.FieldWriter
Field. |
static class |
ClassFileWriter.MethodWriter
Method. |
| Constructor Summary | |
|---|---|
ClassFileWriter(int major,
int minor)
Constructs a class file writer. |
|
| Method Summary | |
|---|---|
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. |
byte[] |
end(int accessFlags,
int thisClass,
int superClass,
int[] interfaces,
ClassFileWriter.AttributeWriter aw)
Ends writing and returns the contents of the class file. |
ClassFileWriter.ConstPoolWriter |
getConstPool()
Returns a constant pool. |
ClassFileWriter.FieldWriter |
getFieldWriter()
Returns a filed writer. |
ClassFileWriter.MethodWriter |
getMethodWriter()
Returns a method writer. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ClassFileWriter(int major,
int minor)
major - the major version (ClassFile.JAVA_4, ClassFile.JAVA_5, ...).minor - the minor version (0 for JDK 1.3 and later).| Method Detail |
|---|
public ClassFileWriter.ConstPoolWriter getConstPool()
public ClassFileWriter.FieldWriter getFieldWriter()
public ClassFileWriter.MethodWriter getMethodWriter()
public byte[] end(int accessFlags,
int thisClass,
int superClass,
int[] interfaces,
ClassFileWriter.AttributeWriter aw)
accessFlags - access flags.thisClass - this class. an index indicating its CONSTANT_Class_info.superClass - super class. an index indicating its CONSTANT_Class_info.interfaces - implemented interfaces.
index numbers indicating their ClassInfo.
It may be null.aw - attributes of the class file. May be null.AccessFlag
public void end(DataOutputStream out,
int accessFlags,
int thisClass,
int superClass,
int[] interfaces,
ClassFileWriter.AttributeWriter aw)
throws IOException
accessFlags - access flags.thisClass - this class. an index indicating its CONSTANT_Class_info.superClass - super class. an index indicating its CONSTANT_Class_info.interfaces - implemented interfaces.
index numbers indicating their CONSTATNT_Class_info.
It may be null.aw - attributes of the class file. May be null.
IOExceptionAccessFlag
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||