|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectbsh.ClassGeneratorUtil
ClassGeneratorUtil utilizes the ASM (www.objectweb.org) bytecode generator by Eric Bruneton in order to generate class "stubs" for BeanShell at runtime.
Stub classes contain all of the fields of a BeanShell scripted class as well as two "callback" references to BeanShell namespaces: one for static methods and one for instance methods. Methods of the class are delegators which invoke corresponding methods on either the static or instance bsh object and then unpack and return the results. The static namespace utilizes a static import to delegate variable access to the class' static fields. The instance namespace utilizes a dynamic import (i.e. mixin) to delegate variable access to the class' instance variables.
Constructors for the class delegate to the static initInstance() method of ClassGeneratorUtil to initialize new instances of the object. initInstance() invokes the instance intializer code (init vars and instance blocks) and then delegates to the corresponding scripted constructor method in the instance namespace. Constructors contain special switch logic which allows the BeanShell to control the calling of alternate constructors (this() or super() references) at runtime.
Specially named superclass delegator methods are also generated in order to allow BeanShell to access overridden methods of the superclass (which reflection does not normally allow).
| Nested Class Summary | |
static class |
ClassGeneratorUtil.ConstructorArgs
A ConstructorArgs object holds evaluated arguments for a constructor call as well as the index of a possible alternate selector to invoke. |
| Field Summary | |
(package private) static java.lang.String |
BSHCONSTRUCTORS
The bsh static namespace variable that holds the constructor methods |
(package private) static java.lang.String |
BSHINIT
The bsh static namespace variable name of the instance initializer |
(package private) static java.lang.String |
BSHSTATIC
The name of the static field holding the reference to the bsh static This (the callback namespace for static methods) |
(package private) static java.lang.String |
BSHSUPER
The prefix for the name of the super delegate methods. |
(package private) static java.lang.String |
BSHTHIS
The name of the instance field holding the reference to the bsh instance This (the callback namespace for instance methods) |
(package private) Modifiers |
classModifiers
|
(package private) java.lang.String |
className
|
(package private) NameSpace |
classStaticNameSpace
|
(package private) DelayedEvalBshMethod[] |
constructors
|
(package private) static int |
DEFAULTCONSTRUCTOR
The switch branch number for the default constructor. |
(package private) java.lang.String |
fqClassName
fully qualified class name (with package) e.g. |
(package private) java.lang.Class[] |
interfaces
|
(package private) boolean |
isInterface
|
(package private) DelayedEvalBshMethod[] |
methods
|
(package private) static java.lang.String |
OBJECT
|
(package private) java.lang.Class |
superClass
|
(package private) java.lang.String |
superClassName
|
(package private) java.lang.reflect.Constructor[] |
superConstructors
|
(package private) Variable[] |
vars
|
| Constructor Summary | |
ClassGeneratorUtil(Modifiers classModifiers,
java.lang.String className,
java.lang.String packageName,
java.lang.Class superClass,
java.lang.Class[] interfaces,
Variable[] vars,
DelayedEvalBshMethod[] bshmethods,
NameSpace classStaticNameSpace,
boolean isInterface)
|
|
| Method Summary | |
(package private) boolean |
classContainsMethod(java.lang.Class clas,
java.lang.String methodName,
java.lang.String[] paramTypes)
|
(package private) static void |
doSwitchBranch(int index,
java.lang.String targetClassName,
java.lang.String[] paramTypes,
Label endLabel,
Label[] labels,
int consArgsVar,
CodeVisitor cv)
|
byte[] |
generateClass()
Generate the class bytecode for this class. |
(package private) void |
generateConstructor(int index,
java.lang.String[] paramTypes,
int modifiers,
ClassWriter cw)
Generate a constructor. |
(package private) void |
generateConstructorSwitch(int consIndex,
int argsVar,
int consArgsVar,
CodeVisitor cv)
Generate a switch with a branch for each possible alternate constructor. |
(package private) static void |
generateField(java.lang.String fieldName,
java.lang.String type,
int modifiers,
ClassWriter cw)
Generate a field - static or instance. |
(package private) static void |
generateMethod(java.lang.String className,
java.lang.String fqClassName,
java.lang.String methodName,
java.lang.String returnType,
java.lang.String[] paramTypes,
int modifiers,
ClassWriter cw)
Generate a delegate method - static or instance. |
static void |
generateParameterReifierCode(java.lang.String[] paramTypes,
boolean isStatic,
CodeVisitor cv)
Generates the code to reify the arguments of the given method. |
(package private) static void |
generatePlainReturnCode(java.lang.String returnType,
CodeVisitor cv)
Generate return code for a normal bytecode |
static void |
generateReturnCode(java.lang.String returnType,
CodeVisitor cv)
Generates the code to unreify the result of the given method. |
(package private) static void |
generateSuperDelegateMethod(java.lang.Class superClass,
java.lang.String superClassName,
java.lang.String methodName,
java.lang.String returnType,
java.lang.String[] paramTypes,
int modifiers,
ClassWriter cw)
Generate a superclass method delegate accessor method. |
(package private) static int |
getASMModifiers(Modifiers modifiers)
Translate bsh.Modifiers into ASM modifier bitflags. |
(package private) static This |
getClassInstanceThis(java.lang.Object instance,
java.lang.String className)
Get the instance bsh namespace field from the object instance. |
(package private) static This |
getClassStaticThis(java.lang.Class clas,
java.lang.String className)
Get the static bsh namespace field from the class. |
static ClassGeneratorUtil.ConstructorArgs |
getConstructorArgs(java.lang.String superClassName,
This classStaticThis,
java.lang.Object[] consArgs,
int index)
Evaluate the arguments (if any) for the constructor specified by the constructor index. |
(package private) static java.lang.String |
getMethodDescriptor(java.lang.String returnType,
java.lang.String[] paramTypes)
|
(package private) static java.lang.String[] |
getTypeDescriptors(java.lang.Class[] cparams)
|
static void |
initInstance(java.lang.Object instance,
java.lang.String className,
java.lang.Object[] args)
Initialize an instance of the class. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
static final java.lang.String BSHSTATIC
static final java.lang.String BSHTHIS
static final java.lang.String BSHSUPER
static final java.lang.String BSHINIT
static final java.lang.String BSHCONSTRUCTORS
static final int DEFAULTCONSTRUCTOR
static final java.lang.String OBJECT
java.lang.String className
java.lang.String fqClassName
java.lang.Class superClass
java.lang.String superClassName
java.lang.Class[] interfaces
Variable[] vars
java.lang.reflect.Constructor[] superConstructors
DelayedEvalBshMethod[] constructors
DelayedEvalBshMethod[] methods
NameSpace classStaticNameSpace
Modifiers classModifiers
boolean isInterface
| Constructor Detail |
public ClassGeneratorUtil(Modifiers classModifiers,
java.lang.String className,
java.lang.String packageName,
java.lang.Class superClass,
java.lang.Class[] interfaces,
Variable[] vars,
DelayedEvalBshMethod[] bshmethods,
NameSpace classStaticNameSpace,
boolean isInterface)
packageName - e.g. "com.foo.bar"| Method Detail |
public byte[] generateClass()
static int getASMModifiers(Modifiers modifiers)
static void generateField(java.lang.String fieldName,
java.lang.String type,
int modifiers,
ClassWriter cw)
static void generateMethod(java.lang.String className,
java.lang.String fqClassName,
java.lang.String methodName,
java.lang.String returnType,
java.lang.String[] paramTypes,
int modifiers,
ClassWriter cw)
void generateConstructor(int index,
java.lang.String[] paramTypes,
int modifiers,
ClassWriter cw)
void generateConstructorSwitch(int consIndex,
int argsVar,
int consArgsVar,
CodeVisitor cv)
This method also generates the code to call the static ClassGeneratorUtil getConstructorArgs() method which inspects the scripted constructor to find the alternate constructor signature (if any) and evalute the arguments at runtime. The getConstructorArgs() method returns the actual arguments as well as the index of the constructor to call.
static void doSwitchBranch(int index,
java.lang.String targetClassName,
java.lang.String[] paramTypes,
Label endLabel,
Label[] labels,
int consArgsVar,
CodeVisitor cv)
static java.lang.String getMethodDescriptor(java.lang.String returnType,
java.lang.String[] paramTypes)
static void generateSuperDelegateMethod(java.lang.Class superClass,
java.lang.String superClassName,
java.lang.String methodName,
java.lang.String returnType,
java.lang.String[] paramTypes,
int modifiers,
ClassWriter cw)
boolean classContainsMethod(java.lang.Class clas,
java.lang.String methodName,
java.lang.String[] paramTypes)
static void generatePlainReturnCode(java.lang.String returnType,
CodeVisitor cv)
public static void generateParameterReifierCode(java.lang.String[] paramTypes,
boolean isStatic,
CodeVisitor cv)
cv - the code visitor to be used to generate the bytecode.isStatic - the enclosing methods is static
public static void generateReturnCode(java.lang.String returnType,
CodeVisitor cv)
cv - the code visitor to be used to generate the bytecode.
public static ClassGeneratorUtil.ConstructorArgs getConstructorArgs(java.lang.String superClassName,
This classStaticThis,
java.lang.Object[] consArgs,
int index)
public static void initInstance(java.lang.Object instance,
java.lang.String className,
java.lang.Object[] args)
static This getClassStaticThis(java.lang.Class clas,
java.lang.String className)
className - may be the name of clas itself or a superclass of clas.
static This getClassInstanceThis(java.lang.Object instance,
java.lang.String className)
static java.lang.String[] getTypeDescriptors(java.lang.Class[] cparams)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||