01: /*
02: * Javassist, a Java-bytecode translator toolkit.
03: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
04: *
05: * The contents of this file are subject to the Mozilla Public License Version
06: * 1.1 (the "License"); you may not use this file except in compliance with
07: * the License. Alternatively, the contents of this file may be used under
08: * the terms of the GNU Lesser General Public License Version 2.1 or later.
09: *
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, int howToCallSuper,
28: CtMethod body, ConstParameter constParam, CtClass declaring)
29: throws CannotCompileException {
30: try {
31: CtConstructor cons = new CtConstructor(parameterTypes,
32: declaring);
33: cons.setExceptionTypes(exceptionTypes);
34: Bytecode code = makeBody(declaring, declaring
35: .getClassFile2(), howToCallSuper, body,
36: parameterTypes, constParam);
37: cons.getMethodInfo2().setCodeAttribute(
38: code.toCodeAttribute());
39: return cons;
40: } catch (NotFoundException e) {
41: throw new CannotCompileException(e);
42: }
43: }
44:
45: protected static Bytecode makeBody(CtClass declaring,
46: ClassFile classfile, int howToCallSuper,
47: CtMethod wrappedBody, CtClass[] parameters,
48: ConstParameter cparam) throws CannotCompileException {
49: int stacksize, stacksize2;
50:
51: int super clazz = classfile.getSuperclassId();
52: Bytecode code = new Bytecode(classfile.getConstPool(), 0, 0);
53: code.setMaxLocals(false, parameters, 0);
54: code.addAload(0);
55: if (howToCallSuper == PASS_NONE) {
56: stacksize = 1;
57: code.addInvokespecial(super clazz, "<init>", "()V");
58: } else if (howToCallSuper == PASS_PARAMS) {
59: stacksize = code.addLoadParameters(parameters, 1) + 1;
60: code.addInvokespecial(super clazz, "<init>", Descriptor
61: .ofConstructor(parameters));
62: } else {
63: stacksize = compileParameterList(code, parameters, 1);
64: String desc;
65: if (cparam == null) {
66: stacksize2 = 2;
67: desc = ConstParameter.defaultConstDescriptor();
68: } else {
69: stacksize2 = cparam.compile(code) + 2;
70: desc = cparam.constDescriptor();
71: }
72:
73: if (stacksize < stacksize2)
74: stacksize = stacksize2;
75:
76: code.addInvokespecial(super clazz, "<init>", desc);
77: }
78:
79: if (wrappedBody == null)
80: code.add(Bytecode.RETURN);
81: else {
82: stacksize2 = makeBody0(declaring, classfile, wrappedBody,
83: false, parameters, CtClass.voidType, cparam, code);
84: if (stacksize < stacksize2)
85: stacksize = stacksize2;
86: }
87:
88: code.setMaxStack(stacksize);
89: return code;
90: }
91: }
|