classUtils.javassist
Class CtNewConstructor

java.lang.Object
  extended by classUtils.javassist.CtNewConstructor

public class CtNewConstructor
extends java.lang.Object

A collection of static methods for creating a CtConstructor. An instance of this class does not make any sense.

See Also:
CompileTimeClass.addConstructor(CtConstructor)

Field Summary
static int PASS_ARRAY
          Specifies that parameters are converted into an array of Object and passed to a super-class' constructor.
static int PASS_NONE
          Specifies that no parameters are passed to a super-class' constructor.
static int PASS_PARAMS
          Specifies that parameters are passed as is to a super-class' constructor.
 
Constructor Summary
CtNewConstructor()
           
 
Method Summary
static CtConstructor copy(CtConstructor c, CompileTimeClass declaring, ClassMap map)
          Creats a copy of a constructor.
static CtConstructor defaultConstructor(CompileTimeClass declaring)
          Creates a default (public) constructor.
static CtConstructor make(CompileTimeClass[] parameters, CompileTimeClass[] exceptions, CompileTimeClass declaring)
          Creates a public constructor that only calls a constructor in the super class.
static CtConstructor make(CompileTimeClass[] parameters, CompileTimeClass[] exceptions, int howto, CtMethod body, CtMethod.ConstParameter cparam, CompileTimeClass declaring)
          Creates a public constructor.
static CtConstructor make(CompileTimeClass[] parameters, CompileTimeClass[] exceptions, java.lang.String body, CompileTimeClass declaring)
          Creates a public constructor.
static CtConstructor make(java.lang.String src, CompileTimeClass declaring)
          Compiles the given source code and creates a constructor.
static CtConstructor skeleton(CompileTimeClass[] parameters, CompileTimeClass[] exceptions, CompileTimeClass declaring)
          Creates a public constructor that only calls a constructor in the super class.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PASS_NONE

public static final int PASS_NONE
Specifies that no parameters are passed to a super-class' constructor. That is, the default constructor is invoked.

See Also:
Constant Field Values

PASS_ARRAY

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

See Also:
Constant Field Values

PASS_PARAMS

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.

See Also:
Constant Field Values
Constructor Detail

CtNewConstructor

public CtNewConstructor()
Method Detail

make

public static CtConstructor make(java.lang.String src,
                                 CompileTimeClass 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 - the source text.
declaring - the class to which the created constructor is added.
Throws:
CannotCompileException

make

public static CtConstructor make(CompileTimeClass[] parameters,
                                 CompileTimeClass[] exceptions,
                                 java.lang.String body,
                                 CompileTimeClass declaring)
                          throws CannotCompileException
Creates a public constructor.

Parameters:
returnType - the type of the returned value.
mname - the method name.
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 is null, the substituted constructor body does nothing except calling super().
declaring - the class to which the created method is added.
Throws:
CannotCompileException

copy

public static CtConstructor copy(CtConstructor c,
                                 CompileTimeClass declaring,
                                 ClassMap map)
                          throws CannotCompileException
Creats a copy of a constructor.

Parameters:
c - the copied constructor.
declaring - the class to which the created method is added.
map - the hashtable associating original class names with substituted names. It can be null.
Throws:
CannotCompileException
See Also:
CtConstructor.CtConstructor(CtConstructor,CompileTimeClass,ClassMap)

defaultConstructor

public static CtConstructor defaultConstructor(CompileTimeClass declaring)
                                        throws CannotCompileException
Creates a default (public) constructor.

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

Throws:
CannotCompileException

skeleton

public static CtConstructor skeleton(CompileTimeClass[] parameters,
                                     CompileTimeClass[] exceptions,
                                     CompileTimeClass 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 - parameter types
exceptions - exception types
declaring - the class to which the created constructor is added.
Throws:
CannotCompileException
See Also:
CtField.Initializer.byParameter(int)

make

public static CtConstructor make(CompileTimeClass[] parameters,
                                 CompileTimeClass[] exceptions,
                                 CompileTimeClass 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 - parameter types
exceptions - exception types
declaring - the class to which the created constructor is added.
Throws:
CannotCompileException

make

public static CtConstructor make(CompileTimeClass[] parameters,
                                 CompileTimeClass[] exceptions,
                                 int howto,
                                 CtMethod body,
                                 CtMethod.ConstParameter cparam,
                                 CompileTimeClass 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:

Here, cvalue is the constant value specified by cparam.

If cparam is null, the signature must be:

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:

If cparam is null, the signature must be:

Although the type of the returned value is Object, the value must be always null.

Example:

where the class Sample is as follows:

This program produces the following class:

Parameters:
parameters - a list of the parameter types
exceptions - a list of the exceptions
howto - how to pass parameters to the super-class' constructor (PASS_NONE, PASS_ARRAY, or PASS_PARAMS)
body - appended body (may be null). It must be not a constructor but a method.
cparam - constant parameter (may be null.)
declaring - the class to which the created constructor is added.
Throws:
CannotCompileException
See Also:
CtNewMethod.wrapped(CompileTimeClass,String,CompileTimeClass[],CompileTimeClass[],CtMethod,CtMethod.ConstParameter,CompileTimeClass)