org.objectweb.easybeans.enhancer
Class CommonClassGenerator

java.lang.Object
  extended by org.objectweb.easybeans.enhancer.CommonClassGenerator
All Implemented Interfaces:
org.objectweb.asm.Opcodes
Direct Known Subclasses:
EasyBeansInvocationContextGenerator, InterceptorManagerGenerator

public abstract class CommonClassGenerator
extends java.lang.Object
implements org.objectweb.asm.Opcodes

Class with useful routines for writing a class.

Author:
Florent Benoit

Field Summary
static java.lang.String ARRAY_OBJECTS
          Define an array of objects.
private  org.objectweb.asm.ClassWriter cw
          The ClassWriter to which this adapter delegates calls.
static java.lang.String EASYBEANS_FACTORY
          Factory class (used to make the bean factory available).
private  org.objectweb.asm.FieldVisitor fv
          Field visitor.
static int GENERATED_CLASS_VERSION
          Version used for generated class.
static java.lang.String JAVA_LANG_EXCEPTION
          Defines java.lang.Exception class.
static java.lang.String JAVA_LANG_OBJECT
          Defines java.lang.Object class.
static java.lang.String JAVA_LANG_REFLECT_METHOD
          Define java.lang.reflect.Method.
static java.lang.String VOID_METHOD_JAVA_LANG_OBJECT
          Defines a void method with JAVA_LANG_OBJECT as parameter.
 
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, V1_6
 
Constructor Summary
CommonClassGenerator(org.objectweb.asm.ClassWriter cw)
          Creates a default class with useful routines for writing a class.
 
Method Summary
protected  void addAttribute(int access, java.lang.String name, java.lang.String desc)
          Adds an attribute in the current classwriter.
protected  void addAttribute(int access, java.lang.String name, java.lang.String desc, java.lang.Object value)
          Adds an attribute in the current classwriter.
static void addFieldGettersSetters(org.objectweb.asm.ClassVisitor cv, java.lang.String beanClassName, java.lang.String fieldName, java.lang.Class clazz)
          Adds a field with its getters/setters.
static void addFieldGettersSetters(org.objectweb.asm.ClassVisitor cv, java.lang.String beanClassName, java.lang.String fieldName, java.lang.String className)
          Adds a field with its getters/setters.
static void addNullGetter(org.objectweb.asm.ClassVisitor cv, java.lang.String getterName, java.lang.Class clazz)
          Adds a getter for a given fieldName.
static void addReturnType(org.objectweb.asm.Type type, org.objectweb.asm.MethodVisitor mv)
          Adds a return entry depending of the type value.
static java.lang.String encodeArrayClassDesc(java.lang.String className)
          Encodes an internal classname into an array descriptor.
static java.lang.String encodeClassDesc(java.lang.String className)
          Encodes an internal classname into a descriptor.
 org.objectweb.asm.ClassWriter getCW()
           
static int putFieldLoadOpCode(int sortCode)
          Sends the OpCode used in an constructor to set the field.
static void returnsObject(org.objectweb.asm.Type returnType, org.objectweb.asm.MethodVisitor mv)
          Returns the object by casting it or return null if method is of void type.
static void transformObjectIntoPrimitive(org.objectweb.asm.Type type, org.objectweb.asm.MethodVisitor mv)
          If a type is one of the object type with something which could be linked to a primitive type, cast object into primitive.
static void transformPrimitiveIntoObject(org.objectweb.asm.Type type, org.objectweb.asm.MethodVisitor mv)
          If a type is one of the primitive object : boolean, int, long, etc, adds an instruction to transform type into an object.
static void visitClassType(org.objectweb.asm.Type type, org.objectweb.asm.MethodVisitor mv)
          Allow to access to a class.
ie, for int.class it will access to Integer.TYPE.
For objects, it only calls the Object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ARRAY_OBJECTS

public static final java.lang.String ARRAY_OBJECTS
Define an array of objects.

See Also:
Constant Field Values

JAVA_LANG_OBJECT

public static final java.lang.String JAVA_LANG_OBJECT
Defines java.lang.Object class.

See Also:
Constant Field Values

VOID_METHOD_JAVA_LANG_OBJECT

public static final java.lang.String VOID_METHOD_JAVA_LANG_OBJECT
Defines a void method with JAVA_LANG_OBJECT as parameter.

See Also:
Constant Field Values

JAVA_LANG_EXCEPTION

public static final java.lang.String JAVA_LANG_EXCEPTION
Defines java.lang.Exception class.

See Also:
Constant Field Values

JAVA_LANG_REFLECT_METHOD

public static final java.lang.String JAVA_LANG_REFLECT_METHOD
Define java.lang.reflect.Method.

See Also:
Constant Field Values

EASYBEANS_FACTORY

public static final java.lang.String EASYBEANS_FACTORY
Factory class (used to make the bean factory available).


GENERATED_CLASS_VERSION

public static final int GENERATED_CLASS_VERSION
Version used for generated class.

See Also:
Constant Field Values

cw

