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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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. hasConstructor = true;
  55. }
  56. catch (NotFoundException e) {
  57. throw new CannotCompileException(e);
  58. }
  59. super.toBytecode(out);
  60. }
  61. /**
  62. * Adds constructors inhrited from the super class.
  63. *
  64. * <p>After this method is called, the class inherits all the
  65. * constructors from the super class. The added constructor
  66. * calls the super's constructor with the same signature.
  67. */
  68. public void inheritAllConstructors()
  69. throws CannotCompileException, NotFoundException
  70. {
  71. CtClass superclazz;
  72. CtConstructor[] cs;
  73. superclazz = getSuperclass();
  74. cs = superclazz.getDeclaredConstructors();
  75. int n = 0;
  76. for (int i = 0; i < cs.length; ++i) {
  77. CtConstructor c = cs[i];
  78. if (Modifier.isPublic(c.getModifiers())) {
  79. CtConstructor cons
  80. = CtNewConstructor.make(c.getParameterTypes(),
  81. c.getExceptionTypes(), this);
  82. addConstructor(cons);
  83. ++n;
  84. }
  85. }
  86. if (n < 1)
  87. throw new CannotCompileException(
  88. "no public constructor in " + superclazz.getName());
  89. }
  90. }