KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > CtNewConstructor


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist;
17
18 import javassist.bytecode.*;
19 import javassist.compiler.Javac;
20 import javassist.compiler.CompileError;
21 import javassist.CtMethod.ConstParameter;
22
23 /**
24  * A collection of static methods for creating a <code>CtConstructor</code>.
25  * An instance of this class does not make any sense.
26  *
27  * <p>A class initializer (static constructor) cannot be created by the
28  * methods in this class. Call <code>makeClassInitializer()</code> in
29  * <code>CtClass</code> and append code snippet to the body of the class
30  * initializer obtained by <code>makeClassInitializer()</code>.
31  *
32  * @see CtClass#addConstructor(CtConstructor)
33  */

34 public class CtNewConstructor {
35     /**
36      * Specifies that no parameters are passed to a super-class'
37      * constructor. That is, the default constructor is invoked.
38      */

39     public static final int PASS_NONE = 0; // call super()
40

41     /**
42      * Specifies that parameters are converted into an array of
43      * <code>Object</code> and passed to a super-class'
44      * constructor.
45      */

46     public static final int PASS_ARRAY = 1; // an array of parameters
47

48     /**
49      * Specifies that parameters are passed <i>as is</i>
50      * to a super-class' constructor. The signature of that
51      * constructor must be the same as that of the created constructor.
52      */

53     public static final int PASS_PARAMS = 2;
54
55     /**
56      * Compiles the given source code and creates a constructor.
57      * The source code must include not only the constructor body
58      * but the whole declaration.
59      *
60      * @param src the source text.
61      * @param declaring the class to which the created constructor is added.
62      */

63     public static CtConstructor make(String JavaDoc src, CtClass declaring)
64         throws CannotCompileException
65     {
66         Javac compiler = new Javac(declaring);
67         try {
68             CtMember obj = compiler.compile(src);
69             if (obj instanceof CtConstructor)
70                 return (CtConstructor)obj;
71         }
72         catch (CompileError e) {
73             throw new CannotCompileException(e);
74         }
75
76         throw new CannotCompileException("not a constructor");
77     }
78
79     /**
80      * Creates a public constructor.
81      *
82      * @param parameters a list of the parameter types.
83      * @param exceptions a list of the exception types.
84      * @param body the source text of the constructor body.
85      * It must be a block surrounded by <code>{}</code>.
86      * If it is <code>null</code>, the substituted
87      * constructor body does nothing except calling
88      * <code>super()</code>.
89      * @param declaring the class to which the created method is added.
90      */

91     public static CtConstructor make(CtClass[] parameters,
92                                      CtClass[] exceptions,
93                                      String JavaDoc body, CtClass declaring)
94         throws CannotCompileException
95     {
96         try {
97             CtConstructor cc = new CtConstructor(parameters, declaring);
98             cc.setExceptionTypes(exceptions);
99             cc.setBody(body);
100             return cc;
101         }
102         catch (NotFoundException e) {
103             throw new CannotCompileException(e);
104         }
105     }
106
107     /**
108      * Creats a copy of a constructor.
109      *
110      * @param c the copied constructor.
111      * @param declaring the class to which the created method is added.
112      * @param map the hashtable associating original class names
113      * with substituted names.
114      * It can be <code>null</code>.
115      *
116      * @see CtConstructor#CtConstructor(CtConstructor,CtClass,ClassMap)
117      */

118     public static CtConstructor copy(CtConstructor c, CtClass declaring,
119                                 ClassMap map) throws CannotCompileException {
120         return new CtConstructor(c, declaring, map);
121     }
122
123     /**
124      * Creates a default (public) constructor.
125      *
126      * <p>The created constructor takes no parameter. It calls
127      * <code>super()</code>.
128      */

129     public static CtConstructor defaultConstructor(CtClass declaring)
130         throws CannotCompileException
131     {
132         CtConstructor cons = new CtConstructor((CtClass[])null, declaring);
133
134         ConstPool cp = declaring.getClassFile2().getConstPool();
135         Bytecode code = new Bytecode(cp, 1, 1);
136         code.addAload(0);
137         try {
138             code.addInvokespecial(declaring.getSuperclass(),
139                                   "<init>", "()V");
140         }
141         catch (NotFoundException e) {
142             throw new CannotCompileException(e);
143         }
144
145         code.add(Bytecode.RETURN);
146
147         cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute());
148         return cons;
149     }
150
151     /**
152      * Creates a public constructor that only calls a constructor
153      * in the super class. The created constructor receives parameters
154      * specified by <code>parameters</code> but calls the super's
155      * constructor without those parameters (that is, it calls the default
156      * constructor).
157      *
158      * <p>The parameters passed to the created constructor should be
159      * used for field initialization. <code>CtField.Initializer</code>
160      * objects implicitly insert initialization code in constructor
161      * bodies.
162      *
163      * @param parameters parameter types
164      * @param exceptions exception types
165      * @param declaring the class to which the created constructor
166      * is added.
167      * @see CtField.Initializer#byParameter(int)
168      */

