Top Description Constructors Methods
javassist

public Class CtNewMethod

extends Object
Class Inheritance
Imports
javassist.CtMethod.ConstParameter, javassist.bytecode.AccessFlag, .Bytecode, .ConstPool, .ExceptionsAttribute, .FieldInfo, .MethodInfo, javassist.compiler.CompileError, .Javac

A collection of static methods for creating a CtMethod. An instance of this class does not make any sense.
See Also
CtClass#addMethod(CtMethod)

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static CtMethod
abstractMethod(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
,
CtClass
the class to which the created method is added.
declaring
)

Creates a public abstract method.

public static CtMethod
copy(CtMethod
the source method.
src
,
CtClass
the class to which the created method is added.
declaring
,
ClassMap
the hash table associating original class names with substituted names. It can be null.
map
)

Creates a copy of a method.

public static CtMethod
copy(CtMethod
the source method.
src
,
String
the name of the created method.
name
,
CtClass
the class to which the created method is added.
declaring
,
ClassMap
the hash table associating original class names with substituted names. It can be null.
map
)

Creates a copy of a method with a new name.

public static CtMethod
delegator(CtMethod
the method that the created method forwards to.
delegate
,
CtClass
the class to which the created method is added.
declaring
)

Creates a method forwarding to a delegate in a super class.

private static CtMethod
delegator0(CtMethod delegate, CtClass declaring)

public static CtMethod
getter(String
the name of the getter
methodName
,
CtField
the field accessed.
field
)

Creates a public getter method.

public static CtMethod
make(String
the source text.
src
,
CtClass
the class to which the created method is added.
declaring
)

Compiles the given source code and creates a method.

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 $proceed().
delegateObj
,
String
the name of the method that is called by $proceed().
delegateMethod
)

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 {}. If it is null, the created method does nothing except returning zero or null.
body
,
CtClass
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 {}. If it is null, the created method does nothing except returning zero or null.
body
,
CtClass
the class to which the created method is added.
declaring
)

Creates a method.

public static CtMethod
setter(String
the name of the setter
methodName
,
CtField
the field accessed.
field
)

Creates a public setter method.

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.ConstParameter
the constant parameter (maybe null).
constParam
,
CtClass
the class to which the created method is added.
declaring
)

Creates a wrapped method.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CtNewMethodback to summary
public CtNewMethod()

Method Detail

abstractMethodback to summary
public static CtMethod abstractMethod(CtClass returnType, String mname, CtClass[] parameters, CtClass[] exceptions, CtClass declaring) throws NotFoundException

Creates a public abstract method.

Parameters
returnType:CtClass

the type of the returned value

mname:String

the method name

parameters:CtClass[]

a list of the parameter types

exceptions:CtClass[]

a list of the exception types

declaring:CtClass

the class to which the created method is added.

See Also
CtMethod#CtMethod(CtClass, String, CtClass[], CtClass)
copyback 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 this constructor. See the description of the constructor for particular behavior of the copying.

Parameters
src:CtMethod

the source method.

declaring:CtClass

the class to which the created method is added.

map:ClassMap

the hash table associating original class names with substituted names. It can be null.

See Also
CtMethod#CtMethod(CtMethod, CtClass, ClassMap)
copyback 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 this constructor. See the description of the constructor for particular behavior of the copying.

Parameters
src:CtMethod

the source method.

name:String

the name of the created method.

declaring:CtClass

the class to which the created method is added.

map:ClassMap

the hash table associating original class names with substituted names. It can be null.

See Also
CtMethod#CtMethod(CtMethod, CtClass, ClassMap)
delegatorback 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 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().

Parameters
delegate:CtMethod

the method that the created method forwards to.

declaring:CtClass

the class to which the created method is added.

delegator0back to summary
private static CtMethod delegator0(CtMethod delegate, CtClass declaring) throws CannotCompileException, NotFoundException
getterback 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.

Parameters
methodName:String

the name of the getter

field:CtField

the field accessed.

makeback 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; }"
Parameters
src:String

the source text.

declaring:CtClass

the class to which the created method is added.

makeback 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 $proceed(), then it is compiled into a method call on the specified object.

Parameters
src:String

the source text.

declaring:CtClass

the class to which the created method is added.

delegateObj:String

the source text specifying the object that is called on by $proceed().

delegateMethod:String

the name of the method that is called by $proceed().

makeback 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.

Parameters
returnType:CtClass

the type of the returned value.

mname:String

the method name.

parameters:CtClass[]

a list of the parameter types.

exceptions:CtClass[]

a list of the exception types.

body:String

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:CtClass

the class to which the created method is added.

See Also
make(int, CtClass, String, CtClass[], CtClass[], String, CtClass)
makeback 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. modifiers can contain Modifier.STATIC.

Parameters
modifiers:int

access modifiers.

returnType:CtClass

the type of the returned value.

mname:String

the method name.

parameters:CtClass[]

a list of the parameter types.

exceptions:CtClass[]

a list of the exception types.

body:String

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:CtClass

the class to which the created method is added.

See Also
Modifier
setterback 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 setModifiers() in CtBehavior.

Parameters
methodName:String

the name of the setter

field:CtField

the field accessed.

wrappedback to summary
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 of Object.

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 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 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 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.

Parameters
returnType:CtClass

the type of the returned value.

mname:String

the method name.

parameterTypes:CtClass[]

a list of the parameter types.

exceptionTypes:CtClass[]

a list of the exception types.

body:CtMethod

the method body (must not be a static method).

constParam:CtMethod.ConstParameter

the constant parameter (maybe null).

declaring:CtClass

the class to which the created method is added.