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.

CtNewClass.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2004 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 java.io.DataOutputStream;
  17. import java.io.IOException;
  18. import javassist.bytecode.ClassFile;
  19. class CtNewClass extends CtClassType {
  20. /* true if the class is an interface.
  21. */
  22. protected boolean hasConstructor;
  23. CtNewClass(String name, ClassPool cp,
  24. boolean isInterface, CtClass superclass) {
  25. super(name, cp);
  26. wasChanged = true;
  27. eraseCache();
  28. String superName;
  29. if (superclass == null)
  30. superName = null;
  31. else
  32. superName = superclass.getName();
  33. classfile = new ClassFile(isInterface, name, superName);
  34. setModifiers(Modifier.setPublic(getModifiers()));
  35. hasConstructor = isInterface;
  36. }
  37. protected void extendToString(StringBuffer buffer) {
  38. if (hasConstructor)
  39. buffer.append("hasConstructor ");
  40. super.extendToString(buffer);
  41. }
  42. public void addConstructor(CtConstructor c)
  43. throws CannotCompileException
  44. {
  45. hasConstructor = true;
  46. super.addConstructor(c);
  47. }
  48. public void toBytecode(DataOutputStream out)
  49. throws CannotCompileException, IOException
  50. {
  51. if (!hasConstructor)
  52. try {
  53. inheritAllConstructors();
  54. }
  55. catch (NotFoundException e) {
  56. throw new CannotCompileException(e);
  57. }
  58. super.toBytecode(out);
  59. }
  60. /**
  61. * Adds constructors inhrited from the super class.
  62. *
  63. * <p>After this method is called, the class inherits all the
  64. * constructors from the super class. The added constructor
  65. * calls the super's constructor with the same signature.
  66. */
  67. public void inheritAllConstructors()
  68. throws CannotCompileException, NotFoundException
  69. {
  70. CtClass superclazz;
  71. CtConstructor[] cs;
  72. superclazz = getSuperclass();
  73. cs = superclazz.getDeclaredConstructors();
  74. int n = 0;
  75. for (int i = 0; i < cs.length; ++i) {
  76. CtConstructor c = cs[i];
  77. if (Modifier.isPublic(c.getModifiers())) {
  78. CtConstructor cons
  79. = CtNewConstructor.make(c.getParameterTypes(),
  80. c.getExceptionTypes(), this);
  81. addConstructor(cons);
  82. ++n;
  83. }
  84. }
  85. if (n < 1)
  86. throw new CannotCompileException(
  87. "no public constructor in " + superclazz.getName());
  88. }
  89. }