added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / lookup / WildcardBinding.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.lookup;
12
13 import java.util.Map;
14 import org.eclipse.jdt.core.compiler.CharOperation;
15 import org.eclipse.jdt.internal.compiler.ast.Wildcard;
16
17 /*
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<?>.
21  */
22 public class WildcardBinding extends ReferenceBinding {
23
24         ReferenceBinding genericType;
25         int rank;
26     public TypeBinding bound; // when unbound denotes the corresponding type variable (so as to retrieve its bound lazily)
27         char[] genericSignature;
28         public int kind;
29         ReferenceBinding superclass;
30         ReferenceBinding[] superInterfaces;
31         TypeVariableBinding typeVariable; // corresponding variable
32         LookupEnvironment environment;
33         
34         /**
35          * When unbound, the bound denotes the corresponding type variable (so as to retrieve its bound lazily)
36          */
37         public WildcardBinding(ReferenceBinding genericType, int rank, TypeBinding bound, int kind, LookupEnvironment environment) {
38                 this.genericType = genericType;
39                 this.rank = rank;
40             this.kind = kind;
41                 this.modifiers = AccPublic | AccGenericSignature; // treat wildcard as public
42                 this.environment = environment;
43                 initialize(genericType, bound);
44
45                 if (genericType instanceof UnresolvedReferenceBinding)
46                         ((UnresolvedReferenceBinding) genericType).addWrapper(this);
47                 if (bound instanceof UnresolvedReferenceBinding)
48                         ((UnresolvedReferenceBinding) bound).addWrapper(this);
49         }
50
51         public int kind() {
52                 return WILDCARD_TYPE;
53         }       
54                 
55         /**
56          * Returns true if the argument type satisfies all bounds of the type parameter
57          */
58         public boolean boundCheck(TypeBinding argumentType) {
59             switch (this.kind) {
60                 case Wildcard.UNBOUND :
61                     return true;
62                 case Wildcard.EXTENDS :
63                     return argumentType.isCompatibleWith(this.bound);
64                 default: // SUPER
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);
69             }
70     }
71         /**
72          * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#canBeInstantiated()
73          */
74         public boolean canBeInstantiated() {
75                 // cannot be asked per construction
76                 return false;
77         }
78         /**
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>
81          */
82         public void collectSubstitutes(TypeBinding otherType, Map substitutes) {
83
84                 if (this.bound == null)
85                         return;
86                 if (otherType.isWildcard()) {
87                         WildcardBinding otherWildcard = (WildcardBinding) otherType;
88                         if (otherWildcard.bound != null) {
89                                 this.bound.collectSubstitutes(otherWildcard.bound, substitutes);
90                         }
91                 } else {
92             this.bound.collectSubstitutes(otherType, substitutes);
93                 }           
94         }
95         
96         /**
97          * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#debugName()
98          */
99         public String debugName() {
100             return toString();          
101         }       
102         
103     /* (non-Javadoc)
104      * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#erasure()
105      */
106     public TypeBinding erasure() {
107         if (this.kind == Wildcard.EXTENDS)
108                 return this.bound.erasure();
109         return typeVariable().erasure();
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#signature()
114      */
115     public char[] genericTypeSignature() {
116         if (this.genericSignature == null) {
117             switch (this.kind) {
118                 case Wildcard.UNBOUND : 
119                     this.genericSignature = WILDCARD_STAR;
120                     break;
121                 case Wildcard.EXTENDS :
122                     this.genericSignature = CharOperation.concat(WILDCARD_PLUS, this.bound.genericTypeSignature());
123                                         break;
124                                 default: // SUPER
125                                     this.genericSignature = CharOperation.concat(WILDCARD_MINUS, this.bound.genericTypeSignature());
126             }
127         } 
128         return this.genericSignature;
129     }
130     
131         public int hashCode() {
132                 return this.genericType.hashCode();
133         }
134
135         void initialize(ReferenceBinding someGenericType, TypeBinding someBound) {
136                 this.genericType = someGenericType;
137                 this.bound = someBound;
138                 if (someGenericType != null) {
139                         this.fPackage = someGenericType.getPackage();
140                 }
141                 if (someBound != null) {
142                     if (someBound.isTypeVariable())
143                         this.tagBits |= HasTypeVariable;
144                 }
145         }
146
147         /**
148      * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#isSuperclassOf(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)
149      */
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;
156             }
157         }
158         return false;
159     }
160
161     /**
162          * Returns true if the type is a wildcard
163          */
164         public boolean isUnboundWildcard() {
165             return this.kind == Wildcard.UNBOUND;
166         }
167         
168     /**
169          * Returns true if the type is a wildcard
170          */
171         public boolean isWildcard() {
172             return true;
173         }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.jdt.internal.compiler.lookup.Binding#readableName()
177      */
178     public char[] readableName() {
179         switch (this.kind) {
180             case Wildcard.UNBOUND : 
181                 return WILDCARD_NAME;
182             case Wildcard.EXTENDS :
183                 return CharOperation.concat(WILDCARD_NAME, WILDCARD_EXTENDS, this.bound.readableName());
184                         default: // SUPER
185                             return CharOperation.concat(WILDCARD_NAME, WILDCARD_SUPER, this.bound.readableName());
186         }
187     }
188     
189         ReferenceBinding resolve() {
190                 BinaryTypeBinding.resolveType(this.genericType, this.environment, null, 0);
191             switch(this.kind) {
192                 case Wildcard.EXTENDS :
193                 case Wildcard.SUPER :
194                                 BinaryTypeBinding.resolveType(this.bound, this.environment, null, 0);
195                                 break;
196                         case Wildcard.UNBOUND :
197             }
198                 return this;
199         }
200         
201     /* (non-Javadoc)
202      * @see org.eclipse.jdt.internal.compiler.lookup.Binding#shortReadableName()
203      */
204     public char[] shortReadableName() {
205         switch (this.kind) {
206             case Wildcard.UNBOUND : 
207                 return WILDCARD_NAME;
208             case Wildcard.EXTENDS :
209                 return CharOperation.concat(WILDCARD_NAME, WILDCARD_EXTENDS, this.bound.shortReadableName());
210                         default: // SUPER
211                             return CharOperation.concat(WILDCARD_NAME, WILDCARD_SUPER, this.bound.shortReadableName());
212         }
213     }
214     
215     /**
216      * @see org.eclipse.jdt.internal.compiler.lookup.TypeBinding#signature()
217      */
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) {
222                 switch (this.kind) {
223                     case Wildcard.EXTENDS :
224                         return this.bound.signature();
225                                 default: // SUPER | UNBOUND
226                                     return this.typeVariable().signature();
227                 }        
228                 }
229                 return this.signature;
230     }
231     
232     /* (non-Javadoc)
233      * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#sourceName()
234      */
235     public char[] sourceName() {
236         switch (this.kind) {
237             case Wildcard.UNBOUND : 
238                 return WILDCARD_NAME;
239             case Wildcard.EXTENDS :
240                 return CharOperation.concat(WILDCARD_NAME, WILDCARD_EXTENDS, this.bound.sourceName());
241                         default: // SUPER
242                             return CharOperation.concat(WILDCARD_NAME, WILDCARD_SUPER, this.bound.sourceName());
243         }        
244     }
245
246     /* (non-Javadoc)
247      * @see org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding#superclass()
248      */
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;
255                                 }
256                         }
257                         this.superclass = superType != null && superType.isClass()
258                                 ? (ReferenceBinding) superType
259                                 : environment.getType(JAVA_LANG_OBJECT);
260                 }
261
262                 return this.superclass;
263     }
264     /* (non-Javadoc)
265      * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#superInterfaces()
266      */
267     public ReferenceBinding[] superInterfaces() {
268         if (this.superInterfaces == null) {
269                 if (this.typeVariable() != null) {
270                         this.superInterfaces = this.typeVariable.superInterfaces();
271                 } else {
272                         this.superInterfaces = NoSuperInterfaces;
273                 }
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
280                                 }
281                         }
282         }
283         return this.superInterfaces;
284     }
285
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
290                         affected = true;
291                 } else if (this.bound == unresolvedType) {
292                         this.bound = resolvedType.isGenericType() ? env.createRawType(resolvedType, resolvedType.enclosingType()) : resolvedType;
293                         affected = true;
294                 }
295                 if (affected) 
296                         initialize(this.genericType, this.bound);
297         }
298
299         /**
300          * @see java.lang.Object#toString()
301          */
302         public String toString() {
303         switch (this.kind) {
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()));
308                         default: // SUPER
309                             return new String(CharOperation.concat(WILDCARD_NAME, WILDCARD_SUPER, this.bound.debugName().toCharArray()));
310         }        
311         }               
312         /**
313          * Returns associated type variable, or null in case of inconsistency
314          */
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];
320                 }
321                 return this.typeVariable;
322         }
323 }