Creates a public constructor.
If howto is PASS_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 is PASS_NONE ,
the created constructor calls the super's default constructor.
The superclass must contain a constructor taking no parameters.
If howto is PASS_ARRAY ,
the created constructor calls the super's constructor
with the given parameters in the form of an array of
Object . The signature of the super's constructor
must be:
constructor(Object[] params, <type> cvalue)
Here, cvalue is the constant value specified
by cparam .
If cparam is null , 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 that body 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 of Object .
The method specified by body must have the
signature shown below:
Object method(Object[] params, <type> cvalue)
If cparam is null , the signature
must be:
Object method(Object[] params)
Although the type of the returned value is Object ,
the value must be always null .
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 types Parameters: exceptions - a list of the exceptions Parameters: howto - how to pass parameters to the super-class'constructor (PASS_NONE ,PASS_ARRAY ,or PASS_PARAMS ) Parameters: body - appended body (may be null ).It must be not a constructor but a method. Parameters: cparam - constant parameter (may be null .) Parameters: declaring - the class to which the created constructoris added. See Also: CtNewMethod.wrapped(CtClassStringCtClass[]CtClass[]CtMethodCtMethod.ConstParameterCtClass) |