Class CtNewConstructor
CtConstructor
.
An instance of this class does not make any sense.
A class initializer (static constructor) cannot be created by the
methods in this class. Call makeClassInitializer()
in
CtClass
and append code snippet to the body of the class
initializer obtained by makeClassInitializer()
.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Specifies that parameters are converted into an array ofObject
and passed to a super-class' constructor.static final int
Specifies that no parameters are passed to a super-class' constructor.static final int
Specifies that parameters are passed as is to a super-class' constructor. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic CtConstructor
copy
(CtConstructor c, CtClass declaring, ClassMap map) Creates a copy of a constructor.static CtConstructor
defaultConstructor
(CtClass declaring) Creates a default (public) constructor.static CtConstructor
Compiles the given source code and creates a constructor.static CtConstructor
make
(CtClass[] parameters, CtClass[] exceptions, int howto, CtMethod body, CtMethod.ConstParameter cparam, CtClass declaring) Creates a public constructor.static CtConstructor
Creates a public constructor.static CtConstructor
Creates a public constructor that only calls a constructor in the super class.static CtConstructor
Creates a public constructor that only calls a constructor in the super class.
-
Field Details
-
PASS_NONE
public static final int PASS_NONESpecifies that no parameters are passed to a super-class' constructor. That is, the default constructor is invoked.- See Also:
-
PASS_ARRAY
public static final int PASS_ARRAYSpecifies that parameters are converted into an array ofObject
and passed to a super-class' constructor.- See Also:
-
PASS_PARAMS
public static final int PASS_PARAMSSpecifies that parameters are passed as is to a super-class' constructor. The signature of that constructor must be the same as that of the created constructor.- See Also:
-
-
Constructor Details
-
CtNewConstructor
public CtNewConstructor()
-
-
Method Details
-
make
Compiles the given source code and creates a constructor. The source code must include not only the constructor body but the whole declaration.- Parameters:
src
- the source text.declaring
- the class to which the created constructor is added.- Throws:
CannotCompileException
-
make
public static CtConstructor make(CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) throws CannotCompileException Creates a public constructor.- Parameters:
parameters
- a list of the parameter types.exceptions
- a list of the exception types.body
- the source text of the constructor body. It must be a block surrounded by{}
. If it isnull
, the substituted constructor body does nothing except callingsuper()
.declaring
- the class to which the created method is added.- Throws:
CannotCompileException
-
copy
public static CtConstructor copy(CtConstructor c, CtClass declaring, ClassMap map) throws CannotCompileException Creates a copy of a constructor. This is a convenience method for callingthis constructor
. See the description of the constructor for particular behavior of the copying.- Parameters:
c
- the copied constructor.declaring
- the class to which the created method is added.map
- the hash table associating original class names with substituted names. It can benull
.- Throws:
CannotCompileException
- See Also:
-
defaultConstructor
Creates a default (public) constructor.The created constructor takes no parameter. It calls
super()
.- Throws:
CannotCompileException
-
skeleton
public static CtConstructor skeleton(CtClass[] parameters, CtClass[] exceptions, CtClass declaring) throws CannotCompileException Creates a public constructor that only calls a constructor in the super class. The created constructor receives parameters specified byparameters
but calls the super's constructor without those parameters (that is, it calls the default constructor).The parameters passed to the created constructor should be used for field initialization.
CtField.Initializer
objects implicitly insert initialization code in constructor bodies.- Parameters:
parameters
- parameter typesexceptions
- exception typesdeclaring
- the class to which the created constructor is added.- Throws:
CannotCompileException
- See Also:
-
make
public static CtConstructor make(CtClass[] parameters, CtClass[] exceptions, CtClass declaring) throws CannotCompileException Creates a public constructor that only calls a constructor in the super class. The created constructor receives parameters specified byparameters
and calls the super's constructor with those parameters.- Parameters:
parameters
- parameter typesexceptions
- exception typesdeclaring
- the class to which the created constructor is added.- Throws:
CannotCompileException
-
make
public static CtConstructor make(CtClass[] parameters, CtClass[] exceptions, int howto, CtMethod body, CtMethod.ConstParameter cparam, CtClass declaring) throws CannotCompileException Creates a public constructor.If
howto
isPASS_PARAMS
, the created constructor calls the super's constructor with the same signature. The superclass must contain a constructor taking the same set of parameters as the created one.If
howto
isPASS_NONE
, the created constructor calls the super's default constructor. The superclass must contain a constructor taking no parameters.If
howto
isPASS_ARRAY
, the created constructor calls the super's constructor with the given parameters in the form of an array ofObject
. The signature of the super's constructor must be:constructor(Object[] params, <type> cvalue)
Here,
cvalue
is the constant value specified bycparam
.If
cparam
isnull
, the signature must be:constructor(Object[] params)
If
body
is not null, a copy of that method is embedded in the body of the created constructor. The embedded method is executed after the super's constructor is called and the values of fields are initialized. Note thatbody
must not be a constructor but a method.Since the embedded method is wrapped in parameter-conversion code as in
CtNewMethod.wrapped()
, the constructor parameters are passed in the form of an array ofObject
. The method specified bybody
must have the signature shown below:Object method(Object[] params, <type> cvalue)
If
cparam
isnull
, the signature must be:Object method(Object[] params)
Although the type of the returned value is
Object
, the value must be alwaysnull
.Example:
ClassPool pool = ... ; CtClass xclass = pool.makeClass("X"); CtMethod method = pool.getMethod("Sample", "m"); xclass.setSuperclass(pool.get("Y")); CtClass[] argTypes = { CtClass.intType }; ConstParameter cparam = ConstParameter.string("test"); CtConstructor c = CtNewConstructor.make(argTypes, null, PASS_PARAMS, method, cparam, xclass); xclass.addConstructor(c);
where the class
Sample
is as follows:public class Sample { public Object m(Object[] args, String msg) { System.out.println(msg); return null; } }
This program produces the following class:
public class X extends Y { public X(int p0) { super(p0); String msg = "test"; Object[] args = new Object[] { p0 }; // begin of copied body System.out.println(msg); Object result = null; // end } }
- Parameters:
parameters
- a list of the parameter typesexceptions
- a list of the exceptionshowto
- how to pass parameters to the super-class' constructor (PASS_NONE
,PASS_ARRAY
, orPASS_PARAMS
)body
- appended body (may benull
). It must be not a constructor but a method.cparam
- constant parameter (may benull
.)declaring
- the class to which the created constructor is added.- Throws:
CannotCompileException
- See Also:
-