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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 java.io.DataOutputStream;
  18. import java.io.IOException;
  19. import javassist.bytecode.ClassFile;
  20. class CtNewClass extends CtClassType {
  21. /* true if the class is an interface.
  22. */
  23. protected boolean hasConstructor;
  24. CtNewClass(String name, ClassPool cp,
  25. boolean isInterface, CtClass superclass) {
  26. super(name, cp);
  27. wasChanged = true;
  28. String superName;
  29. if (isInterface || superclass == null)
  30. superName = null;
  31. else
  32. superName = superclass.getName();
  33. classfile = new ClassFile(isInterface, name, superName);
  34. if (isInterface && superclass != null)
  35. classfile.setInterfaces(new String[] { superclass.getName() });
  36. setModifiers(Modifier.setPublic(getModifiers()));
  37. hasConstructor = isInterface;
  38. }
  39. protected void extendToString(StringBuffer buffer) {
  40. if (hasConstructor)
  41. buffer.append("hasConstructor ");
  42. super.extendToString(buffer);
  43. }
  44. public void addConstructor(CtConstructor c)
  45. throws CannotCompileException
  46. {
  47. hasConstructor = true;
  48. super.addConstructor(c);
  49. }
  50. public void toBytecode(DataOutputStream out)
  51. throws CannotCompileException, IOException
  52. {
  53. if (!hasConstructor)
  54. try {
  55. inheritAllConstructors();
  56. hasConstructor = true;
  57. }
  58. catch (NotFoundException e) {
  59. throw new CannotCompileException(e);
  60. }
  61. super.toBytecode(out);
  62. }
  63. /**
  64. * Adds constructors inhrited from the super class.
  65. *
  66. * <p>After this method is called, the class inherits all the
  67. * constructors from the super class. The added constructor
  68. * calls the super's constructor with the same signature.
  69. */
  70. public void inheritAllConstructors()
  71. throws CannotCompileException, NotFoundException
  72. {
  73. CtClass superclazz;
  74. CtConstructor[] cs;
  75. superclazz = getSuperclass();
  76. cs = superclazz.getDeclaredConstructors();
  77. int n = 0;
  78. for (int i = 0; i < cs.length; ++i) {
  79. CtConstructor c = cs[i];
  80. int mod = c.getModifiers();
  81. if (isInheritable(mod, superclazz)) {
  82. CtConstructor cons
  83. = CtNewConstructor.make(c.getParameterTypes(),
  84. c.getExceptionTypes(), this);
  85. cons.setModifiers(mod & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE));
  86. addConstructor(cons);
  87. ++n;
  88. }
  89. }
  90. if (n < 1)
  91. throw new CannotCompileException(
  92. "no inheritable constructor in " + superclazz.getName());
  93. }
  94. private boolean isInheritable(int mod, CtClass superclazz) {
  95. if (Modifier.isPrivate(mod))
  96. return false;
  97. if (Modifier.isPackage(mod)) {
  98. String pname = getPackageName();
  99. String pname2 = superclazz.getPackageName();
  100. if (pname == null)
  101. return pname2 == null;
  102. else
  103. return pname.equals(pname2);
  104. }
  105. return true;
  106. }
  107. }