ArrayType.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 1997, 2007 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.bytecode;
  4. import java.io.*;
  5. import java.util.*;
  6. public class ArrayType extends ObjectType
  7. implements Externalizable {
  8. public Type elements;
  9. public ArrayType(Type elements) {
  10. this(elements, elements.getName() + "[]");
  11. }
  12. ArrayType(Type elements, String name) {
  13. this_name = name;
  14. this.elements = elements;
  15. }
  16. public String getSignature() {
  17. if (signature == null)
  18. setSignature("[" + elements.getSignature());
  19. return signature;
  20. }
  21. public Type getImplementationType() {
  22. Type eltype = elements.getImplementationType();
  23. return elements == eltype ? this : make(eltype);
  24. }
  25. @Override
  26. public Type getRawType() {
  27. Type eltype = elements.getRawType();
  28. return elements == eltype ? this : make(eltype);
  29. }
  30. /** Name assumed to end with "[]". */
  31. static ArrayType make(String name) {
  32. Type elements = Type.getType(name.substring(0, name.length()-2));
  33. ArrayType array_type = elements.array_type;
  34. if (array_type == null) {
  35. array_type = new ArrayType(elements, name);
  36. elements.array_type = array_type;
  37. }
  38. return array_type;
  39. }
  40. /** Find or create an ArrayType for the specified element type. */
  41. public static ArrayType make(Type elements) {
  42. ArrayType array_type = elements.array_type;
  43. if (array_type == null) {
  44. array_type = new ArrayType(elements, elements.getName() + "[]");
  45. elements.array_type = array_type;
  46. }
  47. return array_type;
  48. }
  49. public Type getComponentType() { return elements; }
  50. public String getInternalName() { return getSignature(); }
  51. public Class getReflectClass() {
  52. try {
  53. if (reflectClass == null) {
  54. String cname = getInternalName().replace('/', '.');
  55. Class elClass = elements.getReflectClass();
  56. reflectClass = Class.forName(cname, false,
  57. elClass.getClassLoader());
  58. }
  59. flags |= EXISTING_CLASS;
  60. } catch (java.lang.ClassNotFoundException ex) {
  61. if ((flags & EXISTING_CLASS) != 0) {
  62. RuntimeException rex
  63. = new RuntimeException("no such array class: "+getName());
  64. rex.initCause(ex);
  65. throw rex;
  66. }
  67. }
  68. return reflectClass;
  69. }
  70. public int getMethods(Filter filter, int searchSupers, List<Method> result) {
  71. if (searchSupers > 0) {
  72. int count = Type.objectType.getMethods(filter, 0, result);
  73. if (searchSupers > 1 && filter.select(Type.clone_method)) {
  74. if (result != null) {
  75. Method meth = Type.clone_method;
  76. result.add(meth);
  77. }
  78. count++;
  79. }
  80. return count;
  81. }
  82. return 0;
  83. }
  84. @Override
  85. public int isCompatibleWithValue(Type valueType) {
  86. if (valueType instanceof ArrayType) {
  87. return getComponentType().isCompatibleWithValue
  88. (((ArrayType) valueType).getComponentType());
  89. }
  90. if (valueType == nullType)
  91. return 2;
  92. if (valueType == objectType)
  93. return 0;
  94. return -1;
  95. }
  96. public int compare(Type other) {
  97. if (other == nullType)
  98. return 1;
  99. if (other instanceof ArrayType)
  100. return elements.compare(((ArrayType) other).elements);
  101. else if (other.getName().equals("java.lang.Object")
  102. || other == toStringType)
  103. return -1;
  104. else
  105. return -3;
  106. }
  107. public void writeExternal(ObjectOutput out) throws IOException {
  108. out.writeObject(elements);
  109. }
  110. public void readExternal(ObjectInput in)
  111. throws IOException, ClassNotFoundException {
  112. elements = (Type) in.readObject();
  113. }
  114. public Object readResolve() throws ObjectStreamException {
  115. ArrayType array_type = elements.array_type;
  116. if (array_type != null)
  117. return array_type;
  118. else {
  119. elements.array_type = this;
  120. return this;
  121. }
  122. }
  123. }