public class ClassFileWriter extends Object
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; }
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
ClassFileWriter.AttributeWriter
This writes attributes.
|
static class |
ClassFileWriter.ConstPoolWriter
Constant Pool.
|
static class |
ClassFileWriter.FieldWriter
Field.
|
static class |
ClassFileWriter.MethodWriter
Method.
|
| Constructor and Description |
|---|
ClassFileWriter(int major,
int minor)
Constructs a class file writer.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
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).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.AccessFlagpublic 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.IOExceptionAccessFlagCopyright © 2016 Shigeru Chiba, www.javassist.org. All Rights Reserved.