javassist

Class CtMethod

public final class CtMethod extends CtBehavior

An instance of CtMethod represents a method.

See the super class CtBehavior since a number of useful methods are in CtBehavior. A number of useful factory methods are in CtNewMethod.

See Also: getDeclaredMethods CtNewMethod

Nested Class Summary
static classCtMethod.ConstParameter
Instances of this class represent a constant parameter.
Constructor Summary
CtMethod(CtClass returnType, String mname, CtClass[] parameters, CtClass declaring)
Creates a public abstract method.
CtMethod(CtMethod src, CtClass declaring, ClassMap map)
Creates a copy of a CtMethod object.
Method Summary
booleanequals(Object obj)
Indicates whether obj has the same name and the same signature as this method.
StringgetLongName()
Returns the method name followed by parameter types such as javassist.CtMethod.setBody(String).
StringgetName()
Obtains the name of this method.
CtClassgetReturnType()
Obtains the type of the returned value.
inthashCode()
Returns a hash code value for the method.
booleanisEmpty()
Returns true if the method body is empty, that is, {}.
static CtMethodmake(String src, CtClass declaring)
Compiles the given source code and creates a method.
voidsetBody(CtMethod src, ClassMap map)
Copies a method body from another method.
voidsetName(String newname)
Changes the name of this method.
voidsetWrappedBody(CtMethod mbody, CtMethod.ConstParameter constParam)
Replace a method body with a new method body wrapping the given method.

Constructor Detail

CtMethod

public CtMethod(CtClass returnType, String mname, CtClass[] parameters, CtClass declaring)
Creates a public abstract method. The created method must be added to a class with CtClass.addMethod().

Parameters: declaring the class to which the created method is added. returnType the type of the returned value mname the method name parameters a list of the parameter types

See Also: addMethod

CtMethod

public CtMethod(CtMethod src, CtClass declaring, ClassMap map)
Creates a copy of a CtMethod object. The created method must be added to a class with CtClass.addMethod().

All occurrences of class names in the created method are replaced with names specified by map if map is not null.

For example, suppose that a method at() is as follows:

(X is a class name.) If map substitutes String for X, then the created method is:

By default, all the occurrences of the names of the class declaring at() and the superclass are replaced with the name of the class and the superclass that the created method is added to. This is done whichever map is null or not. To prevent this replacement, call ClassMap.fix().

Note: if the .class notation (for example, String.class) is included in an expression, the Javac compiler may produce a helper method. Since this constructor never copies this helper method, the programmers have the responsiblity of copying it. Otherwise, use Class.forName() in the expression.

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

See Also: addMethod fix

Method Detail

equals

public boolean equals(Object obj)
Indicates whether obj has the same name and the same signature as this method.

getLongName

public String getLongName()
Returns the method name followed by parameter types such as javassist.CtMethod.setBody(String).

Since: 3.5

getName

public String getName()
Obtains the name of this method.

getReturnType

public CtClass getReturnType()
Obtains the type of the returned value.

hashCode

public int hashCode()
Returns a hash code value for the method. If two methods have the same name and signature, then the hash codes for the two methods are equal.

isEmpty

public boolean isEmpty()
Returns true if the method body is empty, that is, {}. It also returns true if the method is an abstract method.

make

public static CtMethod make(String src, CtClass declaring)
Compiles the given source code and creates a method. This method simply delegates to make() in CtNewMethod. See it for more details. CtNewMethod has a number of useful factory methods.

Parameters: src the source text. declaring the class to which the created method is added.

See Also: CtNewMethod

setBody

public void setBody(CtMethod src, ClassMap map)
Copies a method body from another method. If this method is abstract, the abstract modifier is removed after the method body is copied.

All occurrences of the class names in the copied method body are replaced with the names specified by map if map is not null.

Parameters: src the method that the body is copied from. map the hashtable associating original class names with substituted names. It can be null.

setName

public void setName(String newname)
Changes the name of this method.

setWrappedBody

public void setWrappedBody(CtMethod mbody, CtMethod.ConstParameter constParam)
Replace a method body with a new method body wrapping the given method.

Parameters: mbody the wrapped method constParam the constant parameter given to the wrapped method (maybe null).

See Also: (CtClass,String,CtClass[],CtClass[],CtMethod,CtMethod.ConstParameter,CtClass)

Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.