1 /*******************************************************************************
2 * Copyright (c) 2000-2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.lookup;
14 import org.eclipse.jdt.core.compiler.CharOperation;
15 import org.eclipse.jdt.internal.compiler.ast.Wildcard;
18 * A wildcard acts as an argument for parameterized types, allowing to
19 * abstract parameterized types, e.g. List<String> is not compatible with List<Object>,
20 * but compatible with List<?>.
22 public class WildcardBinding extends ReferenceBinding {
24 ReferenceBinding genericType;
26 public TypeBinding bound; // when unbound denotes the corresponding type variable (so as to retrieve its bound lazily)
27 char[] genericSignature;
29 ReferenceBinding superclass;
30 ReferenceBinding[] superInterfaces;
31 TypeVariableBinding typeVariable; // corresponding variable
32 LookupEnvironment environment;
35 * When unbound, the bound denotes the corresponding type variable (so as to retrieve its bound lazily)
37 public WildcardBinding(ReferenceBinding genericType, int rank, TypeBinding bound, int kind, LookupEnvironment environment) {
38 this.genericType = genericType;
41 this.modifiers = AccPublic | AccGenericSignature; // treat wildcard as public
42 this.environment = environment;
43 initialize(genericType, bound);
45 if (genericType instanceof UnresolvedReferenceBinding)
46 ((UnresolvedReferenceBinding) genericType).addWrapper(this);
47 if (bound instanceof UnresolvedReferenceBinding)
48 ((UnresolvedReferenceBinding) bound).addWrapper(this);
56 * Returns true if the argument type satisfies all bounds of the type parameter
58 public boolean boundCheck(TypeBinding argumentType) {
60 case Wildcard.UNBOUND :
62 case Wildcard.EXTENDS :
63 return argumentType.isCompatibleWith(this.bound);
65 // allowed as long as one is compatible with other (either way)
66 // ? super Exception ok for: IOException, since it would be ok for (Exception)ioException
67 return this.bound.isCompatibleWith(argumentType)
68 || argumentType.isCompatibleWith(this.bound);
72 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#canBeInstantiated()
74 public boolean canBeInstantiated() {
75 // cannot be asked per construction
79 * Collect the substitutes into a map for certain type variables inside the receiver type
80 * e.g. Collection<T>.findSubstitute(T, Collection<List<X>>): T --> List<X>
82 public void collectSubstitutes(TypeBinding otherType, Map substitutes) {
84 if (this.bound == null)
86 if (otherType.isWildcard()) {
87 WildcardBinding otherWildcard = (WildcardBinding) otherType;
88 if (otherWildcard.bound != null) {
89 this.bound.collectSubstitutes(otherWildcard.bound, substitutes);
92 this.bound.collectSubstitutes(otherType, substitutes);
97 * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#debugName()
99 public String debugName() {
104 * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#erasure()
106 public TypeBinding erasure() {
107 if (this.kind == Wildcard.EXTENDS)
108 return this.bound.erasure();
109 return typeVariable().erasure();
113 * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#signature()
115 public char[] genericTypeSignature() {
116 if (this.genericSignature == null) {
118 case Wildcard.UNBOUND :
119 this.genericSignature = WILDCARD_STAR;
121 case Wildcard.EXTENDS :
122 this.genericSignature = CharOperation.concat(WILDCARD_PLUS, this.bound.genericTypeSignature());
125 this.genericSignature = CharOperation.concat(WILDCARD_MINUS, this.bound.genericTypeSignature());
128 return this.genericSignature;
131 public int hashCode() {
132 return this.genericType.hashCode();
135 void initialize(ReferenceBinding someGenericType, TypeBinding someBound) {
136 this.genericType = someGenericType;
137 this.bound = someBound;
138 if (someGenericType != null) {
139 this.fPackage = someGenericType.getPackage();
141 if (someBound != null) {
142 if (someBound.isTypeVariable())
143 this.tagBits |= HasTypeVariable;
148 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#isSuperclassOf(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)
150 public boolean isSuperclassOf(ReferenceBinding otherType) {
151 if (this.kind == Wildcard.SUPER) {
152 if (this.bound instanceof ReferenceBinding) {
153 return ((ReferenceBinding) this.bound).isSuperclassOf(otherType);
154 } else { // array bound
155 return otherType.id == T_JavaLangObject;
162 * Returns true if the type is a wildcard
164 public boolean isUnboundWildcard() {
165 return this.kind == Wildcard.UNBOUND;
169 * Returns true if the type is a wildcard
171 public boolean isWildcard() {
176 * @see org.eclipse.jdt.internal.compiler.lookup.Binding#readableName()
178 public char[] readableName() {
180 case Wildcard.UNBOUND :
181 return WILDCARD_NAME;
182 case Wildcard.EXTENDS :
183 return CharOperation.concat(WILDCARD_NAME, WILDCARD_EXTENDS, this.bound.readableName());
185 return CharOperation.concat(WILDCARD_NAME, WILDCARD_SUPER, this.bound.readableName());
189 ReferenceBinding resolve() {
190 BinaryTypeBinding.resolveType(this.genericType, this.environment, null, 0);
192 case Wildcard.EXTENDS :
193 case Wildcard.SUPER :
194 BinaryTypeBinding.resolveType(this.bound, this.environment, null, 0);
196 case Wildcard.UNBOUND :
202 * @see org.eclipse.jdt.internal.compiler.lookup.Binding#shortReadableName()
204 public char[] shortReadableName() {
206 case Wildcard.UNBOUND :
207 return WILDCARD_NAME;
208 case Wildcard.EXTENDS :
209 return CharOperation.concat(WILDCARD_NAME, WILDCARD_EXTENDS, this.bound.shortReadableName());
211 return CharOperation.concat(WILDCARD_NAME, WILDCARD_SUPER, this.bound.shortReadableName());
216 * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#signature()
218 public char[] signature() {
219 // should not be called directly on a wildcard; signature should only be asked on
220 // original methods or type erasures (which cannot denote wildcards at first level)
221 if (this.signature == null) {
223 case Wildcard.EXTENDS :
224 return this.bound.signature();
225 default: // SUPER | UNBOUND
226 return this.typeVariable().signature();
229 return this.signature;
233 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#sourceName()
235 public char[] sourceName() {
237 case Wildcard.UNBOUND :
238 return WILDCARD_NAME;
239 case Wildcard.EXTENDS :
240 return CharOperation.concat(WILDCARD_NAME, WILDCARD_EXTENDS, this.bound.sourceName());
242 return CharOperation.concat(WILDCARD_NAME, WILDCARD_SUPER, this.bound.sourceName());
247 * @see org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding#superclass()
249 public ReferenceBinding superclass() {
250 if (this.superclass == null) {
251 TypeBinding superType = this.typeVariable().firstBound;
252 if (this.kind == Wildcard.EXTENDS) {
253 if (this.bound.isClass()) {
254 superType = this.bound;
257 this.superclass = superType != null && superType.isClass()
258 ? (ReferenceBinding) superType
259 : environment.getType(JAVA_LANG_OBJECT);
262 return this.superclass;
265 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#superInterfaces()
267 public ReferenceBinding[] superInterfaces() {
268 if (this.superInterfaces == null) {
269 if (this.typeVariable() != null) {
270 this.superInterfaces = this.typeVariable.superInterfaces();
272 this.superInterfaces = NoSuperInterfaces;
274 if (this.kind == Wildcard.EXTENDS) {
275 if (this.bound.isInterface()) {
276 // augment super interfaces with the wildcard bound
277 int length = this.superInterfaces.length;
278 System.arraycopy(this.superInterfaces, 0, this.superInterfaces = new ReferenceBinding[length+1], 1, length);
279 this.superInterfaces[0] = (ReferenceBinding) this.bound; // make bound first
283 return this.superInterfaces;
286 public void swapUnresolved(UnresolvedReferenceBinding unresolvedType, ReferenceBinding resolvedType, LookupEnvironment env) {
287 boolean affected = false;
288 if (this.genericType == unresolvedType) {
289 this.genericType = resolvedType; // no raw conversion
291 } else if (this.bound == unresolvedType) {
292 this.bound = resolvedType.isGenericType() ? env.createRawType(resolvedType, resolvedType.enclosingType()) : resolvedType;
296 initialize(this.genericType, this.bound);
300 * @see java.lang.Object#toString()
302 public String toString() {
304 case Wildcard.UNBOUND :
305 return new String(WILDCARD_NAME);
306 case Wildcard.EXTENDS :
307 return new String(CharOperation.concat(WILDCARD_NAME, WILDCARD_EXTENDS, this.bound.debugName().toCharArray()));
309 return new String(CharOperation.concat(WILDCARD_NAME, WILDCARD_SUPER, this.bound.debugName().toCharArray()));
313 * Returns associated type variable, or null in case of inconsistency
315 public TypeVariableBinding typeVariable() {
316 if (this.typeVariable == null) {
317 TypeVariableBinding[] typeVariables = this.genericType.typeVariables();
318 if (this.rank < typeVariables.length)
319 this.typeVariable = typeVariables[this.rank];
321 return this.typeVariable;