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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 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. public void addConstructor(CtConstructor c)
  38. throws CannotCompileException
  39. {
  40. hasConstructor = true;
  41. super.addConstructor(c);
  42. }
  43. void toBytecode(DataOutputStream out)
  44. throws CannotCompileException, IOException
  45. {
  46. if (!hasConstructor)
  47. try {
  48. inheritAllConstructors();
  49. }
  50. catch (NotFoundException e) {
  51. throw new CannotCompileException(e);
  52. }
  53. super.toBytecode(out);
  54. }
  55. /**
  56. * Adds constructors inhrited from the super class.
  57. *
  58. * <p>After this method is called, the class inherits all the
  59. * constructors from the super class. The added constructor
  60. * calls the super's constructor with the same signature.
  61. */
  62. public void inheritAllConstructors()
  63. throws CannotCompileException, NotFoundException
  64. {
  65. CtClass superclazz;
  66. CtConstructor[] cs;
  67. superclazz = getSuperclass();
  68. cs = superclazz.getDeclaredConstructors();
  69. int n = 0;
  70. for (int i = 0; i < cs.length; ++i) {
  71. CtConstructor c = cs[i];
  72. if (Modifier.isPublic(c.getModifiers())) {
  73. CtConstructor cons
  74. = CtNewConstructor.make(c.getParameterTypes(),
  75. c.getExceptionTypes(), this);
  76. addConstructor(cons);
  77. ++n;
  78. }
  79. }
  80. if (n < 1)
  81. throw new CannotCompileException(
  82. "no public constructor in " + superclazz.getName());
  83. }
  84. }