CtMethod.
An instance of this class does not make any sense.
CtClass#addMethod(CtMethod)
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static CtMethod | |
| public static CtMethod | |
| public static CtMethod | |
| public static CtMethod | |
| private static CtMethod | |
| public static CtMethod | |
| public static CtMethod | |
| public static CtMethod | make(String
the source text. src, CtClass the class to which the created method is added. declaring, String the source text specifying the object
that is called on by delegateObj, String $proceed().the name of the method
that is called by delegateMethod)$proceed().Compiles the given source code and creates a method. |
| public static CtMethod | make(CtClass
the type of the returned value. returnType, String the method name. mname, CtClass[] a list of the parameter types. parameters, CtClass[] a list of the exception types. exceptions, String the source text of the method body.
It must be a block surrounded by body, CtClass {}.
If it is null, the created method
does nothing except returning zero or null.the class to which the created method is added. declaring)Creates a public (non-static) method. |
| public static CtMethod | make(int
access modifiers. modifiers, CtClass the type of the returned value. returnType, String the method name. mname, CtClass[] a list of the parameter types. parameters, CtClass[] a list of the exception types. exceptions, String the source text of the method body.
It must be a block surrounded by body, CtClass {}.
If it is null, the created method
does nothing except returning zero or null.the class to which the created method is added. declaring)Creates a method. |
| public static CtMethod | |
| public static CtMethod | wrapped(CtClass
the type of the returned value. returnType, String the method name. mname, CtClass[] a list of the parameter types. parameterTypes, CtClass[] a list of the exception types. exceptionTypes, CtMethod the method body
(must not be a static method). body, CtMethod.the constant parameter
(maybe constParam,null).the class to which the created method is
added. declaring)Creates a wrapped method. |
| CtNewMethod | back to summary |
|---|---|
| public CtNewMethod() | |
| abstractMethod | back to summary |
|---|---|
| public static CtMethod abstractMethod(CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, CtClass declaring) throws NotFoundException Creates a public abstract method.
| |
| copy | back to summary |
|---|---|
| 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 calling
| |
| copy | back to summary |
|---|---|
| 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 calling
| |
| delegator | back to summary |
|---|---|
| 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
by 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
| |
| delegator0 | back to summary |
|---|---|
| private static CtMethod delegator0(CtMethod delegate, CtClass declaring) throws CannotCompileException, NotFoundException | |
| getter | back to summary |
|---|---|
| public static CtMethod getter(String methodName, CtField field) throws CannotCompileException 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. | |
| make | back to summary |
|---|---|
| public static CtMethod make(String src, CtClass declaring) 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; }"
| |
| make | back to summary |
|---|---|
| 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 | |
| make | back to summary |
|---|---|
| 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.
| |
| make | back to summary |
|---|---|
| public static CtMethod make(int modifiers, CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) throws CannotCompileException Creates a method.
| |
| setter | back to summary |
|---|---|
| public static CtMethod setter(String methodName, CtField field) throws CannotCompileException 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
by | |
| wrapped | back to summary |
|---|---|
| public static CtMethod wrapped(CtClass returnType, String mname, CtClass[] parameterTypes, CtClass[] exceptionTypes, CtMethod body, CtMethod. Creates a wrapped method. The wrapped method receives parameters
in the form of an array of The body of the created method is a copy of the body of the method
specified by The method specified by Object method(Object[] params, <type> cvalue) The type of the Object method(Object[] params) The method body copied from
Object[] params = new Object[] { p0, p1, ... };
<type> cvalue = <constant-value>;
... copied method body ...
Object result = <returned value>
return (<returnType>)result;
The variables 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
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 public class Sample extends java.util.Vector {
public Object add0(Object[] args) {
super.addElement(args[0]);
return null;
}
}
This program produces a class 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
| |