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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- 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. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist;
  17. import javassist.CtMethod.ConstParameter;
  18. import javassist.bytecode.Bytecode;
  19. import javassist.bytecode.ClassFile;
  20. import javassist.bytecode.Descriptor;
  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. public static CtConstructor wrapped(CtClass[] parameterTypes,
  26. CtClass[] exceptionTypes,
  27. int howToCallSuper,
  28. CtMethod body,
  29. ConstParameter constParam,
  30. CtClass declaring)
  31. throws CannotCompileException
  32. {
  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. // a stack map table is not needed.
  41. return cons;
  42. }
  43. catch (NotFoundException e) {
  44. throw new CannotCompileException(e);
  45. }
  46. }
  47. protected static Bytecode makeBody(CtClass declaring, ClassFile classfile,
  48. int howToCallSuper,
  49. CtMethod wrappedBody,
  50. CtClass[] parameters,
  51. ConstParameter cparam)
  52. throws CannotCompileException
  53. {
  54. int stacksize, stacksize2;
  55. int superclazz = classfile.getSuperclassId();
  56. Bytecode code = new Bytecode(classfile.getConstPool(), 0, 0);
  57. code.setMaxLocals(false, parameters, 0);
  58. code.addAload(0);
  59. if (howToCallSuper == PASS_NONE) {
  60. stacksize = 1;
  61. code.addInvokespecial(superclazz, "<init>", "()V");
  62. }
  63. else if (howToCallSuper == PASS_PARAMS) {
  64. stacksize = code.addLoadParameters(parameters, 1) + 1;
  65. code.addInvokespecial(superclazz, "<init>",
  66. Descriptor.ofConstructor(parameters));
  67. }
  68. else {
  69. stacksize = compileParameterList(code, parameters, 1);
  70. String desc;
  71. if (cparam == null) {
  72. stacksize2 = 2;
  73. desc = ConstParameter.defaultConstDescriptor();
  74. }
  75. else {
  76. stacksize2 = cparam.compile(code) + 2;
  77. desc = cparam.constDescriptor();
  78. }
  79. if (stacksize < stacksize2)
  80. stacksize = stacksize2;
  81. code.addInvokespecial(superclazz, "<init>", desc);
  82. }
  83. if (wrappedBody == null)
  84. code.add(Bytecode.RETURN);
  85. else {
  86. stacksize2 = makeBody0(declaring, classfile, wrappedBody,
  87. false, parameters, CtClass.voidType,
  88. cparam, code);
  89. if (stacksize < stacksize2)
  90. stacksize = stacksize2;
  91. }
  92. code.setMaxStack(stacksize);
  93. return code;
  94. }
  95. }