169     public static CtConstructor skeleton(CtClass[] parameters,
170                         CtClass[] exceptions, CtClass declaring)
171         throws CannotCompileException
172     {
173         return make(parameters, exceptions, PASS_NONE,
174                     null, null, declaring);
175     }
176
177     /**
178      * Creates a public constructor that only calls a constructor
179      * in the super class. The created constructor receives parameters
180      * specified by <code>parameters</code> and calls the super's
181      * constructor with those parameters.
182      *
183      * @param parameters parameter types
184      * @param exceptions exception types
185      * @param declaring the class to which the created constructor
186      * is added.
187      */

188     public static CtConstructor make(CtClass[] parameters,
189                                      CtClass[] exceptions, CtClass declaring)
190         throws CannotCompileException
191     {
192         return make(parameters, exceptions, PASS_PARAMS,
193                     null, null, declaring);
194     }
195
196     /**
197      * Creates a public constructor.
198      *
199      * <p>If <code>howto</code> is <code>PASS_PARAMS</code>,
200      * the created constructor calls the super's constructor with the
201      * same signature. The superclass must contain
202      * a constructor taking the same set of parameters as the created one.
203      *
204      * <p>If <code>howto</code> is <code>PASS_NONE</code>,
205      * the created constructor calls the super's default constructor.
206      * The superclass must contain a constructor taking no parameters.
207      *
208      * <p>If <code>howto</code> is <code>PASS_ARRAY</code>,
209      * the created constructor calls the super's constructor
210      * with the given parameters in the form of an array of
211      * <code>Object</code>. The signature of the super's constructor
212      * must be:
213      *
214      * <ul><code>constructor(Object[] params, &lt;type&gt; cvalue)
215      * </code></ul>
216      *
217      * <p>Here, <code>cvalue</code> is the constant value specified
218      * by <code>cparam</code>.
219      *
220      * <p>If <code>cparam</code> is <code>null</code>, the signature
221      * must be:
222      *
223      * <ul><code>constructor(Object[] params)</code></ul>
224      *
225      * <p>If <code>body</code> is not null, a copy of that method is
226      * embedded in the body of the created constructor.
227      * The embedded method is executed after
228      * the super's constructor is called and the values of fields are
229      * initialized. Note that <code>body</code> must not
230      * be a constructor but a method.
231      *
232      * <p>Since the embedded method is wrapped
233      * in parameter-conversion code
234      * as in <code>CtNewMethod.wrapped()</code>,
235      * the constructor parameters are
236      * passed in the form of an array of <code>Object</code>.
237      * The method specified by <code>body</code> must have the
238      * signature shown below:
239      *
240      * <ul><code>Object method(Object[] params, &lt;type&gt; cvalue)
241      * </code></ul>
242      *
243      * <p>If <code>cparam</code> is <code>null</code>, the signature
244      * must be:
245      *
246      * <ul><code>Object method(Object[] params)</code></ul>
247      *
248      * <p>Although the type of the returned value is <code>Object</code>,
249      * the value must be always <code>null</code>.
250      *
251      * <p><i>Example:</i>
252      *
253      * <ul><pre>ClassPool pool = ... ;
254      * CtClass xclass = pool.makeClass("X");
255      * CtMethod method = pool.getMethod("Sample", "m");
256      * xclass.setSuperclass(pool.get("Y"));
257      * CtClass[] argTypes = { CtClass.intType };
258      * ConstParameter cparam = ConstParameter.string("test");
259      * CtConstructor c = CtNewConstructor.make(argTypes, null,
260      * PASS_PARAMS, method, cparam, xclass);
261      * xclass.addConstructor(c);</pre></ul>
262      *
263      * <p>where the class <code>Sample</code> is as follows:
264      *
265      * <ul><pre>public class Sample {
266      * public Object m(Object[] args, String msg) {
267      * System.out.println(msg);
268      * return null;
269      * }
270      * }</pre></ul>
271      *
272      * <p>This program produces the following class:
273      *
274      * <ul><pre>public class X extends Y {
275      * public X(int p0) {
276      * super(p0);
277      * String msg = "test";
278      * Object[] args = new Object[] { p0 };
279      * // begin of copied body
280      * System.out.println(msg);
281      * Object result = null;
282      * // end
283      * }
284      * }</pre></ul>
285      *
286      * @param parameters a list of the parameter types
287      * @param exceptions a list of the exceptions
288      * @param howto how to pass parameters to the super-class'
289      * constructor (<code>PASS_NONE</code>,
290      * <code>PASS_ARRAY</code>,
291      * or <code>PASS_PARAMS</code>)
292      * @param body appended body (may be <code>null</code>).
293      * It must be not a constructor but a method.
294      * @param cparam constant parameter (may be <code>null</code>.)
295      * @param declaring the class to which the created constructor
296      * is added.
297      *
298      * @see CtNewMethod#wrapped(CtClass,String,CtClass[],CtClass[],CtMethod,CtMethod.ConstParameter,CtClass)
299      */

300     public static CtConstructor make(CtClass[] parameters,
301                                      CtClass[] exceptions, int howto,
302                                      CtMethod body, ConstParameter cparam,
303                                      CtClass declaring)
304         throws CannotCompileException
305     {
306         return CtNewWrappedConstructor.wrapped(parameters, exceptions,
307                                         howto, body, cparam, declaring);
308     }
309 }
310
Popular Tags