/Users/lyon/j4p/src/javassist/CtNewWrappedConstructor.java
|
1 /*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2003 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.CtMethod.ConstParameter;
20
21 class CtNewWrappedConstructor extends CtNewWrappedMethod {
22 private static final int PASS_NONE = CtNewConstructor.PASS_NONE;
23 private static final int PASS_ARRAY = CtNewConstructor.PASS_ARRAY;
24 private static final int PASS_PARAMS = CtNewConstructor.PASS_PARAMS;
25
26 public static CtConstructor wrapped(CtClass[] parameterTypes,
27 CtClass[] exceptionTypes,
28 int howToCallSuper,
29 CtMethod body,
30 ConstParameter constParam,
31 CtClass declaring)
32 throws CannotCompileException {
33 try {
34 CtConstructor cons = new CtConstructor(parameterTypes, declaring);
35 cons.setExceptionTypes(exceptionTypes);
36 Bytecode code = makeBody(declaring, declaring.getClassFile2(),
37 howToCallSuper, body,
38 parameterTypes, constParam);
39 cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute());
40 return cons;
41 } catch (NotFoundException e) {
42 throw new CannotCompileException(e);
43 }
44 }
45
46 protected static Bytecode makeBody(CtClass declaring, ClassFile classfile,
47 int howToCallSuper,
48 CtMethod wrappedBody,
49 CtClass[] parameters,
50 ConstParameter cparam)
51 throws CannotCompileException {
52 int stacksize, stacksize2;
53
54 int superclazz = classfile.getSuperclassId();
55 Bytecode code = new Bytecode(classfile.getConstPool(), 0, 0);
56 code.setMaxLocals(false, parameters, 0);
57 code.addAload(0);
58 if (howToCallSuper == PASS_NONE) {
59 stacksize = 1;
60 code.addInvokespecial(superclazz, "<init>", "()V");
61 } else if (howToCallSuper == PASS_PARAMS) {
62 stacksize = code.addLoadParameters(parameters, 1) + 1;
63 code.addInvokespecial(superclazz, "<init>",
64 Descriptor.ofConstructor(parameters));
65 } else {
66 stacksize = compileParameterList(code, parameters, 1);
67 String desc;
68 if (cparam == null) {
69 stacksize2 = 2;
70 desc = ConstParameter.defaultConstDescriptor();
71 } else {
72 stacksize2 = cparam.compile(code) + 2;
73 desc = cparam.constDescriptor();
74 }
75
76 if (stacksize < stacksize2)
77 stacksize = stacksize2;
78
79 code.addInvokespecial(superclazz, "<init>", desc);
80 }
81
82 if (wrappedBody == null)
83 code.add(Bytecode.RETURN);
84 else {
85 stacksize2 = makeBody0(declaring, classfile, wrappedBody,
86 false, parameters, CtClass.voidType,
87 cparam, code);
88 if (stacksize < stacksize2)
89 stacksize = stacksize2;
90 }
91
92 code.setMaxStack(stacksize);
93 return code;
94 }
95 }
96