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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. @Override
  40. protected void extendToString(StringBuffer buffer) {
  41. if (hasConstructor)
  42. buffer.append("hasConstructor ");
  43. super.extendToString(buffer);
  44. }
  45. @Override
  46. public void addConstructor(CtConstructor c)
  47. throws CannotCompileException
  48. {
  49. hasConstructor = true;
  50. super.addConstructor(c);
  51. }
  52. @Override
  53. public void toBytecode(DataOutputStream out)
  54. throws CannotCompileException, IOException
  55. {
  56. if (!hasConstructor)
  57. try {
  58. inheritAllConstructors();
  59. hasConstructor = true;
  60. }
  61. catch (NotFoundException e) {
  62. throw new CannotCompileException(e);
  63. }
  64. super.toBytecode(out);
  65. }
  66. /**
  67. * Adds constructors inhrited from the super class.
  68. *
  69. * <p>After this method is called, the class inherits all the
  70. * constructors from the super class. The added constructor
  71. * calls the super's constructor with the same signature.
  72. */
  73. public void inheritAllConstructors()
  74. throws CannotCompileException, NotFoundException
  75. {
  76. CtClass superclazz;
  77. CtConstructor[] cs;
  78. superclazz = getSuperclass();
  79. cs = superclazz.getDeclaredConstructors();
  80. int n = 0;
  81. for (int i = 0; i < cs.length; ++i) {
  82. CtConstructor c = cs[i];
  83. int mod = c.getModifiers();
  84. if (isInheritable(mod, superclazz)) {
  85. CtConstructor cons
  86. = CtNewConstructor.make(c.getParameterTypes(),
  87. c.getExceptionTypes(), this);
  88. cons.setModifiers(mod & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE));
  89. addConstructor(cons);
  90. ++n;
  91. }
  92. }
  93. if (n < 1)
  94. throw new CannotCompileException(
  95. "no inheritable constructor in " + superclazz.getName());
  96. }
  97. private boolean isInheritable(int mod, CtClass superclazz) {
  98. if (Modifier.isPrivate(mod))
  99. return false;
  100. if (Modifier.isPackage(mod)) {
  101. String pname = getPackageName();
  102. String pname2 = superclazz.getPackageName();
  103. if (pname == null)
  104. return pname2 == null;
  105. return pname.equals(pname2);
  106. }
  107. return true;
  108. }
  109. }