Class CtConstructor
- java.lang.Object
-
- javassist.CtMember
-
- javassist.CtBehavior
-
- javassist.CtConstructor
-
public final class CtConstructor extends CtBehavior
An instance of CtConstructor represents a constructor. It may represent a static constructor (class initializer). To distinguish a constructor and a class initializer, callisClassInitializer()
.See the super class
CtBehavior
as well since a number of useful methods are inCtBehavior
.
-
-
Constructor Summary
Constructors Constructor Description CtConstructor(CtClass[] parameters, CtClass declaring)
Creates a constructor with no constructor body.CtConstructor(CtConstructor src, CtClass declaring, ClassMap map)
Creates a copy of aCtConstructor
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
callsSuper()
Returns true if this constructor calls a constructor of the super class.java.lang.String
getLongName()
Returns the constructor name followed by parameter types such asjavassist.CtConstructor(CtClass[],CtClass)
.java.lang.String
getName()
Obtains the name of this constructor.void
insertBeforeBody(java.lang.String src)
Inserts bytecode just after another constructor in the super class or this class is called.boolean
isClassInitializer()
Returns true if this object represents a static initializer.boolean
isConstructor()
Returns true if this object represents a constructor.boolean
isEmpty()
Returns true if the constructor (or static initializer) is the default one.void
setBody(java.lang.String src)
Sets a constructor body.void
setBody(CtConstructor src, ClassMap map)
Copies a constructor body from another constructor.CtMethod
toMethod(java.lang.String name, CtClass declaring)
Makes a copy of this constructor and converts it into a method.CtMethod
toMethod(java.lang.String name, CtClass declaring, ClassMap map)
Makes a copy of this constructor and converts it into a 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, setExceptionTypes, setGenericSignature, setModifiers, useCflow
-
Methods inherited from class javassist.CtMember
getDeclaringClass, hasAnnotation, toString, visibleFrom
-
-
-
-
Constructor Detail
-
CtConstructor
public CtConstructor(CtClass[] parameters, CtClass declaring)
Creates a constructor with no constructor body. The created constructor must be added to a class withCtClass.addConstructor()
.The created constructor does not include a constructor body, which must be specified with
setBody()
.- Parameters:
declaring
- the class to which the created method is added.parameters
- a list of the parameter types- See Also:
CtClass.addConstructor(CtConstructor)
,setBody(String)
,setBody(CtConstructor,ClassMap)
-
CtConstructor
public CtConstructor(CtConstructor src, CtClass declaring, ClassMap map) throws CannotCompileException
Creates a copy of aCtConstructor
object. The created constructor must be added to a class withCtClass.addConstructor()
.All occurrences of class names in the created constructor are replaced with names specified by
map
ifmap
is notnull
.By default, all the occurrences of the names of the class declaring
src
and the superclass are replaced with the name of the class and the superclass that the created constructor is added to. This is done whichevermap
is null or not. To prevent this replacement, callClassMap.fix()
orput()
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, 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:
CtClass.addConstructor(CtConstructor)
,ClassMap.fix(String)
-
-
Method Detail
-
isConstructor
public boolean isConstructor()
Returns true if this object represents a constructor.
-
isClassInitializer
public boolean isClassInitializer()
Returns true if this object represents a static initializer.
-
getLongName
public java.lang.String getLongName()
Returns the constructor name followed by parameter types such asjavassist.CtConstructor(CtClass[],CtClass)
.- Specified by:
getLongName
in classCtBehavior
- Since:
- 3.5
-
getName
public java.lang.String getName()
Obtains the name of this constructor. It is the same as the simple name of the class declaring this constructor. If this object represents a class initializer, then this method returns"<clinit>"
.
-
isEmpty
public boolean isEmpty()
Returns true if the constructor (or static initializer) is the default one. This method returns true if the constructor takes some arguments but it does not perform anything except callingsuper()
(the no-argument constructor of the super class).- Specified by:
isEmpty
in classCtBehavior
-
callsSuper
public boolean callsSuper() throws CannotCompileException
Returns true if this constructor calls a constructor of the super class. This method returns false if it calls another constructor of this class bythis()
.- Throws:
CannotCompileException
-
setBody
public void setBody(java.lang.String src) throws CannotCompileException
Sets a constructor body.- Overrides:
setBody
in classCtBehavior
- Parameters:
src
- the source code representing the constructor body. It must be a single statement or block. If it isnull
, the substituted constructor body does nothing except callingsuper()
.- Throws:
CannotCompileException
-
setBody
public void setBody(CtConstructor src, ClassMap map) throws CannotCompileException
Copies a constructor body from another constructor.All occurrences of the class names in the copied body are replaced with the names specified by
map
ifmap
is 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
-
insertBeforeBody
public void insertBeforeBody(java.lang.String src) throws CannotCompileException
Inserts bytecode just after another constructor in the super class or this class is called. It does not work if this object represents a class initializer.- Parameters:
src
- the source code representing the inserted bytecode. It must be a single statement or block.- Throws:
CannotCompileException
-
toMethod
public CtMethod toMethod(java.lang.String name, CtClass declaring) throws CannotCompileException
Makes a copy of this constructor and converts it into a method. The signature of the mehtod is the same as the that of this constructor. The return type isvoid
. The resulting method must be appended to the class specified bydeclaring
. If this constructor is a static initializer, the resulting method takes no parameter.An occurrence of another constructor call
this()
or a super constructor callsuper()
is eliminated from the resulting method.The immediate super class of the class declaring this constructor must be also a super class of the class declaring the resulting method. If the constructor accesses a field, the class declaring the resulting method must also declare a field with the same name and type.
- Parameters:
name
- the name of the resulting method.declaring
- the class declaring the resulting method.- Throws:
CannotCompileException
-
toMethod
public CtMethod toMethod(java.lang.String name, CtClass declaring, ClassMap map) throws CannotCompileException
Makes a copy of this constructor and converts it into a method. The signature of the method is the same as the that of this constructor. The return type isvoid
. The resulting method must be appended to the class specified bydeclaring
. If this constructor is a static initializer, the resulting method takes no parameter.An occurrence of another constructor call
this()
or a super constructor callsuper()
is eliminated from the resulting method.The immediate super class of the class declaring this constructor must be also a super class of the class declaring the resulting method (this is obviously true if the second parameter
declaring
is the same as the class declaring this constructor). If the constructor accesses a field, the class declaring the resulting method must also declare a field with the same name and type.- Parameters:
name
- the name of the resulting method.declaring
- the class declaring the resulting method. It is normally the same as the class declaring this constructor.map
- the hash table associating original class names with substituted names. The original class names will be replaced while making a copy.map
can benull
.- Throws:
CannotCompileException
-
-