Class CtNewMethod
CtMethod
.
An instance of this class does not make any sense.- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic CtMethod
abstractMethod
(CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, CtClass declaring) Creates a public abstract method.static CtMethod
Creates a copy of a method with a new name.static CtMethod
Creates a copy of a method.static CtMethod
Creates a method forwarding to a delegate in a super class.static CtMethod
Creates a public getter method.static CtMethod
make
(int modifiers, CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) Creates a method.static CtMethod
Compiles the given source code and creates a method.static CtMethod
Compiles the given source code and creates a method.static CtMethod
make
(CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) Creates a public (non-static) method.static CtMethod
Creates a public setter method.static CtMethod
wrapped
(CtClass returnType, String mname, CtClass[] parameterTypes, CtClass[] exceptionTypes, CtMethod body, CtMethod.ConstParameter constParam, CtClass declaring) Creates a wrapped method.
-
Constructor Details
-
CtNewMethod
public CtNewMethod()
-
-
Method Details
-
make
Compiles the given source code and creates a method. The source code must include not only the method body but the whole declaration, for example,"public Object id(Object obj) { return obj; }"
- Parameters:
src
- the source text.declaring
- the class to which the created method is added.- Throws:
CannotCompileException
-
make
public static CtMethod make(String src, CtClass declaring, String delegateObj, String delegateMethod) throws CannotCompileException Compiles the given source code and creates a method. The source code must include not only the method body but the whole declaration, for example,"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.- Parameters:
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()
.- Throws:
CannotCompileException
-
make
public static CtMethod make(CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) throws CannotCompileException Creates a public (non-static) method. The created method cannot be changed to a static method later.- Parameters:
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 isnull
, the created method does nothing except returning zero or null.declaring
- the class to which the created method is added.- Throws:
CannotCompileException
- See Also:
-
make
public static CtMethod make(int modifiers, CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) throws CannotCompileException Creates a method.modifiers
can containModifier.STATIC
.- Parameters:
modifiers
- access modifiers.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 isnull
, the created method does nothing except returning zero or null.declaring
- the class to which the created method is added.- Throws:
CannotCompileException
- See Also:
-
copy
public static CtMethod copy(CtMethod src, CtClass declaring, ClassMap map) throws CannotCompileException Creates a copy of a method. This method is provided for creating a new method based on an existing method. This is a convenience method for callingthis constructor
. See the description of the constructor for particular behavior of the copying.- Parameters:
src
- the source method.declaring
- the class to which the created method is added.map
- the hash table associating original class names with substituted names. It can benull
.- Throws:
CannotCompileException
- See Also:
-
copy
public static CtMethod copy(CtMethod src, String name, CtClass declaring, ClassMap map) throws CannotCompileException Creates a copy of a method with a new name. This method is provided for creating a new method based on an existing method. This is a convenience method for callingthis constructor
. See the description of the constructor for particular behavior of the copying.- Parameters:
src
- the source method.name
- the name of the created method.declaring
- the class to which the created method is added.map
- the hash table associating original class names with substituted names. It can benull
.- Throws:
CannotCompileException
- See Also:
-
abstractMethod
public static CtMethod abstractMethod(CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, CtClass declaring) throws NotFoundException Creates a public abstract method.- Parameters:
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.- Throws:
NotFoundException
- See Also:
-
getter
Creates a public getter method. The getter method returns the value of the specified field in the class to which this method is added. The created method is initially not static even if the field is static. Change the modifiers if the method should be static.- Parameters:
methodName
- the name of the getterfield
- the field accessed.- Throws:
CannotCompileException
-
setter
Creates a public setter method. The setter method assigns the value of the first parameter to the specified field in the class to which this method is added. The created method is not static even if the field is static. You may not change it to be static bysetModifiers()
inCtBehavior
.- Parameters:
methodName
- the name of the setterfield
- the field accessed.- Throws:
CannotCompileException
-
delegator
public static CtMethod delegator(CtMethod delegate, CtClass declaring) throws CannotCompileException Creates a method forwarding to a delegate in a super class. The created method calls a method specified bydelegate
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()
.- Parameters:
delegate
- the method that the created method forwards to.declaring
- the class to which the created method is added.- Throws:
CannotCompileException
-
wrapped
public static CtMethod wrapped(CtClass returnType, String mname, CtClass[] parameterTypes, CtClass[] exceptionTypes, CtMethod body, CtMethod.ConstParameter constParam, CtClass declaring) throws CannotCompileException Creates a wrapped method. The wrapped method receives parameters in the form of an array ofObject
.The body of the created method is a copy of the body of the 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 onconstParam
. IfconstParam
isnull
, the signature must be:Object method(Object[] params)
The method body copied from
body
is wrapped in parameter-conversion code, which converts parameters specified byparameterTypes
into an array ofObject
. The returned value is also converted from theObject
type to the type specified byreturnType
. 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 ofcvalue
is specified byconstParam
.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 isvoid
, 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 the copied body super.addElement(args[0]); Object result = null; // end } }
Note that the type of the parameter to
add()
depends only on the value ofargTypes
passed toCtNewMethod.wrapped()
. Thus, it is easy to modify this program to produce aStringVector
class, which is a vector containing onlyString
objects, and other vector classes.- Parameters:
returnType
- the type of the returned value.mname
- the method name.parameterTypes
- a list of the parameter types.exceptionTypes
- a list of the exception types.body
- the method body (must not be a static method).constParam
- the constant parameter (maybenull
).declaring
- the class to which the created method is added.- Throws:
CannotCompileException
-