|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sadun.util.codegen.ObjectWrapperGenerator
This class generates wrapper classes for existing classes, in both source and compiled form. tools.jar must be in classpath if Class compilation is required.
The wrapper class source code is produced in a working directory (by default, the temporary directory) and compiled on the fly. The resulting class will implement the same interfaces as the wrapped object.
Wrapping is performed in two phases: preparation and code/class generation.
The preparation phase starts by invoking beginWrap()
(or overloads)
which declares which class to wrap. The wrapper class will have a method for each public
method exposed by the original wrapped class. static methods will simply invoke
the corresponding method of the wrapped class.
Further methods can be added programmatically by addMethod()
(or overloads), defining signature and body. An incorrect body will generate compilation
errors if an attempt to produce a Class object is executed later.
The standard implementation for the wrapper object methods consists in simply invoking corresponding
methods in the wrapped object. However, prologue or epilogue code (occurring before or after the wrapped object
method invocation) can be added by using the setPrologue()
and setEpilogue()
methods (in various overloads).
Prologue/epilogue code may have a set of thrown exceptions attached.
Prologue and epilogue code can contain references to the some values depending on the current method, as of the following table, expressed by the syntax $(symbol name).
Unless declared otherwise when invoking beginWrap()
, the generated
wrappers implement the Wrapper
interface.
Symbol | Description |
$(methodName) | the name of the method |
$(returnType) | the return type of the method |
$(paramNames) | the parameter types of the method |
$(paramTypes) | the parameter types of the method |
For example, the following code is valid in prologue/epilogue: System.out.println("Invoking wrapped $(methodName)");
endWrap()
terminates the preparation phase.
At this point, either createCode()
, createClass()
or wrap()
may be invoked to produce either the source code, a compiled Class object
or a wrapper object instance.
Here's an example of typical inteface-based usage:
// Create a generator
ObjectWrapperGenerator gen = new ObjectWrapperGenerator();
// Define the class to wrap
Class cls = ArrayList.class;
gen.beginWrap(cls, "ListWrapper");
// Add a prologue to all methods
gen.setPrologue(
cls.getMethods(),
"System.out.println(\"Wrapped call to \\\"$(methodName)\\\"\");",
new Class[] { RuntimeException.class });
// End the preparation phase
gen.endWrap();
// Create a wrapped object instance and cast to the List interface
List l = (List) gen.wrap(new ArrayList());
// Use the wrapped object
l.add(new String("Hello"));
// Use the Wrapper
interface
System.out.println("Wrapping "+((Wrapper)l).getWrappedObject().getClass());
Constructor Summary | |
ObjectWrapperGenerator()
|
Method Summary | |
void |
addInterface(java.lang.Class interfaceCls)
Add an interface and a corresponding implementing object to the wrapped object. |
void |
addInterface(java.lang.Class interfaceCls,
java.lang.reflect.Method method,
java.lang.String code)
|
void |
addInterface(java.lang.Class interfaceCls,
java.lang.String name,
java.lang.Class[] paramTypes,
java.lang.String[] paramNames,
java.lang.Class returnType,
java.lang.String code)
|
void |
addMethod(java.lang.String name,
java.lang.Class[] paramTypes,
java.lang.Class returnType,
java.lang.String bodyCode)
Add a public method specification to the wrapper class. |
void |
addMethod(java.lang.String name,
java.lang.Class[] paramTypes,
java.lang.Class returnType,
java.lang.String bodyCode,
int modifiers)
Add a method specification to the wrapper class. |
void |
addMethod(java.lang.String name,
java.lang.Class[] paramTypes,
java.lang.String[] paramNames,
java.lang.Class returnType,
java.lang.String bodyCode)
Add a public method specification to the wrapper class. |
void |
addMethod(java.lang.String name,
java.lang.Class[] paramTypes,
java.lang.String[] paramNames,
java.lang.Class returnType,
java.lang.String bodyCode,
int modifiers)
Add a method specification to the wrapper class. |
void |
beginWrap(java.lang.Class cls)
Begin the preparation phase, setting which class is to be wrapped. |
void |
beginWrap(java.lang.Class cls,
boolean implementWrapperInterface)
Begin the preparation phase, setting which class is to be wrapped. |
void |
beginWrap(java.lang.Class cls,
java.lang.String wrapperClassName)
Begin the preparation phase, setting which class is to be wrapped and the fully qualified name of the wrapper class. |
void |
beginWrap(java.lang.Class cls,
java.lang.String wrapperClassName,
boolean implementWrapperInterface)
Begin the preparation phase, setting which class is to be wrapped and the name of the wrapper class. |
java.lang.Class |
createClass()
Return a wrapper class as a Class Object. |
java.lang.String |
createCode()
Create the source code for the class. |
void |
endWrap()
Terminate the preparation phase. |
java.lang.String |
getWorkingDirectory()
Get the working directory, where source and class files are stored. |
boolean |
isVerbose()
Return true if the generator is in verbose mode, otherwise false. |
static java.lang.String[] |
makeParamNames(java.lang.String methodName,
java.lang.Class[] paramTypes)
|
void |
setEpilogue(java.lang.reflect.Method[] methods,
java.lang.String epilogueCode)
Set epilogue code for a set of wrapped method. |
void |
setEpilogue(java.lang.reflect.Method[] methods,
java.lang.String prologueCode,
java.lang.Class[] throwedExceptions)
Set epilogue code for a set of wrapped method. |
void |
setEpilogue(java.lang.reflect.Method m,
java.lang.String epilogueCode)
Set epilogue code for a wrapped method. |
void |
setEpilogue(java.lang.reflect.Method m,
java.lang.String epilogueCode,
java.lang.Class[] throwedExceptions)
Set epilogue code for a wrapped method. |
void |
setEpilogue(java.lang.String methodName,
java.lang.Class[] paramTypes,
java.lang.String prologueCode)
Set epilogue code for a wrapped method, defined by name and parameter types. |
void |
setPrologue(java.lang.reflect.Method[] methods,
java.lang.String prologueCode)
Set prologue code for a set of methods. |
void |
setPrologue(java.lang.reflect.Method[] methods,
java.lang.String prologueCode,
java.lang.Class[] throwedExceptions)
Set prologue code for a set of methods. |
void |
setPrologue(java.lang.reflect.Method m,
java.lang.String prologueCode)
Set prologue code for a wrapped method, defined by name and parameter types. |
void |
setPrologue(java.lang.reflect.Method m,
java.lang.String prologueCode,
java.lang.Class[] throwedExceptions)
Set prologue code for a wrapped method, defined by name and parameter types. |
void |
setPrologue(java.lang.String methodName,
java.lang.Class[] paramTypes,
java.lang.String prologueCode)
Set prologue code for a wrapped method, defined by name and parameter types. |
void |
setVerbose(boolean verbose)
Set the verbose mode, which provides information on the generator's actions on standard error |
void |
setWorkingDirectory(java.lang.String workingDirectory)
Set the working directory, where source and class files are stored. |
java.lang.Object |
wrap(java.lang.Object obj)
Return an instance of the wrapper object for the given object (when no other implementors are necessary). |
java.lang.Object |
wrap(java.lang.Object[] obj)
Return an instance of the wrapper object for the given object. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public ObjectWrapperGenerator()
Method Detail |
public static java.lang.String[] makeParamNames(java.lang.String methodName, java.lang.Class[] paramTypes)
public void beginWrap(java.lang.Class cls, boolean implementWrapperInterface)
For example, the wrapper for a class MyClass will be named MyClassWrapper.
cls
- the class to be wrappedimplementWrapperInterface
- if true, the wrapper object will implement the
Wrapper
interfacepublic void beginWrap(java.lang.Class cls)
For example, the wrapper for a class MyClass will be named MyClassWrapper.
cls
- the class to be wrappedpublic void beginWrap(java.lang.Class cls, java.lang.String wrapperClassName)
cls
- the class to be wrappedwrapperClassName
- the fully qualified name of the wrapper classpublic void beginWrap(java.lang.Class cls, java.lang.String wrapperClassName, boolean implementWrapperInterface)
cls
- the class to be wrappedwrapperClassName
- the fully qualified name of the wrapper classimplementWrapperInterface
- if true, the wrapper object will implement the
Wrapper
interfacepublic void addInterface(java.lang.Class interfaceCls)
interfaceCls
- public void addInterface(java.lang.Class interfaceCls, java.lang.String name, java.lang.Class[] paramTypes, java.lang.String[] paramNames, java.lang.Class returnType, java.lang.String code)
public void addInterface(java.lang.Class interfaceCls, java.lang.reflect.Method method, java.lang.String code)
public void setPrologue(java.lang.reflect.Method m, java.lang.String prologueCode)
The prloogue code is executed before the invocation of the wrapped object method occurs. The prologue code can contain refereneces to symbols (see class description).
If the code throws any checked exception, use the setPrologue() with exceptions
overload.
m
- the wrapped methodprologueCode
- the codepublic void setPrologue(java.lang.reflect.Method m, java.lang.String prologueCode, java.lang.Class[] throwedExceptions)
The prloogue code is executed before the invocation of the wrapped object method occurs. The prologue code can contain refereneces to symbols (see class description).
m
- prologueCode
- throwedExceptions
- public void setPrologue(java.lang.reflect.Method[] methods, java.lang.String prologueCode, java.lang.Class[] throwedExceptions)
The prologue code is executed before the invocation of the wrapped object method occurs. The prologue code can contain refereneces to symbols (see class description).
methods
- the set of wrapped method with the same prologueprologueCode
- the codethrowedExceptions
- the exception thrown by the code.public void setPrologue(java.lang.reflect.Method[] methods, java.lang.String prologueCode)
The prloogue code is executed before the invocation of the wrapped object method occurs. The prologue code can contain refereneces to symbols (see class description).
If the code throws any checked exception, use the setPrologue() with exceptions
overload.
methods
- the set of wrapped method with the same prologueprologueCode
- the codepublic void setPrologue(java.lang.String methodName, java.lang.Class[] paramTypes, java.lang.String prologueCode)
The prloogue code is executed before the invocation of the wrapped object method occurs. The prologue code can contain refereneces to symbols (see class description).
methodName
- the name of the wrapped methodparamTypes
- the parameter types of the wrapped methodprologueCode
- the codepublic void setEpilogue(java.lang.String methodName, java.lang.Class[] paramTypes, java.lang.String prologueCode)
The epilogue code is executed after the invocation of the wrapped object method occurs. The epilogue code can contain refereneces to symbols (see class description).
If the code throws any checked exception, use the setEpilogue() with exceptions
overload.
methodName
- the name of the wrapped methodparamTypes
- the parameters of the wrapped methodprologueCode
- the codepublic void setEpilogue(java.lang.reflect.Method m, java.lang.String epilogueCode)
The epilogue code is executed after the invocation of the wrapped object method occurs. The epilogue code can contain refereneces to symbols (see class description).
If the code throws any checked exception, use the setEpilogue() with exceptions
overload.
m
- the wrapped methodepilogueCode
- the codepublic void setEpilogue(java.lang.reflect.Method m, java.lang.String epilogueCode, java.lang.Class[] throwedExceptions)
The epilogue code is executed after the invocation of the wrapped object method occurs. The epilogue code can contain refereneces to symbols (see class description).
m
- the wrapped methodepilogueCode
- the codethrowedExceptions
- the exceptions thrown by the codepublic void setEpilogue(java.lang.reflect.Method[] methods, java.lang.String prologueCode, java.lang.Class[] throwedExceptions)
The epilogue code is executed after the invocation of the wrapped object method occurs. The epilogue code can contain refereneces to symbols (see class description).
methods
- the methods to wrapthrowedExceptions
- the exceptions thrown by the codepublic void setEpilogue(java.lang.reflect.Method[] methods, java.lang.String epilogueCode)
The epilogue code is executed after the invocation of the wrapped object method occurs. The epilogue code can contain refereneces to symbols (see class description).
If the code throws any checked exception, use the setEpilogue() with exceptions
overload.
methods
- the methods to wrapepilogueCode
- the codepublic void addMethod(java.lang.String name, java.lang.Class[] paramTypes, java.lang.String[] paramNames, java.lang.Class returnType, java.lang.String bodyCode, int modifiers)
name
- the method nameparamTypes
- the types of the parametersparamNames
- the names of the parametersreturnType
- the return typebodyCode
- the Java source code of the method bodymodifiers
- the modifiers for the methodpublic void addMethod(java.lang.String name, java.lang.Class[] paramTypes, java.lang.Class returnType, java.lang.String bodyCode, int modifiers)
The names of the parameters are automatically generated by using a VariableNameGenerator
object and following the rules defined therein.
name
- the method nameparamTypes
- the types of the parametersreturnType
- the return typebodyCode
- the Java source code of the method bodymodifiers
- the modifiers for the methodpublic void addMethod(java.lang.String name, java.lang.Class[] paramTypes, java.lang.String[] paramNames, java.lang.Class returnType, java.lang.String bodyCode)
name
- the method nameparamTypes
- the types of the parametersparamNames
- the names of the parametersreturnType
- the return typebodyCode
- the Java source code of the method bodypublic void addMethod(java.lang.String name, java.lang.Class[] paramTypes, java.lang.Class returnType, java.lang.String bodyCode)
The names of the parameters are automatically generated by using a VariableNameGenerator
object and following the rules defined therein.
name
- the method nameparamTypes
- the types of the parametersreturnType
- the return typebodyCode
- the Java source code of the method bodypublic void endWrap()
public java.lang.String createCode()
public java.lang.Class createClass() throws ObjectWrapperGeneratorException
java.io.IOException
ObjectWrapperGeneratorException
public java.lang.Object wrap(java.lang.Object obj) throws ObjectWrapperGeneratorException
obj
-
java.io.IOException
ObjectWrapperGeneratorException
public java.lang.Object wrap(java.lang.Object[] obj) throws ObjectWrapperGeneratorException
obj
-
java.io.IOException
ObjectWrapperGeneratorException
public java.lang.String getWorkingDirectory()
public void setWorkingDirectory(java.lang.String workingDirectory)
workingDirectory
- the directory where source and class files are stored.public boolean isVerbose()
public void setVerbose(boolean verbose)
verbose
- true or false to enable/disable the verbose mode
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |