javassist
Class CtConstructor

java.lang.Object
  extended byjavassist.CtMember
      extended byjavassist.CtBehavior
          extended byjavassist.CtConstructor

public final class CtConstructor
extends CtBehavior

An instance of CtConstructor represents a constructor. It may represent a static constructor (class initializer). To distinguish a constructor and a class initializer, call isClassInitializer().

See the super class CtBehavior as well since a number of useful methods are in CtBehavior.

See Also:
CtClass.getDeclaredConstructors(), CtClass.getClassInitializer(), CtNewConstructor

Constructor Summary
CtConstructor(CtClass[] parameters, CtClass declaring)
          Creates a constructor with no constructor body.
CtConstructor(CtConstructor src, CtClass declaring, ClassMap map)
          Creates a copy of a CtConstructor object.
 
Method Summary
 java.lang.String getName()
          Obtains the name of this constructor.
 void insertBeforeBody(java.lang.String src)
          Inserts bytecode just after another constructor in the super class or this class is called.
 boolean isClassInitializer()
          Returns true if this object represents a static initializer.
 boolean isConstructor()
          Returns true if this object represents a constructor.
 boolean isEmpty()
          Returns true if the constructor is the default one.
 void setBody(CtConstructor src, ClassMap map)
          Copies a constructor body from another constructor.
 void setBody(java.lang.String src)
          Sets a constructor body.
 
Methods inherited from class javassist.CtBehavior
addCatch, addCatch, addLocalVariable, getAttribute, getExceptionTypes, getMethodInfo, getMethodInfo2, getModifiers, getParameterTypes, getSignature, insertAfter, insertAfter, insertAt, insertAt, insertBefore, instrument, instrument, setAttribute, setBody, setExceptionTypes, setModifiers, useCflow
 
Methods inherited from class javassist.CtMember
getDeclaringClass, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CtConstructor

public CtConstructor(CtClass[] parameters,
                     CtClass declaring)
Creates a constructor with no constructor body. The created constructor must be added to a class with CtClass.addConstructor().

The created constructor does not include a constructor body, must be specified with setBody().

Parameters:
declaring - the class to which the created method is added.
parameters - a list of the parameter types
See Also:
CtClass.addConstructor(CtConstructor), setBody(String), setBody(CtConstructor,ClassMap)

CtConstructor

public CtConstructor(CtConstructor src,
                     CtClass declaring,
                     ClassMap map)
              throws CannotCompileException
Creates a copy of a CtConstructor object. The created constructor must be added to a class with CtClass.addConstructor().

All occurrences of class names in the created constructor are replaced with names specified by map if map is not null.

By default, all the occurrences of the names of the class declaring src and the superclass are replaced with the name of the class and the superclass that the created constructor is added to. This is done whichever map is null or not. To prevent this replacement, call ClassMap.fix().

Note: if the .class notation (for example, String.class) is included in an expression, the Javac compiler may produce a helper method. Since this constructor never copies this helper method, the programmers have the responsiblity of copying it. Otherwise, use Class.forName() in the expression.

Parameters:
src - the source method.
declaring - the class to which the created method is added.
map - the hashtable associating original class names with substituted names. It can be null.
See Also:
CtClass.addConstructor(CtConstructor), ClassMap.fix(String)
Method Detail

isConstructor

public boolean isConstructor()
Returns true if this object represents a constructor.


isClassInitializer

public boolean isClassInitializer()
Returns true if this object represents a static initializer.


getName

public java.lang.String getName()
Obtains the name of this constructor. It is the same as the simple name of the class declaring this constructor. If this object represents a class initializer, then this method returns "<clinit>".

Specified by:
getName in class CtMember
See Also:
getName()

isEmpty

public boolean isEmpty()
Returns true if the constructor is the default one.

Specified by:
isEmpty in class CtBehavior

setBody

public void setBody(java.lang.String src)
             throws CannotCompileException
Sets a constructor body.

Overrides:
setBody in class CtBehavior
Parameters:
src - the source code representing the constructor body. It must be a single statement or block. If it is null, the substituted constructor body does nothing except calling super().
Throws:
CannotCompileException

setBody

public void setBody(CtConstructor src,
                    ClassMap map)
             throws CannotCompileException
Copies a constructor body from another constructor.

All occurrences of the class names in the copied body are replaced with the names specified by map if map is not null.

Parameters:
src - the method that the body is copied from.
map - the hashtable associating original class names with substituted names. It can be null.
Throws:
CannotCompileException

insertBeforeBody

public void insertBeforeBody(java.lang.String src)
                      throws CannotCompileException
Inserts bytecode just after another constructor in the super class or this class is called. It does not work if this object represents a class initializer.

Parameters:
src - the source code representing the inserted bytecode. It must be a single statement or block.
Throws:
CannotCompileException


Javassist, a Java-bytecode translator toolkit. Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.