Top Description Inners Fields Constructors Methods
javassist

public final Class CtMethod

extends CtBehavior
Class Inheritance
Imports
javassist.bytecode.AccessFlag, .BadBytecode, .Bytecode, .CodeAttribute, .CodeIterator, .ConstPool, .Descriptor, .MethodInfo, .Opcode

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
CtClass#getDeclaredMethods(), CtNewMethod

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
CtMethod.ConstParameter

Instances of this class represent a constant parameter.

pack-priv static class
pack-priv static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
protected String
Inherited from javassist.CtBehavior:
methodInfo

Constructor Summary

AccessConstructor and Description
pack-priv
CtMethod(MethodInfo minfo, CtClass declaring)

public
CtMethod(CtClass
the type of the returned value
returnType
,
String
the method name
mname
,
CtClass[]
a list of the parameter types
parameters
,
CtClass
the class to which the created method is added.
declaring
)

Creates a public abstract method.

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

Creates a copy of a CtMethod object.

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether obj has the same name and the same signature as this method.

public String
getLongName()

Implements abstract javassist.CtBehavior.getLongName.

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

public String
getName()

Implements abstract javassist.CtMember.getName.

Obtains the name of this method.

public CtClass
getReturnType()

Obtains the type of the returned value.

pack-priv final String
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the method.

public boolean
isEmpty()

Implements abstract javassist.CtBehavior.isEmpty.

Returns true if the method body is empty, that is, {}.

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(MethodInfo minfo, CtClass
the class declaring the method.
declaring
)

Creates a method from a MethodInfo object.

pack-priv void
nameReplaced()

Overrides javassist.CtMember.nameReplaced.

This method is invoked when setName() or replaceClassName() in CtClass is called.

public void
setBody(CtMethod
the method that the body is copied from.
src
,
ClassMap
the hashtable associating original class names with substituted names. It can be null.
map
)

Copies a method body from another method.

public void
setName(String newname)

Changes the name of this method.

public void
setWrappedBody(CtMethod
the wrapped method
mbody
,
CtMethod.ConstParameter
the constant parameter given to the wrapped method (maybe null).
constParam
)

Replace a method body with a new method body wrapping the given method.

Inherited from javassist.CtBehavior:
addCatchaddCatchaddLocalVariableaddParametercopyextendToStringgetAnnotationgetAnnotationsgetAttributegetAvailableAnnotationsgetAvailableParameterAnnotationsgetExceptionTypesgetGenericSignaturegetMethodInfogetMethodInfo2getModifiersgetParameterAnnotationsgetParameterAnnotationsgetParameterTypesgetReturnType0getSignaturegetStartPosOfBodyhasAnnotationinsertAfterinsertAfterinsertAfterinsertAtinsertAtinsertBeforeinsertParameterinstrumentinstrumentsetAttributesetBodysetBodysetBody0setExceptionTypessetGenericSignaturesetModifiersuseCflow

Field Detail

cachedStringRepback to summary
protected String cachedStringRep

Constructor Detail

CtMethodback to summary
pack-priv CtMethod(MethodInfo minfo, CtClass declaring)
See Also
make(MethodInfo minfo, CtClass declaring)
CtMethodback to summary
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
returnType:CtClass

the type of the returned value

mname:String

the method name

parameters:CtClass[]

a list of the parameter types

declaring:CtClass

the class to which the created method is added.

See Also
CtClass#addMethod(CtMethod)
CtMethodback to summary
public CtMethod(CtMethod src, CtClass declaring, ClassMap map) throws CannotCompileException

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:

public X at(int i) {
    return (X)super.elementAt(i);
}

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

public String at(int i) {
    return (String)super.elementAt(i);
}

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() or put() to explicitly specify replacement.

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

the source method.

declaring:CtClass

the class to which the created method is added.

map:ClassMap

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

See Also
CtClass#addMethod(CtMethod), ClassMap#fix(String)

Method Detail

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether obj has the same name and the same signature as this method.

Parameters
obj:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getLongNameback to summary
public String getLongName()

