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().
CtClass#addConstructor(CtConstructor), CtClass#makeClassInitializer()
| Modifier and Type | Field and Description |
|---|---|
| public static final int | PASS_ARRAY
Specifies that parameters are converted into an array of
|
| public static final int | PASS_NONE
Specifies that no parameters are passed to a super-class' constructor. |
| public static final int | PASS_PARAMS
Specifies that parameters are passed as is to a super-class' constructor. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static CtConstructor | copy(CtConstructor
the copied constructor. c, CtClass the class to which the created method is added. declaring, ClassMap the hash table associating original class names
with substituted names.
It can be map)null.Creates a copy of a constructor. |
| public static CtConstructor | |
| public static CtConstructor | |
| public static CtConstructor | make(CtClass[]
a list of the parameter types. parameters, CtClass[] a list of the exception types. exceptions, String the source text of the constructor body.
It must be a block surrounded by body, CtClass {}.
If it is null, the substituted
constructor body does nothing except calling
super().the class to which the created method is added. declaring)Creates a public constructor. |
| public static CtConstructor | |
| public static CtConstructor | make(CtClass[]
a list of the parameter types parameters, CtClass[] a list of the exceptions exceptions, int how to pass parameters to the super-class'
constructor ( howto, CtMethod PASS_NONE,
PASS_ARRAY,
or PASS_PARAMS)appended body (may be body, CtMethod.null).
It must be not a constructor but a method.constant parameter (may be cparam,null.)the class to which the created constructor
is added. declaring)Creates a public constructor. |
| public static CtConstructor |
| PASS_ARRAY | back to summary |
|---|---|
| public static final int PASS_ARRAY Specifies that parameters are converted into an array of
| |
| PASS_NONE | back to summary |
|---|---|
| public static final int PASS_NONE Specifies that no parameters are passed to a super-class' constructor. That is, the default constructor is invoked. | |
| PASS_PARAMS | back to summary |
|---|---|
| public static final int PASS_PARAMS Specifies 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. | |
| CtNewConstructor | back to summary |
|---|---|
| public CtNewConstructor() | |
| copy | back to summary |
|---|---|
| public static CtConstructor copy(CtConstructor c, CtClass declaring, ClassMap map) throws CannotCompileException Creates a copy of a constructor.
This is a convenience method for calling
| |
| defaultConstructor | back to summary |
|---|---|
| public static CtConstructor defaultConstructor(CtClass declaring) throws CannotCompileException Creates a default (public) constructor. The created constructor takes no parameter. It calls
| |
| make | back to summary |
|---|---|
| public static CtConstructor make(String src, CtClass declaring) throws CannotCompileException Compiles the given source code and creates a constructor. The source code must include not only the constructor body but the whole declaration. | |
| make | back to summary |
|---|---|
| public static CtConstructor make(CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) throws CannotCompileException Creates a public constructor.
| |
| make | back to summary |
|---|---|
| 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 by | |
| make | back to summary |
|---|---|
| public static CtConstructor make(CtClass[] parameters, CtClass[] exceptions, int howto, CtMethod body, CtMethod. Creates a public constructor. If If If constructor(Object[] params, <type> cvalue) Here, If constructor(Object[] params) If Since the embedded method is wrapped
in parameter-conversion code
as in Object method(Object[] params, <type> cvalue) If Object method(Object[] params) Although the type of the returned value is 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
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
}
}
| |
| skeleton | back to summary |
|---|---|
| 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 by The parameters passed to the created constructor should be
used for field initialization.
| |