Class CtMethod
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classInstances of this class represent a constant parameter. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanIndicates whetherobjhas the same name and the same signature as this method.Returns the method name followed by parameter types such asjavassist.CtMethod.setBody(String).getName()Obtains the name of this method.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 CtMethodCompiles the given source code and creates a method.static CtMethodmake(MethodInfo minfo, CtClass declaring) Creates a method from aMethodInfoobject.voidCopies a method body from another method.voidChanges the name of this method.voidsetWrappedBody(CtMethod mbody, CtMethod.ConstParameter constParam) Replace a method body with a new method body wrapping the given method.Methods inherited from class javassist.CtBehavior
addCatch, addCatch, addLocalVariable, addParameter, getAnnotation, getAnnotations, getAttribute, getAvailableAnnotations, getAvailableParameterAnnotations, getExceptionTypes, getGenericSignature, getMethodInfo, getMethodInfo2, getModifiers, getParameterAnnotations, getParameterTypes, getSignature, hasAnnotation, insertAfter, insertAfter, insertAfter, insertAt, insertAt, insertBefore, insertParameter, instrument, instrument, setAttribute, setBody, setBody, setExceptionTypes, setGenericSignature, setModifiers, useCflowMethods inherited from class javassist.CtMember
getDeclaringClass, hasAnnotation, toString, visibleFrom
-
Constructor Details
-
CtMethod
Creates a public abstract method. The created method must be added to a class withCtClass.addMethod().- Parameters:
declaring- the class to which the created method is added.returnType- the type of the returned valuemname- the method nameparameters- a list of the parameter types- See Also:
-
CtMethod
Creates a copy of aCtMethodobject. The created method must be added to a class withCtClass.addMethod().All occurrences of class names in the created method are replaced with names specified by
mapifmapis notnull.For example, suppose that a method
at()is as follows:public X at(int i) { return (X)super.elementAt(i); }(
Xis a class name.) IfmapsubstitutesStringforX, 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 whichevermapis null or not. To prevent this replacement, callClassMap.fix()orput()to explicitly specify replacement.Note: if the
.classnotation (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, useClass.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 benull.- Throws:
CannotCompileException- See Also:
-
-
Method Details
-
make
Compiles the given source code and creates a method. This method simply delegates tomake()inCtNewMethod. See it for more details.CtNewMethodhas a number of useful factory methods.- Parameters:
src- the source text.declaring- the class to which the created method is added.- Throws:
CannotCompileException- See Also:
-
make
Creates a method from aMethodInfoobject.- Parameters:
declaring- the class declaring the method.- Throws:
CannotCompileException- if the theMethodInfoobject and the declaring class have differentConstPoolobjects- Since:
- 3.6
-
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. -
equals
Indicates whetherobjhas the same name and the same signature as this method. -
getLongName
Returns the method name followed by parameter types such asjavassist.CtMethod.setBody(String).- Specified by:
getLongNamein classCtBehavior- Since:
- 3.5
-
getName
Obtains the name of this method. -
setName
Changes the name of this method. -
getReturnType
Obtains the type of the returned value.- Throws:
NotFoundException
-
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.- Specified by:
isEmptyin classCtBehavior
-
setBody
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
mapifmapis notnull.- Parameters:
src- the method that the body is copied from.map- the hashtable associating original class names with substituted names. It can benull.- Throws:
CannotCompileException
-
setWrappedBody
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- the wrapped methodconstParam- the constant parameter given to the wrapped method (maybenull).- Throws:
CannotCompileException- See Also:
-