javassist.bytecode
Class ClassFileWriter

java.lang.Object
  extended by javassist.bytecode.ClassFileWriter

public class ClassFileWriter
extends java.lang.Object

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
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(java.io.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
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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 Detail

getConstPool

public ClassFileWriter.ConstPoolWriter getConstPool()
Returns a constant pool.


getFieldWriter

public ClassFileWriter.FieldWriter getFieldWriter()
Returns a filed writer.


getMethodWriter

public ClassFileWriter.MethodWriter 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 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.
See Also:
AccessFlag

end

public void end(java.io.DataOutputStream out,
                int accessFlags,
                int thisClass,
                int superClass,
                int[] interfaces,
                ClassFileWriter.AttributeWriter aw)
         throws java.io.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 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.
Throws:
java.io.IOException
See Also:
AccessFlag


Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999-2010 Shigeru Chiba. All Rights Reserved.