Implements abstract javassist.CtBehavior.getLongName.

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

Annotations
@Override
Since
3.5
getNameback to summary
public String getName()

Implements abstract javassist.CtMember.getName.

Obtains the name of this method.

Annotations
@Override
getReturnTypeback to summary
public CtClass getReturnType() throws NotFoundException

Obtains the type of the returned value.

getStringRepback to summary
pack-priv final String getStringRep()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.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.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Implements abstract javassist.CtBehavior.isEmpty.

Returns true if the method body is empty, that is, {}. It also returns true if the method is an abstract method.

Annotations
@Override
makeback to summary
public static CtMethod make(String src, CtClass declaring) throws CannotCompileException

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

the source text.

declaring:CtClass

the class to which the created method is added.

See Also
CtNewMethod#make(String, CtClass)
makeback to summary
public static CtMethod make(MethodInfo minfo, CtClass declaring) throws CannotCompileException

Creates a method from a MethodInfo object.

Parameters
declaring:CtClass

the class declaring the method.

Exceptions
CannotCompileException:
if the the MethodInfo object and the declaring class have different ConstPool objects
Since
3.6
nameReplacedback to summary
pack-priv void nameReplaced()

Overrides javassist.CtMember.nameReplaced.

This method is invoked when setName() or replaceClassName() in CtClass is called.

Annotations
@Override
setBodyback to summary
public void setBody(CtMethod src, ClassMap map) throws CannotCompileException

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

the method that the body is copied from.

map:ClassMap

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

setNameback to summary
public void setName(String newname)

Changes the name of this method.

setWrappedBodyback to summary
public void setWrappedBody(CtMethod mbody, CtMethod.ConstParameter constParam) throws CannotCompileException

Replace a method body with a new method body wrapping the given method.

Parameters
mbody:CtMethod

the wrapped method

constParam:CtMethod.ConstParameter

the constant parameter given to the wrapped method (maybe null).

See Also
CtNewMethod#wrapped(CtClass, String, CtClass[], CtClass[], CtMethod, CtMethod.ConstParameter, CtClass)
javassist back to summary

public Class CtMethod.ConstParameter

extends Object
Class Inheritance
Known Direct Subclasses
javassist.CtMethod.IntConstParameter, javassist.CtMethod.LongConstParameter, javassist.CtMethod.StringConstParameter

Instances of this class represent a constant parameter. They are used to specify the parameter given to the methods created by CtNewMethod.wrapped().
See Also
CtMethod#setWrappedBody(CtMethod, CtMethod.ConstParameter), CtNewMethod#wrapped(CtClass, String, CtClass[], CtClass[], CtMethod, CtMethod.ConstParameter, CtClass), CtNewConstructor#make(CtClass[], CtClass[], int, CtMethod, CtMethod.ConstParameter, CtClass)

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv int

Returns:

the size of the stack consumption.
compile
(Bytecode code)

pack-priv String
constDescriptor()

Returns the descriptor for constructors.

pack-priv static String
defaultConstDescriptor()

Returns the default descriptor for constructors.

pack-priv static String
pack-priv String
public static CtMethod.ConstParameter
integer(int
the constant value.
i
)

Makes an integer constant.

public static CtMethod.ConstParameter
integer(long
the constant value.
i
)

Makes a long integer constant.

public static CtMethod.ConstParameter
string(String
the constant value.
s
)

Makes an String constant.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ConstParameterback to summary
pack-priv ConstParameter()

Method Detail

compileback to summary
pack-priv int compile(Bytecode code) throws CannotCompileException
Returns:int

the size of the stack consumption.

constDescriptorback to summary
pack-priv String constDescriptor()

Returns the descriptor for constructors.

See Also
CtNewWrappedConstructor
defaultConstDescriptorback to summary
pack-priv static String defaultConstDescriptor()

Returns the default descriptor for constructors.

defaultDescriptorback to summary
pack-priv static String defaultDescriptor()
See Also
CtNewWrappedMethod
descriptorback to summary
pack-priv String descriptor()
integerback to summary
public static CtMethod.ConstParameter integer(int i)

