public final class CtMethod extends CtBehavior
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
.
CtClass.getDeclaredMethods()
,
CtNewMethod
Modifier and Type | Class and Description |
---|---|
static class |
CtMethod.ConstParameter
Instances of this class represent a constant parameter.
|
Constructor and Description |
---|
CtMethod(CtClass returnType,
java.lang.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. |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object obj)
Indicates whether
obj has the same name and the
same signature as this method. |
java.lang.String |
getLongName()
Returns the method name followed by parameter types
such as
javassist.CtMethod.setBody(String) . |
java.lang.String |
getName()
Obtains the name of this method.
|
CtClass |
getReturnType()
Obtains the type of the returned value.
|
int |
hashCode()
Returns a hash code value for the method.
|
boolean |
isEmpty()
Returns true if the method body is empty, that is,
{} . |
static CtMethod |
make(MethodInfo minfo,
CtClass declaring)
Creates a method from a
MethodInfo object. |
static CtMethod |
make(java.lang.String src,
CtClass declaring)
Compiles the given source code and creates a method.
|
void |
setBody(CtMethod src,
ClassMap map)
Copies a method body from another method.
|
void |
setName(java.lang.String newname)
Changes the name of this method.
|
void |
setWrappedBody(CtMethod mbody,
CtMethod.ConstParameter constParam)
Replace a method body with a new method body wrapping the
given method.
|
addCatch, addCatch, addLocalVariable, addParameter, getAnnotation, getAnnotations, getAttribute, getAvailableAnnotations, getAvailableParameterAnnotations, getExceptionTypes, getGenericSignature, getMethodInfo, getMethodInfo2, getModifiers, getParameterAnnotations, getParameterTypes, getSignature, hasAnnotation, insertAfter, insertAfter, insertAt, insertAt, insertBefore, insertParameter, instrument, instrument, setAttribute, setBody, setBody, setExceptionTypes, setGenericSignature, setModifiers, useCflow
getDeclaringClass, hasAnnotation, toString, visibleFrom
public CtMethod(CtClass returnType, java.lang.String mname, CtClass[] parameters, CtClass declaring)
CtClass.addMethod()
.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 typesCtClass.addMethod(CtMethod)
public CtMethod(CtMethod src, CtClass declaring, ClassMap map) throws CannotCompileException
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.
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
.CannotCompileException
CtClass.addMethod(CtMethod)
,
ClassMap.fix(String)
public static CtMethod make(java.lang.String src, CtClass declaring) throws CannotCompileException
make()
in
CtNewMethod
. See it for more details.
CtNewMethod
has a number of useful factory methods.src
- the source text.declaring
- the class to which the created method is added.CannotCompileException
CtNewMethod.make(String, CtClass)
public static CtMethod make(MethodInfo minfo, CtClass declaring) throws CannotCompileException
MethodInfo
object.declaring
- the class declaring the method.CannotCompileException
- if the the MethodInfo
object and the declaring class have different
ConstPool
objectspublic int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
obj
has the same name and the
same signature as this method.equals
in class java.lang.Object
public java.lang.String getLongName()
javassist.CtMethod.setBody(String)
.getLongName
in class CtBehavior
public java.lang.String getName()
getName
in class CtMember
CtConstructor.getName()
public void setName(java.lang.String newname)
public CtClass getReturnType() throws NotFoundException
NotFoundException
public boolean isEmpty()
{}
.
It also returns true if the method is an abstract method.isEmpty
in class CtBehavior
public void setBody(CtMethod src, ClassMap map) throws CannotCompileException
All occurrences of the class names in the copied method body
are replaced with the names specified by
map
if map
is not null
.
src
- the method that the body is copied from.map
- the hashtable associating original class names
with substituted names.
It can be null
.CannotCompileException
public void setWrappedBody(CtMethod mbody, CtMethod.ConstParameter constParam) throws CannotCompileException
mbody
- the wrapped methodconstParam
- the constant parameter given to
the wrapped method
(maybe null
).CannotCompileException
CtNewMethod.wrapped(CtClass,String,CtClass[],CtClass[],CtMethod,CtMethod.ConstParameter,CtClass)
Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.