private org.objectweb.asm.ClassWriter cw
The ClassWriter to which this adapter delegates calls.


fv

private org.objectweb.asm.FieldVisitor fv
Field visitor.

Constructor Detail

CommonClassGenerator

public CommonClassGenerator(org.objectweb.asm.ClassWriter cw)
Creates a default class with useful routines for writing a class.

Parameters:
cw - the class writer which will generate the class
Method Detail

addAttribute

protected void addAttribute(int access,
                            java.lang.String name,
                            java.lang.String desc)
Adds an attribute in the current classwriter.

Parameters:
access - the field's access flags (see Opcodes). This parameter also indicates if the field is synthetic and/or deprecated.
name - the field's name.
desc - the field's descriptor (see Type).

addAttribute

protected void addAttribute(int access,
                            java.lang.String name,
                            java.lang.String desc,
                            java.lang.Object value)
Adds an attribute in the current classwriter.

Parameters:
access - the field's access flags (see Opcodes). This parameter also indicates if the field is synthetic and/or deprecated.
name - the field's name.
desc - the field's descriptor (see Type).
value - the field's initial value. This parameter, which may be null if the field does not have an initial value, must be an Integer, a Float, a Long, a Double or a String (for int, float, long or String fields respectively). This parameter is only used for static fields. Its value is ignored for non static fields, which must be initialized through bytecode instructions in constructors or methods.

encodeClassDesc

public static java.lang.String encodeClassDesc(java.lang.String className)
Encodes an internal classname into a descriptor.

Parameters:
className - internal class name
Returns:
desc for the given className

encodeArrayClassDesc

public static java.lang.String encodeArrayClassDesc(java.lang.String className)
Encodes an internal classname into an array descriptor.

Parameters:
className - internal class name
Returns:
desc for the given className (array mode)

getCW

public org.objectweb.asm.ClassWriter getCW()
Returns:
the class writer used by this generator

putFieldLoadOpCode

public static int putFieldLoadOpCode(int sortCode)
Sends the OpCode used in an constructor to set the field.

Parameters:
sortCode - type of attribute to set
Returns:
op code

transformPrimitiveIntoObject

public static void transformPrimitiveIntoObject(org.objectweb.asm.Type type,
                                                org.objectweb.asm.MethodVisitor mv)
If a type is one of the primitive object : boolean, int, long, etc, adds an instruction to transform type into an object. ie : from int to Integer by calling Integer.valueOf(i);

Parameters:
type - the object that need to be processed
mv - the method visitor on which there is a need to adds an instruction

transformObjectIntoPrimitive

public static void transformObjectIntoPrimitive(org.objectweb.asm.Type type,
                                                org.objectweb.asm.MethodVisitor mv)
If a type is one of the object type with something which could be linked to a primitive type, cast object into primitive. ie : from Integer to int by calling ((Integer) object).intValue();

Parameters:
type - the object that need to be processed
mv - the method visitor on which there is a need to adds an instruction

addReturnType

public static void addReturnType(org.objectweb.asm.Type type,
                                 org.objectweb.asm.MethodVisitor mv)
Adds a return entry depending of the type value. ie, for int : mv.visitInsn(IRETURN); for void : mv.visitInsn(RETURN);

Parameters:
type - the object that need to be processed
mv - the method visitor on which there is a need to adds an instruction

visitClassType

public static void visitClassType(org.objectweb.asm.Type type,
                                  org.objectweb.asm.MethodVisitor mv)
Allow to access to a class.
ie, for int.class it will access to Integer.TYPE.
For objects, it only calls the Object.

Parameters:
type - the type of the object from which we want the class
mv - object on which we add the instruction

returnsObject

public static void returnsObject(org.objectweb.asm.Type returnType,
                                 org.objectweb.asm.MethodVisitor mv)
Returns the object by casting it or return null if method is of void type.

Parameters:
returnType - the kind of return for this method.
mv - object on which we add the instruction.

addFieldGettersSetters

public static void addFieldGettersSetters(org.objectweb.asm.ClassVisitor cv,
                                          java.lang.String beanClassName,
                                          java.lang.String fieldName,
                                          java.lang.Class clazz)
Adds a field with its getters/setters.

Parameters:
cv - the classvisitor to add field/methods
beanClassName - the name of the bean's class
fieldName - the name of the attribute
clazz - the class of the attribute.

addFieldGettersSetters

public static void addFieldGettersSetters(org.objectweb.asm.ClassVisitor cv,
                                          java.lang.String beanClassName,
                                          java.lang.String fieldName,
                                          java.lang.String className)
Adds a field with its getters/setters.

Parameters:
cv - the classvisitor to add field/methods
beanClassName - the name of the bean's class
fieldName - the name of the attribute
className - the className of the attribute.

addNullGetter

public static void addNullGetter(org.objectweb.asm.ClassVisitor cv,
                                 java.lang.String getterName,
                                 java.lang.Class clazz)
Adds a getter for a given fieldName. If the fieldName is null, it's return null.

Parameters:
cv - the classvisitor to add the method
getterName - the name of the method
clazz - the class of the returned object.