Makes an integer constant.

Parameters
i:int

the constant value.

integerback to summary
public static CtMethod.ConstParameter integer(long i)

Makes a long integer constant.

Parameters
i:long

the constant value.

stringback to summary
public static CtMethod.ConstParameter string(String s)

Makes an String constant.

Parameters
s:String

the constant value.

javassist back to summary

pack-priv Class CtMethod.IntConstParameter

extends ConstParameter
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv int
pack-priv String
constDescriptor()

Overrides javassist.CtMethod.ConstParameter.constDescriptor.

Returns the descriptor for constructors.

pack-priv String
Inherited from javassist.CtMethod.ConstParameter:
defaultConstDescriptordefaultDescriptorintegerintegerstring

Field Detail

paramback to summary
pack-priv int param

Constructor Detail

IntConstParameterback to summary
pack-priv IntConstParameter(int i)

Method Detail

compileback to summary
pack-priv int compile(Bytecode code) throws CannotCompileException

Overrides javassist.CtMethod.ConstParameter.compile.

Returns:int

Doc from javassist.CtMethod.ConstParameter.compile.

the size of the stack consumption.

Annotations
@Override
constDescriptorback to summary
pack-priv String constDescriptor()

Overrides javassist.CtMethod.ConstParameter.constDescriptor.

Doc from javassist.CtMethod.ConstParameter.constDescriptor.

Returns the descriptor for constructors.

Annotations
@Override
descriptorback to summary
pack-priv String descriptor()

Overrides javassist.CtMethod.ConstParameter.descriptor.

Annotations
@Override
javassist back to summary

pack-priv Class CtMethod.LongConstParameter

extends ConstParameter
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv long

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv int
pack-priv String
constDescriptor()

Overrides javassist.CtMethod.ConstParameter.constDescriptor.

Returns the descriptor for constructors.

pack-priv String
Inherited from javassist.CtMethod.ConstParameter:
defaultConstDescriptordefaultDescriptorintegerintegerstring

Field Detail

paramback to summary
pack-priv long param

Constructor Detail

LongConstParameterback to summary
pack-priv LongConstParameter(long l)

Method Detail

compileback to summary
pack-priv int compile(Bytecode code) throws CannotCompileException

Overrides javassist.CtMethod.ConstParameter.compile.

Returns:int

Doc from javassist.CtMethod.ConstParameter.compile.

the size of the stack consumption.

Annotations
@Override
constDescriptorback to summary
pack-priv String constDescriptor()

Overrides javassist.CtMethod.ConstParameter.constDescriptor.

Doc from javassist.CtMethod.ConstParameter.constDescriptor.

Returns the descriptor for constructors.

Annotations
@Override
descriptorback to summary
pack-priv String descriptor()

Overrides javassist.CtMethod.ConstParameter.descriptor.

Annotations
@Override
javassist back to summary

pack-priv Class CtMethod.StringConstParameter

extends ConstParameter
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv String

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv int
pack-priv String
constDescriptor()

Overrides javassist.CtMethod.ConstParameter.constDescriptor.

Returns the descriptor for constructors.

pack-priv String
Inherited from javassist.CtMethod.ConstParameter:
defaultConstDescriptordefaultDescriptorintegerintegerstring

Field Detail

paramback to summary
pack-priv String param

Constructor Detail

StringConstParameterback to summary
pack-priv StringConstParameter(String s)

Method Detail

compileback to summary
pack-priv int compile(Bytecode code) throws CannotCompileException

Overrides javassist.CtMethod.ConstParameter.compile.

Returns:int

Doc from javassist.CtMethod.ConstParameter.compile.

the size of the stack consumption.

Annotations
@Override
constDescriptorback to summary
pack-priv String constDescriptor()

Overrides javassist.CtMethod.ConstParameter.constDescriptor.

Doc from javassist.CtMethod.ConstParameter.constDescriptor.

Returns the descriptor for constructors.

Annotations
@Override
descriptorback to summary
pack-priv String descriptor()

Overrides javassist.CtMethod.ConstParameter.descriptor.

Annotations
@Override