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.

TypeVariableReferenceType.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* *******************************************************************
  2. * Copyright (c) 2005-2012 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. * ******************************************************************/
  9. package org.aspectj.weaver;
  10. import java.util.Map;
  11. /**
  12. * ReferenceType pointing to a type variable. The delegate for this reference type is the upperbound on the type variable (so
  13. * Object if not otherwise specified).
  14. *
  15. * @author Adrian Colyer
  16. * @author Andy Clement
  17. */
  18. public class TypeVariableReferenceType extends ReferenceType implements TypeVariableReference {
  19. private TypeVariable typeVariable;
  20. public TypeVariableReferenceType(TypeVariable typeVariable, World world) {
  21. super(typeVariable.getGenericSignature(), typeVariable.getErasureSignature(), world);
  22. this.typeVariable = typeVariable;
  23. }
  24. @Override
  25. public boolean equals(Object other) {
  26. if (other instanceof TypeVariableReferenceType) {
  27. return typeVariable==((TypeVariableReferenceType)other).typeVariable;
  28. }
  29. return false;
  30. }
  31. @Override
  32. public int hashCode() {
  33. return typeVariable.hashCode();
  34. }
  35. /**
  36. * For a TypeVariableReferenceType the delegate is the delegate for the first bound.
  37. */
  38. @Override
  39. public ReferenceTypeDelegate getDelegate() {
  40. if (this.delegate == null) {
  41. ResolvedType resolvedFirstBound = typeVariable.getFirstBound().resolve(world);
  42. BoundedReferenceTypeDelegate brtd = null;
  43. if (resolvedFirstBound.isMissing()) {
  44. brtd = new BoundedReferenceTypeDelegate((ReferenceType) world.resolve(UnresolvedType.OBJECT));
  45. setDelegate(brtd); // set now because getSourceLocation() below will cause a recursive step to discover the delegate
  46. world.getLint().cantFindType.signal(
  47. "Unable to find type for generic bound. Missing type is " + resolvedFirstBound.getName(),
  48. getSourceLocation());
  49. } else {
  50. brtd = new BoundedReferenceTypeDelegate((ReferenceType) resolvedFirstBound);
  51. setDelegate(brtd);
  52. }
  53. }
  54. return this.delegate;
  55. }
  56. @Override
  57. public UnresolvedType parameterize(Map<String, UnresolvedType> typeBindings) {
  58. UnresolvedType ut = typeBindings.get(getName());
  59. if (ut != null) {
  60. return world.resolve(ut);
  61. }
  62. return this;
  63. }
  64. public TypeVariable getTypeVariable() {
  65. return typeVariable;
  66. }
  67. @Override
  68. public boolean isTypeVariableReference() {
  69. return true;
  70. }
  71. @Override
  72. public String toString() {
  73. return typeVariable.getName();
  74. }
  75. @Override
  76. public boolean isGenericWildcard() {
  77. return false;
  78. }
  79. @Override
  80. public boolean isAnnotation() {
  81. ReferenceType upper = (ReferenceType) typeVariable.getUpperBound();
  82. if (upper.isAnnotation()) {
  83. return true;
  84. }
  85. World world = upper.getWorld();
  86. typeVariable.resolve(world);
  87. ResolvedType annotationType = ResolvedType.ANNOTATION.resolve(world);
  88. UnresolvedType[] ifBounds = typeVariable.getSuperInterfaces();// AdditionalBounds();
  89. for (int i = 0; i < ifBounds.length; i++) {
  90. if (((ReferenceType) ifBounds[i]).isAnnotation()) {
  91. return true;
  92. }
  93. if (ifBounds[i].equals(annotationType)) {
  94. return true; // annotation itself does not have the annotation flag set in Java!
  95. }
  96. }
  97. return false;
  98. }
  99. /**
  100. * return the signature for a *REFERENCE* to a type variable, which is simply: Tname; there is no bounds info included, that is
  101. * in the signature of the type variable itself
  102. */
  103. @Override
  104. public String getSignature() {
  105. StringBuffer sb = new StringBuffer();
  106. sb.append("T");
  107. sb.append(typeVariable.getName());
  108. sb.append(";");
  109. return sb.toString();
  110. }
  111. /**
  112. * @return the name of the type variable
  113. */
  114. public String getTypeVariableName() {
  115. return typeVariable.getName();
  116. }
  117. public ReferenceType getUpperBound() {
  118. return (ReferenceType) typeVariable.resolve(world).getUpperBound();
  119. }
  120. /**
  121. * resolve the type variable we are managing and then return this object. 'this' is already a ResolvedType but the type variable
  122. * may transition from a not-resolved to a resolved state.
  123. */
  124. public ResolvedType resolve(World world) {
  125. typeVariable.resolve(world);
  126. return this;
  127. }
  128. /**
  129. * @return true if the type variable this reference is managing is resolved
  130. */
  131. public boolean isTypeVariableResolved() {
  132. return typeVariable.isResolved;
  133. }
  134. }