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

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