org.objectweb.fractal.julia.asm
Class AbstractClassGenerator

java.lang.Object
  extended by org.objectweb.fractal.julia.asm.AbstractClassGenerator
All Implemented Interfaces:
Opcodes, ClassGenerator
Direct Known Subclasses:
AttributeControllerClassGenerator, InterceptorClassGenerator, InterfaceClassGenerator

public abstract class AbstractClassGenerator
extends Object
implements ClassGenerator, Opcodes

An abstract class generator based on the org.objectweb.asm package. This class generator takes as parameter the name of a super class and a list of interface names, and generates a sub class of the given super class that implements all the given interfaces.

Note: since this class stores information about the class that is currently being generated in its fields, it should not be used by multiple threads simultaneously.


Field Summary
 ClassLoader classLoader
          The class loader to be used to load auxiliary classes, if needed.
 ClassWriter cw
          The class that is being generated.
 List interfaces
          The interfaces to be implemented by the class that is being generated.
 Loader loader
          The loader to be used to load or generate auxiliary classes, if needed.
 String name
          The internal name of the class that is being generated.
 String parameters
          The parameters used to generate the class that is being generated.
 String superClass
          The internal name of the super class of the class that is being generated.
 
Fields inherited from interface org.objectweb.asm.Opcodes
AALOAD, AASTORE, ACC_ABSTRACT, ACC_ANNOTATION, ACC_BRIDGE, ACC_DEPRECATED, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ACONST_NULL, ALOAD, ANEWARRAY, ARETURN, ARRAYLENGTH, ASTORE, ATHROW, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DMUL, DNEG, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FMUL, FNEG, FREM, FRETURN, FSTORE, FSUB, GETFIELD, GETSTATIC, GOTO, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IFEQ, IFGE, IFGT, IFLE, IFLT, IFNE, IFNONNULL, IFNULL, IINC, ILOAD, IMUL, INEG, INSTANCEOF, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISUB, IUSHR, IXOR, JSR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDIV, LLOAD, LMUL, LNEG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, TABLESWITCH, V1_1, V1_2, V1_3, V1_4, V1_5
 
Constructor Summary
AbstractClassGenerator()
           
 
Method Summary
protected  boolean computeMaxs()
          Returns true if maxStack and maxLocals must be automatically computed.
 byte[] generateClass(String name, Tree args, Loader loader, ClassLoader classLoader)
          Generates a sub class of the given class that implements the given interfaces.
protected  void generateConstructor()
          Generates the constructor of the class to be generated.
protected  void generateDefaultMethods()
          Generates the default methods of the class to be generated.
protected  void generateHeader()
          Generates the header of the class to be generated.
protected  void generateInterfaceMethods()
          Generates the methods of the interfaces to be implemented by the class to be generated.
protected abstract  void generateMethod(Method m)
          Generates a method of an interface to be implemented by the class to be generated.
protected  List getImplementedInterfaces()
          Returns all the interfaces implemented by the class to be generated.
static int getOpcodeOffset(Class type)
          Returns the offset which must be added to some opcodes to get an opcode of the given type.
static int getSize(Class type)
          Returns the size of the given type.
protected  String getSource()
          Returns the source of the class to be generated.
protected  void parseArgs(Tree args)
          Initializes this class generator with the given arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cw

public ClassWriter cw
The class that is being generated.


name

public String name
The internal name of the class that is being generated.


superClass

public String superClass
The internal name of the super class of the class that is being generated.


interfaces

public List interfaces
The interfaces to be implemented by the class that is being generated. More precisely, this list of String is the list of the internal names of these interfaces.


parameters

public String parameters
The parameters used to generate the class that is being generated.


loader

public Loader loader
The loader to be used to load or generate auxiliary classes, if needed.


classLoader

public ClassLoader classLoader
The class loader to be used to load auxiliary classes, if needed.

Constructor Detail

AbstractClassGenerator

public AbstractClassGenerator()
Method Detail

generateClass

