Top Description Fields Constructors Methods
javassist

public final Class CtConstructor

extends CtBehavior
Class Inheritance
Imports
javassist.bytecode.BadBytecode, .Bytecode, .ClassFile, .CodeAttribute, .CodeIterator, .ConstPool, .Descriptor, .MethodInfo, .Opcode, javassist.compiler.CompileError, .Javac

An instance of CtConstructor represents a constructor. It may represent a static constructor (class initializer). To distinguish a constructor and a class initializer, call isClassInitializer().

See the super class CtBehavior as well since a number of useful methods are in CtBehavior.

See Also
CtClass#getDeclaredConstructors(), CtClass#getClassInitializer(), CtNewConstructor

Field Summary

Inherited from javassist.CtBehavior:
methodInfo

Constructor Summary

AccessConstructor and Description
protected
CtConstructor(MethodInfo minfo, CtClass declaring)

public
CtConstructor(CtClass[]
a list of the parameter types
parameters
,
CtClass
the class to which the created method is added.
declaring
)

Creates a constructor with no constructor body.

public
CtConstructor(CtConstructor
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 CtConstructor object.

Method Summary

Modifier and TypeMethod and Description
public boolean
callsSuper()

Returns true if this constructor calls a constructor of the super class.

public String
getLongName()

Implements abstract javassist.CtBehavior.getLongName.

Returns the constructor name followed by parameter types such as javassist.CtConstructor(CtClass[],CtClass).

public String
getName()

Implements abstract javassist.CtMember.getName.

Obtains the name of this constructor.

pack-priv int
private String
public void
insertBeforeBody(String
the source code representing the inserted bytecode. It must be a single statement or block.
src
)

Inserts bytecode just after another constructor in the super class or this class is called.

public boolean
isClassInitializer()

Returns true if this object represents a static initializer.

public boolean
isConstructor()

Returns true if this object represents a constructor.

public boolean
isEmpty()

Implements abstract javassist.CtBehavior.isEmpty.

Returns true if the constructor (or static initializer) is the default one.

private static void
public void
setBody(String
the source code representing the constructor body. It must be a single statement or block. If it is null, the substituted constructor body does nothing except calling super().
src
)

Overrides javassist.CtBehavior.setBody.

Sets a constructor body.

public void
setBody(CtConstructor
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 constructor body from another constructor.

public CtMethod
toMethod(String
the name of the resulting method.
name
,
CtClass
the class declaring the resulting method.
declaring
)

Makes a copy of this constructor and converts it into a method.

public CtMethod
toMethod(String
the name of the resulting method.
name
,
CtClass
the class declaring the resulting method. It is normally the same as the class declaring this constructor.
declaring
,
ClassMap
the hash table associating original class names with substituted names. The original class names will be replaced while making a copy. map can be null.
map
)

Makes a copy of this constructor and converts it into a method.

Inherited from javassist.CtBehavior:
addCatchaddCatchaddLocalVariableaddParametercopyextendToStringgetAnnotationgetAnnotationsgetAttributegetAvailableAnnotationsgetAvailableParameterAnnotationsgetExceptionTypesgetGenericSignaturegetMethodInfogetMethodInfo2getModifiersgetParameterAnnotationsgetParameterAnnotationsgetParameterTypesgetReturnType0getSignaturehasAnnotationinsertAfterinsertAfterinsertAfterinsertAtinsertAtinsertBeforeinsertParameterinstrumentinstrumentsetAttributesetBodysetBody0setExceptionTypessetGenericSignaturesetModifiersuseCflow

Constructor Detail

CtConstructorback to summary
protected CtConstructor(MethodInfo minfo, CtClass declaring)
CtConstructorback to summary
public CtConstructor(CtClass[] parameters, CtClass declaring)

Creates a constructor with no constructor body. The created constructor must be added to a class with CtClass.addConstructor().

The created constructor does not include a constructor body, which must be specified with setBody().

Parameters
parameters:CtClass[]

a list of the parameter types

declaring:CtClass

the class to which the created method is added.

See Also
CtClass#addConstructor(CtConstructor), CtConstructor#setBody(String), CtConstructor#setBody(CtConstructor, ClassMap)
CtConstructorback to summary
public CtConstructor(CtConstructor src, CtClass declaring, ClassMap map) throws CannotCompileException

Creates a copy of a CtConstructor object. The created constructor must be added to a class with CtClass.addConstructor().

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

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

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#addConstructor(CtConstructor), ClassMap#fix(String)

Method Detail

callsSuperback to summary
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 by this().

getLongNameback to summary
public String getLongName()

Implements abstract javassist.CtBehavior.getLongName.

Returns the constructor name followed by parameter types such as javassist.CtConstructor(CtClass[],CtClass).

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

Implements abstract javassist.CtMember.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>".

Annotations
@Override
getStartPosOfBodyback to summary
pack-priv int getStartPosOfBody(CodeAttribute ca) throws CannotCompileException

Overrides javassist.CtBehavior.getStartPosOfBody.

Annotations
@Override
getSuperclassNameback to summary
private String getSuperclassName()
insertBeforeBodyback to summary
public void insertBeforeBody(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:String

the source code representing the inserted bytecode. It must be a single statement or block.

isClassInitializerback to summary
public boolean isClassInitializer()

Returns true if this object represents a static initializer.

isConstructorback to summary
public boolean isConstructor()

Returns true if this object represents a constructor.

isEmptyback to summary
public boolean isEmpty()

Implements abstract javassist.CtBehavior.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 calling super() (the no-argument constructor of the super class).

Annotations
@Override
removeConsCallback to summary
private static void removeConsCall(CodeAttribute ca) throws CannotCompileException
setBodyback to summary
public void setBody(String src) throws CannotCompileException

Overrides javassist.CtBehavior.setBody.

Sets a constructor body.

Parameters
src:String

the source code representing the constructor body. It must be a single statement or block. If it is null, the substituted constructor body does nothing except calling super().

Annotations
@Override
setBodyback to summary
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 if map is not null.

Parameters
src:CtConstructor

the method that the body is copied from.

map:ClassMap

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

toMethodback to summary
public CtMethod toMethod(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 is void. The resulting method must be appended to the class specified by declaring. If this constructor is a static initializer, the resulting method takes no parameter.

An occurrence of another constructor call this() or a super constructor call super() 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:String

the name of the resulting method.

declaring:CtClass

the class declaring the resulting method.

toMethodback to summary
public CtMethod toMethod(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 is void. The resulting method must be appended to the class specified by declaring. If this constructor is a static initializer, the resulting method takes no parameter.

An occurrence of another constructor call this() or a super constructor call super() 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:String

the name of the resulting method.

declaring:CtClass

the class declaring the resulting method. It is normally the same as the class declaring this constructor.

map:ClassMap

the hash table associating original class names with substituted names. The original class names will be replaced while making a copy. map can be null.