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.

CtArray.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /**
  18. * Array types.
  19. */
  20. final class CtArray extends CtClass
  21. {
  22. protected ClassPool pool;
  23. // the name of array type ends with "[]".
  24. CtArray(String name, ClassPool cp)
  25. {
  26. super(name);
  27. pool = cp;
  28. }
  29. @Override
  30. public ClassPool getClassPool()
  31. {
  32. return pool;
  33. }
  34. @Override
  35. public boolean isArray()
  36. {
  37. return true;
  38. }
  39. private CtClass[] interfaces = null;
  40. @Override
  41. public int getModifiers()
  42. {
  43. int mod = Modifier.FINAL;
  44. try {
  45. mod |= getComponentType().getModifiers()
  46. & (Modifier.PROTECTED | Modifier.PUBLIC | Modifier.PRIVATE);
  47. }
  48. catch (NotFoundException e) {}
  49. return mod;
  50. }
  51. @Override
  52. public CtClass[] getInterfaces() throws NotFoundException
  53. {
  54. if (interfaces == null) {
  55. Class<?>[] intfs = Object[].class.getInterfaces();
  56. // java.lang.Cloneable and java.io.Serializable.
  57. // If the JVM is CLDC, intfs is empty.
  58. interfaces = new CtClass[intfs.length];
  59. for (int i = 0; i < intfs.length; i++)
  60. interfaces[i] = pool.get(intfs[i].getName());
  61. }
  62. return interfaces;
  63. }
  64. @Override
  65. public boolean subtypeOf(CtClass clazz) throws NotFoundException
  66. {
  67. if (super.subtypeOf(clazz))
  68. return true;
  69. String cname = clazz.getName();
  70. if (cname.equals(javaLangObject))
  71. return true;
  72. CtClass[] intfs = getInterfaces();
  73. for (int i = 0; i < intfs.length; i++)
  74. if (intfs[i].subtypeOf(clazz))
  75. return true;
  76. return clazz.isArray()
  77. && getComponentType().subtypeOf(clazz.getComponentType());
  78. }
  79. @Override
  80. public CtClass getComponentType() throws NotFoundException
  81. {
  82. String name = getName();
  83. return pool.get(name.substring(0, name.length() - 2));
  84. }
  85. @Override
  86. public CtClass getSuperclass() throws NotFoundException
  87. {
  88. return pool.get(javaLangObject);
  89. }
  90. @Override
  91. public CtMethod[] getMethods()
  92. {
  93. try {
  94. return getSuperclass().getMethods();
  95. }
  96. catch (NotFoundException e) {
  97. return super.getMethods();
  98. }
  99. }
  100. @Override
  101. public CtMethod getMethod(String name, String desc)
  102. throws NotFoundException
  103. {
  104. return getSuperclass().getMethod(name, desc);
  105. }
  106. @Override
  107. public CtConstructor[] getConstructors()
  108. {
  109. try {
  110. return getSuperclass().getConstructors();
  111. }
  112. catch (NotFoundException e) {
  113. return super.getConstructors();
  114. }
  115. }
  116. }