public byte[] generateClass(String name,
                            Tree args,
                            Loader loader,
                            ClassLoader classLoader)
                     throws ClassGenerationException
Generates a sub class of the given class that implements the given interfaces. This method initializes the fields of this class, and then calls parseArgs, generateHeader, generateConstructor, generateDefaultMethods and generateInterfaceMethods, in this order, in order to generate the specified class.

Specified by:
generateClass in interface ClassGenerator
Parameters:
name - the name of the class that must be generated.
args - the parameters that described how the class must be generated.
loader - the loader to be used to load auxiliary classes.
classLoader - the class loader to be used to load auxiliary classes.
Returns:
the generated class, as a byte array.
Throws:
ClassGenerationException - if the class cannot be generated.

parseArgs

protected void parseArgs(Tree args)
Initializes this class generator with the given arguments. The default implementation of this method requires arguments of the form "(objectDescriptor superClassName (itfName1 ... itfNameN) ...)", where superClassName is the name of the super class of the class to be generated, and itfName1 ... itfNameN the names of the interfaces it must implement.

Parameters:
args - the descriptor of the class to be generated.

computeMaxs

protected boolean computeMaxs()
Returns true if maxStack and maxLocals must be automatically computed. This method returns false by default.

Returns:
true if the maximum stack size and the maximum number of local variables of the methods generated by this class generator must be automatically computed (see ClassWriter).

generateHeader

protected void generateHeader()
                       throws ClassGenerationException
Generates the header of the class to be generated.

Throws:
ClassGenerationException - if a problem occurs.

getSource

protected String getSource()
Returns the source of the class to be generated. The default implementation of this method returns "GENERATED".

Returns:
the source of the class to be generated.

getImplementedInterfaces

protected List getImplementedInterfaces()
                                 throws ClassGenerationException
Returns all the interfaces implemented by the class to be generated. This list contains the interfaces interfaces, and some default interfaces such as Generated. This method is used by generateHeader.

Returns:
the names of all the interfaces implemented by the generated class.
Throws:
ClassGenerationException - if a problem occurs.

generateConstructor

protected void generateConstructor()
                            throws ClassGenerationException
Generates the constructor of the class to be generated. The default implementation of this method generates an empty public constructor without arguments. The super class should also have such a constructor.

Throws:
ClassGenerationException - if a problem occurs.

generateDefaultMethods

protected void generateDefaultMethods()
                               throws ClassGenerationException
Generates the default methods of the class to be generated. The default implementation of this method generates the getFcGeneratorParameters method.

Throws:
ClassGenerationException - if a problem occurs.

generateInterfaceMethods

protected void generateInterfaceMethods()
                                 throws ClassGenerationException
Generates the methods of the interfaces to be implemented by the class to be generated. This method calls generateMethod for each method of each interface to be implemented (generateMethod is called only once per method, even for those that are specified in several interfaces).

Throws:
ClassGenerationException - if a problem occurs.

generateMethod

protected abstract void generateMethod(Method m)
                                throws ClassGenerationException
Generates a method of an interface to be implemented by the class to be generated.

Parameters:
m - the method to be generated.
Throws:
ClassGenerationException - if a problem occurs.

getOpcodeOffset

public static int getOpcodeOffset(Class type)
Returns the offset which must be added to some opcodes to get an opcode of the given type. More precisely, returns the offset which must be added to an opc_iXXXX opcode to get the opc_YXXXX opcode corresponding to the given type. For example, if the given type is double the result is 3, which means that opc_dload, opc_dstore, opc_dreturn... opcodes are equal to opc_iload+3, opc_istore+3, opc_ireturn+3...

Parameters:
type - a Java class representing a Java type (primitive or not).
Returns:
the opcode offset of the corresponding to the given type.

getSize

public static int getSize(Class type)
Returns the size of the given type. This size is 2 for the double and long types, and 1 for the other types.

Parameters:
type - a Java class representing a Java type (primitive or not).
Returns:
the size of the given type.