ParameterizedType.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (c) 2011 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.bytecode;
  4. public class ParameterizedType extends ObjectType
  5. {
  6. ClassType rawType;
  7. Type[] typeArgumentTypes;
  8. char[] typeArgumentBounds;
  9. public ClassType getRawType() {
  10. return rawType;
  11. }
  12. public Class getReflectClass() {
  13. return rawType.getReflectClass();
  14. }
  15. public Type[] getTypeArgumentTypes() {
  16. return typeArgumentTypes;
  17. }
  18. public Type getTypeArgumentType(int index) {
  19. return typeArgumentTypes[index];
  20. }
  21. /** Set a wildcard indicator.
  22. */
  23. public void setTypeArgumentBound(int index, char bound) {
  24. int sz = typeArgumentTypes==null||index>=typeArgumentTypes.length
  25. ? index+1
  26. : typeArgumentTypes.length;
  27. char[] bounds = typeArgumentBounds;
  28. if (bounds == null)
  29. typeArgumentBounds = bounds = new char[sz];
  30. else if (sz > bounds.length) {
  31. typeArgumentBounds = new char[sz];
  32. System.arraycopy(bounds, 0, typeArgumentBounds, 0, bounds.length);
  33. bounds = typeArgumentBounds;
  34. }
  35. bounds[index] = bound;
  36. }
  37. public char getTypeArgumentBound(int index) {
  38. if (typeArgumentBounds == null || index >= typeArgumentBounds.length)
  39. return '\0';
  40. return typeArgumentBounds[index];
  41. }
  42. public void setTypeArgumentBounds(char[] bounds) {
  43. this.typeArgumentBounds = bounds;
  44. }
  45. @Override
  46. public String getSignature () { return getRawType().getSignature(); }
  47. @Override
  48. public String getGenericSignature() {
  49. String s = super.getGenericSignature();
  50. if (s == null) {
  51. StringBuilder buf = new StringBuilder();
  52. buf.append('L');
  53. buf.append(rawType.getName().replace('.', '/'));
  54. buf.append('<');
  55. int n = typeArgumentTypes.length;
  56. for (int i = 0; i < n; i++) {
  57. char bound = getTypeArgumentBound(i);
  58. Type tt = getTypeArgumentType(i);
  59. if (bound == '+' && tt == Type.objectType)
  60. buf.append('*');
  61. else {
  62. if (bound != '\0')
  63. buf.append(bound);
  64. buf.append(tt.getMaybeGenericSignature());
  65. }
  66. }
  67. buf.append(">;");
  68. s = buf.toString();
  69. super.setGenericSignature(s);
  70. }
  71. return s;
  72. }
  73. @Override
  74. public void emitCoerceFromObject (CodeAttr code) {
  75. getRawType().emitCoerceFromObject(code);
  76. }
  77. @Override
  78. public void emitIsInstance(CodeAttr code) {
  79. code.emitInstanceof(getRawType());
  80. }
  81. @Override
  82. public String getName() {
  83. return toString();
  84. }
  85. @Override
  86. public String toString() {
  87. StringBuilder buf = new StringBuilder();
  88. buf.append(rawType);
  89. buf.append('<');
  90. int n = typeArgumentTypes.length;
  91. for (int i = 0; i < n; i++) {
  92. if (i > 0)
  93. buf.append(',');
  94. char bound = getTypeArgumentBound(i);
  95. if (bound == '+')
  96. buf.append("? extends ");
  97. if (bound == '-')
  98. buf.append("? super ");
  99. buf.append(getTypeArgumentType(i));
  100. }
  101. buf.append('>');
  102. return buf.toString();
  103. }
  104. public int compare(Type other) {
  105. return rawType.compare(other);
  106. }
  107. public boolean equals(Object other) {
  108. if (other == this)
  109. return true;
  110. if (! (other instanceof ParameterizedType))
  111. return false;
  112. ParameterizedType pother = (ParameterizedType) other;
  113. if (! Type.isSame(rawType, pother.rawType))
  114. return false;
  115. int n = typeArgumentTypes.length;
  116. Type[] otherArgumentTypes = pother.typeArgumentTypes;
  117. if (n != otherArgumentTypes.length)
  118. return false;
  119. while (--n >= 0) {
  120. if (! Type.isSame(typeArgumentTypes[n],
  121. pother.typeArgumentTypes[n])
  122. || getTypeArgumentBound(n) != pother.getTypeArgumentBound(n))
  123. return false;
  124. }
  125. return true;
  126. }
  127. public ParameterizedType(ClassType rawType, Type... typeArgumentTypes) {
  128. this.rawType = rawType;
  129. this.typeArgumentTypes = typeArgumentTypes;
  130. }
  131. }