|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectclassUtils.javassist.CtNewMethod
public class CtNewMethod
A collection of static methods for creating a CtMethod
.
An instance of this class does not make any sense.
CompileTimeClass.addMethod(CtMethod)
Constructor Summary | |
---|---|
CtNewMethod()
|
Method Summary | |
---|---|
static CtMethod |
abstractMethod(CompileTimeClass returnType,
java.lang.String mname,
CompileTimeClass[] parameters,
CompileTimeClass[] exceptions,
CompileTimeClass declaring)
Creates a public abstract method. |
static CtMethod |
copy(CtMethod src,
CompileTimeClass declaring,
ClassMap map)
Creates a copy of a method. |
static CtMethod |
copy(CtMethod src,
java.lang.String name,
CompileTimeClass declaring,
ClassMap map)
Creates a copy of a method with a new name. |
static CtMethod |
delegator(CtMethod delegate,
CompileTimeClass declaring)
Creates a method forwarding to a delegate in a super class. |
static CtMethod |
getter(java.lang.String methodName,
CtField field)
Creates a public getter method. |
static CtMethod |
make(CompileTimeClass returnType,
java.lang.String mname,
CompileTimeClass[] parameters,
CompileTimeClass[] exceptions,
java.lang.String body,
CompileTimeClass declaring)
Creates a public method. |
static CtMethod |
make(java.lang.String src,
CompileTimeClass declaring)
Compiles the given source code and creates a method. |
static CtMethod |
make(java.lang.String src,
CompileTimeClass declaring,
java.lang.String delegateObj,
java.lang.String delegateMethod)
Compiles the given source code and creates a method. |
static CtMethod |
setter(java.lang.String methodName,
CtField field)
Creates a public setter method. |
static CtMethod |
wrapped(CompileTimeClass returnType,
java.lang.String mname,
CompileTimeClass[] parameterTypes,
CompileTimeClass[] exceptionTypes,
CtMethod body,
CtMethod.ConstParameter constParam,
CompileTimeClass declaring)
Creates a wrapped method. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public CtNewMethod()
Method Detail |
---|
public static CtMethod make(java.lang.String src, CompileTimeClass declaring) throws CannotCompileException
"public Object id(Object obj) { return obj; }"
src
- the source text.declaring
- the class to which the created method is added.
CannotCompileException
public static CtMethod make(java.lang.String src, CompileTimeClass declaring, java.lang.String delegateObj, java.lang.String delegateMethod) throws CannotCompileException
"public Object id(Object obj) { return obj; }"
If the source code includes $proceed()
, then
it is compiled into a method call on the specified object.
src
- the source text.declaring
- the class to which the created method is added.delegateObj
- the source text specifying the object
that is called on by $proceed()
.delegateMethod
- the name of the method
that is called by $proceed()
.
CannotCompileException
public static CtMethod make(CompileTimeClass returnType, java.lang.String mname, CompileTimeClass[] parameters, CompileTimeClass[] exceptions, java.lang.String body, CompileTimeClass declaring) throws CannotCompileException
returnType
- the type of the returned value.mname
- the method name.parameters
- a list of the parameter types.exceptions
- a list of the exception types.body
- the source text of the method body.
It must be a block surrounded by {}
.
If it is null
, the created method
does nothing except returning zero or null.declaring
- the class to which the created method is added.
CannotCompileException
public static CtMethod copy(CtMethod src, CompileTimeClass declaring, ClassMap map) throws CannotCompileException
src
- the source method.declaring
- the class to which the created method is added.map
- the hashtable associating original class names
with substituted names.
It can be null
.
CannotCompileException
CtMethod.CtMethod(CtMethod,CompileTimeClass,ClassMap)
public static CtMethod copy(CtMethod src, java.lang.String name, CompileTimeClass declaring, ClassMap map) throws CannotCompileException
src
- the source method.name
- the name of the created method.declaring
- the class to which the created method is added.map
- the hashtable associating original class names
with substituted names.
It can be null
.
CannotCompileException
CtMethod.CtMethod(CtMethod,CompileTimeClass,ClassMap)
public static CtMethod abstractMethod(CompileTimeClass returnType, java.lang.String mname, CompileTimeClass[] parameters, CompileTimeClass[] exceptions, CompileTimeClass declaring) throws NotFoundException
returnType
- the type of the returned valuemname
- the method nameparameters
- a list of the parameter typesexceptions
- a list of the exception typesdeclaring
- the class to which the created method is added.
NotFoundException
CtMethod.CtMethod(CompileTimeClass,String,CompileTimeClass[],CompileTimeClass)
public static CtMethod getter(java.lang.String methodName, CtField field) throws CannotCompileException
methodName
- the name of the getterfield
- the field accessed.
CannotCompileException
public static CtMethod setter(java.lang.String methodName, CtField field) throws CannotCompileException
methodName
- the name of the setterfield
- the field accessed.
CannotCompileException
public static CtMethod delegator(CtMethod delegate, CompileTimeClass declaring) throws CannotCompileException
delegate
with all the parameters passed to the
created method. If the delegate method returns a value,
the created method returns that value to the caller.
The delegate method must be declared in a super class.
The following method is an example of the created method.
int f(int p, int q) { return super.f(p, q); }
The name of the created method can be changed by
setName()
.
delegate
- the method that the created method forwards to.declaring
- the class to which the created method is
added.
CannotCompileException
public static CtMethod wrapped(CompileTimeClass returnType, java.lang.String mname, CompileTimeClass[] parameterTypes, CompileTimeClass[] exceptionTypes, CtMethod body, CtMethod.ConstParameter constParam, CompileTimeClass declaring) throws CannotCompileException
Object
.
The body of the created method is a copy of the body of a method
specified by body
. However, it is wrapped in
parameter-conversion code.
The method specified by body
must have this singature:
Object method(Object[] params, <type> cvalue)
The type of the cvalue
depends on
constParam
.
If constParam
is null
, the signature
must be:
Object method(Object[] params)
The method body copied from body
is wrapped in
parameter-conversion code, which converts parameters specified by
parameterTypes
into an array of Object
.
The returned value is also converted from the Object
type to the type specified by returnType
. Thus,
the resulting method body is as follows:
Object[] params = new Object[] { p0, p1, ... }; <type> cvalue = <constant-value>; ... copied method body ... Object result = <returned value> return (<returnType>)result;
The variables p0
, p2
, ... represent
formal parameters of the created method.
The value of cvalue
is specified by
constParam
.
If the type of a parameter or a returned value is a primitive
type, then the value is converted into a wrapper object such as
java.lang.Integer
. If the type of the returned value
is void
, the returned value is discarded.
Example:
ClassPool pool = ... ; CtClass vec = pool.makeClass("intVector"); vec.setSuperclass(pool.get("java.util.Vector")); CtMethod addMethod = pool.getMethod("Sample", "add0"); CtClass[] argTypes = { CtClass.intType }; CtMethod m = CtNewMethod.wrapped(CtClass.voidType, "add", argTypes, null, addMethod, null, vec); vec.addMethod(m);
where the class Sample
is as follows:
public class Sample extends java.util.Vector { public Object add0(Object[] args) { super.addElement(args[0]); return null; } }
This program produces a class intVector
:
public class intVector extends java.util.Vector { public void add(int p0) { Object[] args = new Object[] { p0 }; // begin of copied body super.addElement(args[0]); Object result = null; // end } }
Note that the type of the parameter to add()
depends
only on the value of argTypes
passed to
CtNewMethod.wrapped()
. Thus, it is easy to
modify this program to produce a
StringVector
class, which is a vector containing
only String
objects, and other vector classes.
returnType
- the type of the returned value.mname
- the method name.parameters
- a list of the parameter types.exceptions
- a list of the exception types.body
- the method body
(must not be a static method).constParam
- the constant parameter
(maybe null
).declaring
- the class to which the created method is
added.
CannotCompileException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |