Top Description Fields Constructors Methods
javassist

public Class CtNewConstructor

extends Object
Class Inheritance
Imports
javassist.CtMethod.ConstParameter, javassist.bytecode.Bytecode, .ConstPool, javassist.compiler.CompileError, .Javac

A collection of static methods for creating a 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().

See Also
CtClass#addConstructor(CtConstructor), CtClass#makeClassInitializer()

Field Summary

Modifier and TypeField and Description
public static final int
PASS_ARRAY

Specifies that parameters are converted into an array of Object and passed to a super-class' constructor.

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.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod 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 null.
map
)

Creates a copy of a constructor.

public static CtConstructor
defaultConstructor(CtClass declaring)

Creates a default (public) constructor.

public static CtConstructor
make(String
the source text.
src
,
CtClass
the class to which the created constructor is added.
declaring
)

Compiles the given source code and creates a constructor.

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 {}. If it is null, the substituted constructor body does nothing except calling super().
body
,
CtClass
the class to which the created method is added.
declaring
)

Creates a public constructor.

public static CtConstructor
make(CtClass[]
parameter types
parameters
,
CtClass[]
exception types
exceptions
,
CtClass
the class to which the created constructor is added.
declaring
)

Creates a public constructor that only calls a constructor in the super class.

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 (PASS_NONE, PASS_ARRAY, or PASS_PARAMS)
howto
,
CtMethod
appended body (may be null). It must be not a constructor but a method.
body
,
CtMethod.ConstParameter
constant parameter (may be null.)
cparam
,
CtClass
the class to which the created constructor is added.
declaring
)

Creates a public constructor.

public static CtConstructor
skeleton(CtClass[]
parameter types
parameters
,
CtClass[]
exception types
exceptions
,
CtClass
the class to which the created constructor is added.
declaring
)

Creates a public constructor that only calls a constructor in the super class.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

PASS_ARRAYback to summary
public static final int PASS_ARRAY

Specifies that parameters are converted into an array of Object and passed to a super-class' constructor.

PASS_NONEback 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_PARAMSback 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.

Constructor Detail

CtNewConstructorback to summary
public CtNewConstructor()

Method Detail

copyback 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 this constructor. See the description of the constructor for particular behavior of the copying.

Parameters
c:CtConstructor

the copied constructor.

declaring:CtClass

the class to which the created method is added.

map:ClassMap

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

See Also
CtConstructor#CtConstructor(CtConstructor, CtClass, ClassMap)
defaultConstructorback to summary
public static CtConstructor defaultConstructor(CtClass declaring) throws CannotCompileException

Creates a default (public) constructor.

The created constructor takes no parameter. It calls super().

makeback 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.

Parameters
src:String

the source text.

declaring:CtClass

the class to which the created constructor is added.

makeback to summary
public static CtConstructor make(CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) throws CannotCompileException

Creates a public constructor.

Parameters
parameters:CtClass[]

a list of the parameter types.

exceptions:CtClass[]

a list of the exception types.

body:String

the source text of the constructor body. It must be a block surrounded by {}. If it is null, the substituted constructor body does nothing except calling super().

declaring:CtClass

the class to which the created method is added.

makeback 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 parameters and calls the super's constructor with those parameters.

Parameters
parameters:CtClass[]

parameter types

exceptions:CtClass[]

exception types

declaring:CtClass

the class to which the created constructor is added.

makeback to summary
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 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:CtClass[]

a list of the parameter types

exceptions:CtClass[]

a list of the exceptions

howto:int

how to pass parameters to the super-class' constructor (PASS_NONE, PASS_ARRAY, or PASS_PARAMS)

body:CtMethod

appended body (may be null). It must be not a constructor but a method.

cparam:CtMethod.ConstParameter

constant parameter (may be null.)

declaring:CtClass

the class to which the created constructor is added.

See Also
CtNewMethod#wrapped(CtClass, String, CtClass[], CtClass[], CtMethod, CtMethod.ConstParameter, CtClass)
skeletonback 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 parameters 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:CtClass[]

parameter types

exceptions:CtClass[]

exception types

declaring:CtClass

the class to which the created constructor is added.

See Also
CtField.Initializer#byParameter(int)