Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CtNewWrappedConstructor.java 4.0KB

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