1 11 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jdt.core.IType; 16 import org.eclipse.jdt.core.dom.ITypeBinding; 17 18 19 20 public final class ParameterizedType extends HierarchyType { 21 22 private GenericType fTypeDeclaration; 23 private TType[] fTypeArguments; 24 25 protected ParameterizedType(TypeEnvironment environment) { 26 super(environment); 27 } 28 29 protected void initialize(ITypeBinding binding, IType javaElementType) { 30 Assert.isTrue(binding.isParameterizedType()); 31 super.initialize(binding, javaElementType); 32 TypeEnvironment environment= getEnvironment(); 33 fTypeDeclaration= (GenericType)environment.create(binding.getTypeDeclaration()); 34 ITypeBinding[] typeArguments= binding.getTypeArguments(); 35 fTypeArguments= new TType[typeArguments.length]; 36 for (int i= 0; i < typeArguments.length; i++) { 37 fTypeArguments[i]= environment.create(typeArguments[i]); 38 } 39 } 40 41 public int getKind() { 42 return PARAMETERIZED_TYPE; 43 } 44 45 public TType getTypeDeclaration() { 46 return fTypeDeclaration; 47 } 48 49 public TType getErasure() { 50 return fTypeDeclaration; 51 } 52 53 public TType[] getTypeArguments() { 54 return (TType[]) fTypeArguments.clone(); 55 } 56 57 public boolean doEquals(TType type) { 58 ParameterizedType other= (ParameterizedType)type; 59 if (! getBindingKey().equals(other.getBindingKey())) 60 return false; 61 if (! getJavaElementType().equals(other.getJavaElementType())) 62 return false; 63 return true; 64 } 65 66 public int hashCode() { 67 return getBindingKey().hashCode(); 68 } 69 70 protected boolean doCanAssignTo(TType lhs) { 71 int targetType= lhs.getKind(); 72 switch (targetType) { 73 case NULL_TYPE: return false; 74 case VOID_TYPE: return false; 75 case PRIMITIVE_TYPE: return false; 76 77 case ARRAY_TYPE: return false; 78 79 case STANDARD_TYPE: return canAssignToStandardType((StandardType)lhs); 80 case GENERIC_TYPE: return false; 81 case PARAMETERIZED_TYPE: return canAssignToParameterizedType((ParameterizedType)lhs); 82 case RAW_TYPE: return canAssignToRawType((RawType)lhs); 83 84 case UNBOUND_WILDCARD_TYPE: 85 case SUPER_WILDCARD_TYPE: 86 case EXTENDS_WILDCARD_TYPE: 87 return ((WildcardType)lhs).checkAssignmentBound(this); 88 89 case TYPE_VARIABLE: return false; 90 case CAPTURE_TYPE: 91 return ((CaptureType)lhs).checkLowerBound(this); 92 } 93 return false; 94 } 95 96 protected boolean isTypeEquivalentTo(TType other) { 97 int otherElementType= other.getKind(); 98 if (otherElementType == RAW_TYPE || otherElementType == GENERIC_TYPE) 99 return getErasure().isTypeEquivalentTo(other.getErasure()); 100 return super.isTypeEquivalentTo(other); 101 } 102 103 private boolean canAssignToRawType(RawType target) { 104 return fTypeDeclaration.isSubType(target.getHierarchyType()); 105 } 106 107 private boolean canAssignToParameterizedType(ParameterizedType target) { 108 GenericType targetDeclaration= target.fTypeDeclaration; 109 ParameterizedType sameSourceType= findSameDeclaration(targetDeclaration); 110 if (sameSourceType == null) 111 return false; 112 TType[] targetArguments= target.fTypeArguments; 113 TType[] sourceArguments= sameSourceType.fTypeArguments; 114 if (targetArguments.length != sourceArguments.length) 115 return false; 116 for (int i= 0; i < sourceArguments.length; i++) { 117 if (!targetArguments[i].checkTypeArgument(sourceArguments[i])) 118 return false; 119 } 120 return true; 121 } 122 123 private ParameterizedType findSameDeclaration(GenericType targetDeclaration) { 124 if (fTypeDeclaration.equals(targetDeclaration)) 125 return this; 126 ParameterizedType result= null; 127 TType type= getSuperclass(); 128 if (type != null && type.getKind() == PARAMETERIZED_TYPE) { 129 result= ((ParameterizedType)type).findSameDeclaration(targetDeclaration); 130 if (result != null) 131 return result; 132 } 133 TType[] interfaces= getInterfaces(); 134 for (int i= 0; i < interfaces.length; i++) { 135 type= interfaces[i]; 136 if (type != null && type.getKind() == PARAMETERIZED_TYPE) { 137 result= ((ParameterizedType)type).findSameDeclaration(targetDeclaration); 138 if (result != null) 139 return result; 140 } 141 } 142 return null; 143 } 144 145 public String getName() { 146 StringBuffer result= new StringBuffer (getJavaElementType().getElementName()); 147 result.append("<"); result.append(fTypeArguments[0].getName()); 149 for (int i= 1; i < fTypeArguments.length; i++) { 150 result.append(", "); result.append(fTypeArguments[i].getName()); 152 } 153 result.append(">"); return result.toString(); 155 } 156 157 protected String getPlainPrettySignature() { 158 StringBuffer result= new StringBuffer (getJavaElementType().getFullyQualifiedName('.')); 159 result.append("<"); result.append(fTypeArguments[0].getPlainPrettySignature()); 161 for (int i= 1; i < fTypeArguments.length; i++) { 162 result.append(", "); result.append(fTypeArguments[i].getPlainPrettySignature()); 164 } 165 result.append(">"); return result.toString(); 167 } 168 } 169 | Popular Tags |