1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. Alternatively, the contents of this file may be used under
* the terms of the GNU Lesser General Public License Version 2.1 or later,
* or the Apache License Version 2.0.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/
package javassist;
import javassist.CtMethod.ConstParameter;
import javassist.bytecode.Bytecode;
import javassist.bytecode.ConstPool;
import javassist.compiler.CompileError;
import javassist.compiler.Javac;
/**
* A collection of static methods for creating a <code>CtConstructor</code>.
* An instance of this class does not make any sense.
*
* <p>A class initializer (static constructor) cannot be created by the
* methods in this class. Call <code>makeClassInitializer()</code> in
* <code>CtClass</code> and append code snippet to the body of the class
* initializer obtained by <code>makeClassInitializer()</code>.
*
* @see CtClass#addConstructor(CtConstructor)
* @see CtClass#makeClassInitializer()
*/
public class CtNewConstructor {
/**
* Specifies that no parameters are passed to a super-class'
* constructor. That is, the default constructor is invoked.
*/
public static final int PASS_NONE = 0; // call super()
/**
* Specifies that parameters are converted into an array of
* <code>Object</code> and passed to a super-class'
* constructor.
*/
public static final int PASS_ARRAY = 1; // an array of parameters
/**
* Specifies that parameters are passed <i>as is</i>
* to a super-class' constructor. The signature of that
* constructor must be the same as that of the created constructor.
*/
public static final int PASS_PARAMS = 2;
/**
* Compiles the given source code and creates a constructor.
* The source code must include not only the constructor body
* but the whole declaration.
*
* @param src the source text.
* @param declaring the class to which the created constructor is added.
*/
public static CtConstructor make(String src, CtClass declaring)
throws CannotCompileException
{
Javac compiler = new Javac(declaring);
try {
CtMember obj = compiler.compile(src);
if (obj instanceof CtConstructor) {
// a stack map table has been already created.
return (CtConstructor)obj;
}
}
catch (CompileError e) {
throw new CannotCompileException(e);
}
throw new CannotCompileException("not a constructor");
}
/**
* Creates a public constructor.
*
* @param parameters a list of the parameter types.
* @param exceptions a list of the exception types.
* @param body the source text of the constructor body.
* It must be a block surrounded by <code>{}</code>.
* If it is <code>null</code>, the substituted
* constructor body does nothing except calling
* <code>super()</code>.
* @param declaring the class to which the created method is added.
*/
public static CtConstructor make(CtClass[] parameters,
CtClass[] exceptions,
String body, CtClass declaring)
throws CannotCompileException
{
try {
CtConstructor cc = new CtConstructor(parameters, declaring);
cc.setExceptionTypes(exceptions);
cc.setBody(body);
return cc;
}
catch (NotFoundException e) {
throw new CannotCompileException(e);
}
}
/**
* Creates a copy of a constructor.
* This is a convenience method for calling
* {@link CtConstructor#CtConstructor(CtConstructor, CtClass, ClassMap) this constructor}.
* See the description of the constructor for particular behavior of the copying.
*
* @param c the copied constructor.
* @param declaring the class to which the created method is added.
* @param map the hash table associating original class names
* with substituted names.
* It can be <code>null</code>.
*
* @see CtConstructor#CtConstructor(CtConstructor,CtClass,ClassMap)
*/
public static CtConstructor copy(CtConstructor c, CtClass declaring,
ClassMap map) throws CannotCompileException {
return new CtConstructor(c, declaring, map);
}
/**
* Creates a default (public) constructor.
*
* <p>The created constructor takes no parameter. It calls
* <code>super()</code>.
*/
public static CtConstructor defaultConstructor(CtClass declaring)
throws CannotCompileException
{
CtConstructor cons = new CtConstructor((CtClass[])null, declaring);
ConstPool cp = declaring.getClassFile2().getConstPool();
Bytecode code = new Bytecode(cp, 1, 1);
code.addAload(0);
try {
code.addInvokespecial(declaring.getSuperclass(),
"<init>", "()V");
}
catch (NotFoundException e) {
throw new CannotCompileException(e);
}
code.add(Bytecode.RETURN);
// no need to construct a stack map table.
cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute());
return cons;
}
/**
* Creates a public constructor that only calls a constructor
* in the super class. The created constructor receives parameters
* specified by <code>parameters</code> but calls the super's
* constructor without those parameters (that is, it calls the default
* constructor).
*
* <p>The parameters passed to the created constructor should be
* used for field initialization. <code>CtField.Initializer</code>
* objects implicitly insert initialization code in constructor
* bodies.
*
* @param parameters parameter types
* @param exceptions exception types
* @param declaring the class to which the created constructor
* is added.
* @see CtField.Initializer#byParameter(int)
*/
public static CtConstructor skeleton(CtClass[] parameters,
CtClass[] exceptions, CtClass declaring)
throws CannotCompileException
{
return make(parameters, exceptions, PASS_NONE,
null, null, declaring);
}
/**
* Creates a public constructor that only calls a constructor
* in the super class. The created constructor receives parameters
* specified by <code>parameters</code> and calls the super's
* constructor with those parameters.
*
* @param parameters parameter types
* @param exceptions exception types
* @param declaring the class to which the created constructor
* is added.
*/
public static CtConstructor make(CtClass[] parameters,
CtClass[] exceptions, CtClass declaring)
throws CannotCompileException
{
return make(parameters, exceptions, PASS_PARAMS,
null, null, declaring);
}
/**
* Creates a public constructor.
*
* <p>If <code>howto</code> is <code>PASS_PARAMS</code>,
* 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.
*
* <p>If <code>howto</code> is <code>PASS_NONE</code>,
* the created constructor calls the super's default constructor.
* The superclass must contain a constructor taking no parameters.
*
* <p>If <code>howto</code> is <code>PASS_ARRAY</code>,
* the created constructor calls the super's constructor
* with the given parameters in the form of an array of
* <code>Object</code>. The signature of the super's constructor
* must be:
*
* <pre>constructor(Object[] params, <type> cvalue)
* </pre>
*
* <p>Here, <code>cvalue</code> is the constant value specified
* by <code>cparam</code>.
*
* <p>If <code>cparam</code> is <code>null</code>, the signature
* must be:
*
* <pre>constructor(Object[] params)</pre>
*
* <p>If <code>body</code> 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 <code>body</code> must not
* be a constructor but a method.
*
* <p>Since the embedded method is wrapped
* in parameter-conversion code
* as in <code>CtNewMethod.wrapped()</code>,
* the constructor parameters are
* passed in the form of an array of <code>Object</code>.
* The method specified by <code>body</code> must have the
* signature shown below:
*
* <pre>Object method(Object[] params, <type> cvalue)</pre>
*
* <p>If <code>cparam</code> is <code>null</code>, the signature
* must be:
*
* <pre>Object method(Object[] params)</pre>
*
* <p>Although the type of the returned value is <code>Object</code>,
* the value must be always <code>null</code>.
*
* <p><i>Example:</i>
*
* <pre>
* 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);</pre>
*
* <p>where the class <code>Sample</code> is as follows:
*
* <pre>
* public class Sample {
* public Object m(Object[] args, String msg) {
* System.out.println(msg);
* return null;
* }
* }</pre>
*
* <p>This program produces the following class:
*
* <pre>
* 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
* }
* }</pre>
*
* @param parameters a list of the parameter types
* @param exceptions a list of the exceptions
* @param howto how to pass parameters to the super-class'
* constructor (<code>PASS_NONE</code>,
* <code>PASS_ARRAY</code>,
* or <code>PASS_PARAMS</code>)
* @param body appended body (may be <code>null</code>).
* It must be not a constructor but a method.
* @param cparam constant parameter (may be <code>null</code>.)
* @param declaring the class to which the created constructor
* is added.
*
* @see CtNewMethod#wrapped(CtClass,String,CtClass[],CtClass[],CtMethod,CtMethod.ConstParameter,CtClass)
*/
public static CtConstructor make(CtClass[] parameters,
CtClass[] exceptions, int howto,
CtMethod body, ConstParameter cparam,
CtClass declaring)
throws CannotCompileException
{
return CtNewWrappedConstructor.wrapped(parameters, exceptions,
howto, body, cparam, declaring);
}
}
|