You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CtNewWrappedConstructor.java 3.9KB

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