author | jjg |
Fri, 29 Aug 2008 11:10:12 -0700 | |
changeset 1206 | 3a05355982a9 |
parent 938 | 13aae74ca013 |
child 1257 | 873b053bf757 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
735 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.javac.code; |
|
27 |
||
28 |
import com.sun.tools.javac.util.*; |
|
29 |
import com.sun.tools.javac.code.Symbol.*; |
|
30 |
||
31 |
import javax.lang.model.type.*; |
|
32 |
||
33 |
import static com.sun.tools.javac.code.Flags.*; |
|
34 |
import static com.sun.tools.javac.code.Kinds.*; |
|
35 |
import static com.sun.tools.javac.code.BoundKind.*; |
|
36 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
37 |
||
38 |
/** This class represents Java types. The class itself defines the behavior of |
|
39 |
* the following types: |
|
40 |
* <pre> |
|
41 |
* base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN), |
|
42 |
* type `void' (tag: VOID), |
|
43 |
* the bottom type (tag: BOT), |
|
44 |
* the missing type (tag: NONE). |
|
45 |
* </pre> |
|
46 |
* <p>The behavior of the following types is defined in subclasses, which are |
|
47 |
* all static inner classes of this class: |
|
48 |
* <pre> |
|
49 |
* class types (tag: CLASS, class: ClassType), |
|
50 |
* array types (tag: ARRAY, class: ArrayType), |
|
51 |
* method types (tag: METHOD, class: MethodType), |
|
52 |
* package types (tag: PACKAGE, class: PackageType), |
|
53 |
* type variables (tag: TYPEVAR, class: TypeVar), |
|
54 |
* type arguments (tag: WILDCARD, class: WildcardType), |
|
55 |
* polymorphic types (tag: FORALL, class: ForAll), |
|
56 |
* the error type (tag: ERROR, class: ErrorType). |
|
57 |
* </pre> |
|
58 |
* |
|
59 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
60 |
* you write code that depends on this, you do so at your own risk. |
|
61 |
* This code and its internal interfaces are subject to change or |
|
62 |
* deletion without notice.</b> |
|
63 |
* |
|
64 |
* @see TypeTags |
|
65 |
*/ |
|
66 |
public class Type implements PrimitiveType { |
|
67 |
||
68 |
/** Constant type: no type at all. */ |
|
69 |
public static final JCNoType noType = new JCNoType(NONE); |
|
70 |
||
71 |
/** If this switch is turned on, the names of type variables |
|
72 |
* and anonymous classes are printed with hashcodes appended. |
|
73 |
*/ |
|
74 |
public static boolean moreInfo = false; |
|
75 |
||
76 |
/** The tag of this type. |
|
77 |
* |
|
78 |
* @see TypeTags |
|
79 |
*/ |
|
80 |
public int tag; |
|
81 |
||
82 |
/** The defining class / interface / package / type variable |
|
83 |
*/ |
|
84 |
public TypeSymbol tsym; |
|
85 |
||
86 |
/** |
|
87 |
* The constant value of this type, null if this type does not |
|
88 |
* have a constant value attribute. Only primitive types and |
|
89 |
* strings (ClassType) can have a constant value attribute. |
|
90 |
* @return the constant value attribute of this type |
|
91 |
*/ |
|
92 |
public Object constValue() { |
|
93 |
return null; |
|
94 |
} |
|
95 |
||
96 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); } |
|
97 |
||
98 |
/** Define a type given its tag and type symbol |
|
99 |
*/ |
|
100 |
public Type(int tag, TypeSymbol tsym) { |
|
101 |
this.tag = tag; |
|
102 |
this.tsym = tsym; |
|
103 |
} |
|
104 |
||
105 |
/** An abstract class for mappings from types to types |
|
106 |
*/ |
|
107 |
public static abstract class Mapping { |
|
108 |
private String name; |
|
109 |
public Mapping(String name) { |
|
110 |
this.name = name; |
|
111 |
} |
|
112 |
public abstract Type apply(Type t); |
|
113 |
public String toString() { |
|
114 |
return name; |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
/** map a type function over all immediate descendants of this type |
|
119 |
*/ |
|
120 |
public Type map(Mapping f) { |
|
121 |
return this; |
|
122 |
} |
|
123 |
||
124 |
/** map a type function over a list of types |
|
125 |
*/ |
|
126 |
public static List<Type> map(List<Type> ts, Mapping f) { |
|
127 |
if (ts.nonEmpty()) { |
|
128 |
List<Type> tail1 = map(ts.tail, f); |
|
129 |
Type t = f.apply(ts.head); |
|
130 |
if (tail1 != ts.tail || t != ts.head) |
|
131 |
return tail1.prepend(t); |
|
132 |
} |
|
133 |
return ts; |
|
134 |
} |
|
135 |
||
136 |
/** Define a constant type, of the same kind as this type |
|
137 |
* and with given constant value |
|
138 |
*/ |
|
139 |
public Type constType(Object constValue) { |
|
140 |
final Object value = constValue; |
|
141 |
assert tag <= BOOLEAN; |
|
142 |
return new Type(tag, tsym) { |
|
143 |
@Override |
|
144 |
public Object constValue() { |
|
145 |
return value; |
|
146 |
} |
|
147 |
@Override |
|
148 |
public Type baseType() { |
|
149 |
return tsym.type; |
|
150 |
} |
|
151 |
}; |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* If this is a constant type, return its underlying type. |
|
156 |
* Otherwise, return the type itself. |
|
157 |
*/ |
|
158 |
public Type baseType() { |
|
159 |
return this; |
|
160 |
} |
|
161 |
||
162 |
/** Return the base types of a list of types. |
|
163 |
*/ |
|
164 |
public static List<Type> baseTypes(List<Type> ts) { |
|
165 |
if (ts.nonEmpty()) { |
|
166 |
Type t = ts.head.baseType(); |
|
167 |
List<Type> baseTypes = baseTypes(ts.tail); |
|
168 |
if (t != ts.head || baseTypes != ts.tail) |
|
169 |
return baseTypes.prepend(t); |
|
170 |
} |
|
171 |
return ts; |
|
172 |
} |
|
173 |
||
174 |
/** The Java source which this type represents. |
|
175 |
*/ |
|
176 |
public String toString() { |
|
177 |
String s = (tsym == null || tsym.name == null) |
|
178 |
? "<none>" |
|
179 |
: tsym.name.toString(); |
|
180 |
if (moreInfo && tag == TYPEVAR) s = s + hashCode(); |
|
181 |
return s; |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* The Java source which this type list represents. A List is |
|
186 |
* represented as a comma-spearated listing of the elements in |
|
187 |
* that list. |
|
188 |
*/ |
|
189 |
public static String toString(List<Type> ts) { |
|
190 |
if (ts.isEmpty()) { |
|
191 |
return ""; |
|
192 |
} else { |
|
193 |
StringBuffer buf = new StringBuffer(); |
|
194 |
buf.append(ts.head.toString()); |
|
195 |
for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail) |
|
196 |
buf.append(",").append(l.head.toString()); |
|
197 |
return buf.toString(); |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* The constant value of this type, converted to String |
|
203 |
*/ |
|
204 |
public String stringValue() { |
|
205 |
assert constValue() != null; |
|
206 |
if (tag == BOOLEAN) |
|
207 |
return ((Integer) constValue()).intValue() == 0 ? "false" : "true"; |
|
208 |
else if (tag == CHAR) |
|
209 |
return String.valueOf((char) ((Integer) constValue()).intValue()); |
|
210 |
else |
|
211 |
return constValue().toString(); |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* This method is analogous to isSameType, but weaker, since we |
|
216 |
* never complete classes. Where isSameType would complete a |
|
217 |
* class, equals assumes that the two types are different. |
|
218 |
*/ |
|
219 |
public boolean equals(Object t) { |
|
220 |
return super.equals(t); |
|
221 |
} |
|
222 |
||
223 |
public int hashCode() { |
|
224 |
return super.hashCode(); |
|
225 |
} |
|
226 |
||
227 |
/** Is this a constant type whose value is false? |
|
228 |
*/ |
|
229 |
public boolean isFalse() { |
|
230 |
return |
|
231 |
tag == BOOLEAN && |
|
232 |
constValue() != null && |
|
233 |
((Integer)constValue()).intValue() == 0; |
|
234 |
} |
|
235 |
||
236 |
/** Is this a constant type whose value is true? |
|
237 |
*/ |
|
238 |
public boolean isTrue() { |
|
239 |
return |
|
240 |
tag == BOOLEAN && |
|
241 |
constValue() != null && |
|
242 |
((Integer)constValue()).intValue() != 0; |
|
243 |
} |
|
244 |
||
245 |
public String argtypes(boolean varargs) { |
|
246 |
List<Type> args = getParameterTypes(); |
|
247 |
if (!varargs) return args.toString(); |
|
248 |
StringBuffer buf = new StringBuffer(); |
|
249 |
while (args.tail.nonEmpty()) { |
|
250 |
buf.append(args.head); |
|
251 |
args = args.tail; |
|
252 |
buf.append(','); |
|
253 |
} |
|
254 |
if (args.head.tag == ARRAY) { |
|
255 |
buf.append(((ArrayType)args.head).elemtype); |
|
256 |
buf.append("..."); |
|
257 |
} else { |
|
258 |
buf.append(args.head); |
|
259 |
} |
|
260 |
return buf.toString(); |
|
261 |
} |
|
262 |
||
263 |
/** Access methods. |
|
264 |
*/ |
|
265 |
public List<Type> getTypeArguments() { return List.nil(); } |
|
266 |
public Type getEnclosingType() { return null; } |
|
267 |
public List<Type> getParameterTypes() { return List.nil(); } |
|
268 |
public Type getReturnType() { return null; } |
|
269 |
public List<Type> getThrownTypes() { return List.nil(); } |
|
270 |
public Type getUpperBound() { return null; } |
|
271 |
public Type getLowerBound() { return null; } |
|
272 |
||
273 |
public void setThrown(List<Type> ts) { |
|
274 |
throw new AssertionError(); |
|
275 |
} |
|
276 |
||
277 |
/** Navigation methods, these will work for classes, type variables, |
|
278 |
* foralls, but will return null for arrays and methods. |
|
279 |
*/ |
|
280 |
||
281 |
/** Return all parameters of this type and all its outer types in order |
|
282 |
* outer (first) to inner (last). |
|
283 |
*/ |
|
284 |
public List<Type> allparams() { return List.nil(); } |
|
285 |
||
286 |
/** Does this type contain "error" elements? |
|
287 |
*/ |
|
288 |
public boolean isErroneous() { |
|
289 |
return false; |
|
290 |
} |
|
291 |
||
292 |
public static boolean isErroneous(List<Type> ts) { |
|
293 |
for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
|
294 |
if (l.head.isErroneous()) return true; |
|
295 |
return false; |
|
296 |
} |
|
297 |
||
298 |
/** Is this type parameterized? |
|
299 |
* A class type is parameterized if it has some parameters. |
|
300 |
* An array type is parameterized if its element type is parameterized. |
|
301 |
* All other types are not parameterized. |
|
302 |
*/ |
|
303 |
public boolean isParameterized() { |
|
304 |
return false; |
|
305 |
} |
|
306 |
||
307 |
/** Is this type a raw type? |
|
308 |
* A class type is a raw type if it misses some of its parameters. |
|
309 |
* An array type is a raw type if its element type is raw. |
|
310 |
* All other types are not raw. |
|
311 |
* Type validation will ensure that the only raw types |
|
312 |
* in a program are types that miss all their type variables. |
|
313 |
*/ |
|
314 |
public boolean isRaw() { |
|
315 |
return false; |
|
316 |
} |
|
317 |
||
318 |
public boolean isCompound() { |
|
319 |
return tsym.completer == null |
|
320 |
// Compound types can't have a completer. Calling |
|
321 |
// flags() will complete the symbol causing the |
|
322 |
// compiler to load classes unnecessarily. This led |
|
323 |
// to regression 6180021. |
|
324 |
&& (tsym.flags() & COMPOUND) != 0; |
|
325 |
} |
|
326 |
||
327 |
public boolean isInterface() { |
|
328 |
return (tsym.flags() & INTERFACE) != 0; |
|
329 |
} |
|
330 |
||
331 |
public boolean isPrimitive() { |
|
332 |
return tag < VOID; |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* Does this type contain occurrences of type t? |
|
337 |
*/ |
|
338 |
public boolean contains(Type t) { |
|
339 |
return t == this; |
|
340 |
} |
|
341 |
||
342 |
public static boolean contains(List<Type> ts, Type t) { |
|
343 |
for (List<Type> l = ts; |
|
344 |
l.tail != null /*inlined: l.nonEmpty()*/; |
|
345 |
l = l.tail) |
|
346 |
if (l.head.contains(t)) return true; |
|
347 |
return false; |
|
348 |
} |
|
349 |
||
350 |
/** Does this type contain an occurrence of some type in `elems'? |
|
351 |
*/ |
|
352 |
public boolean containsSome(List<Type> ts) { |
|
353 |
for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
|
354 |
if (this.contains(ts.head)) return true; |
|
355 |
return false; |
|
356 |
} |
|
357 |
||
358 |
public boolean isSuperBound() { return false; } |
|
359 |
public boolean isExtendsBound() { return false; } |
|
360 |
public boolean isUnbound() { return false; } |
|
361 |
public Type withTypeVar(Type t) { return this; } |
|
362 |
||
363 |
public static List<Type> removeBounds(List<Type> ts) { |
|
364 |
ListBuffer<Type> result = new ListBuffer<Type>(); |
|
365 |
for(;ts.nonEmpty(); ts = ts.tail) { |
|
366 |
result.append(ts.head.removeBounds()); |
|
367 |
} |
|
368 |
return result.toList(); |
|
369 |
} |
|
370 |
public Type removeBounds() { |
|
371 |
return this; |
|
372 |
} |
|
373 |
||
374 |
/** The underlying method type of this type. |
|
375 |
*/ |
|
376 |
public MethodType asMethodType() { throw new AssertionError(); } |
|
377 |
||
378 |
/** Complete loading all classes in this type. |
|
379 |
*/ |
|
380 |
public void complete() {} |
|
381 |
||
382 |
public Object clone() { |
|
383 |
try { |
|
384 |
return super.clone(); |
|
385 |
} catch (CloneNotSupportedException e) { |
|
386 |
throw new AssertionError(e); |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
public TypeSymbol asElement() { |
|
391 |
return tsym; |
|
392 |
} |
|
393 |
||
394 |
public TypeKind getKind() { |
|
395 |
switch (tag) { |
|
396 |
case BYTE: return TypeKind.BYTE; |
|
397 |
case CHAR: return TypeKind.CHAR; |
|
398 |
case SHORT: return TypeKind.SHORT; |
|
399 |
case INT: return TypeKind.INT; |
|
400 |
case LONG: return TypeKind.LONG; |
|
401 |
case FLOAT: return TypeKind.FLOAT; |
|
402 |
case DOUBLE: return TypeKind.DOUBLE; |
|
403 |
case BOOLEAN: return TypeKind.BOOLEAN; |
|
404 |
case VOID: return TypeKind.VOID; |
|
405 |
case BOT: return TypeKind.NULL; |
|
406 |
case NONE: return TypeKind.NONE; |
|
407 |
default: return TypeKind.OTHER; |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
412 |
if (isPrimitive()) |
|
413 |
return v.visitPrimitive(this, p); |
|
414 |
else |
|
415 |
throw new AssertionError(); |
|
416 |
} |
|
417 |
||
418 |
public static class WildcardType extends Type |
|
419 |
implements javax.lang.model.type.WildcardType { |
|
420 |
||
421 |
public Type type; |
|
422 |
public BoundKind kind; |
|
423 |
public TypeVar bound; |
|
424 |
||
425 |
@Override |
|
426 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
427 |
return v.visitWildcardType(this, s); |
|
428 |
} |
|
429 |
||
430 |
public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) { |
|
431 |
super(WILDCARD, tsym); |
|
432 |
assert(type != null); |
|
433 |
this.kind = kind; |
|
434 |
this.type = type; |
|
435 |
} |
|
436 |
public WildcardType(WildcardType t, TypeVar bound) { |
|
437 |
this(t.type, t.kind, t.tsym, bound); |
|
438 |
} |
|
439 |
||
440 |
public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) { |
|
441 |
this(type, kind, tsym); |
|
442 |
this.bound = bound; |
|
443 |
} |
|
444 |
||
445 |
public boolean isSuperBound() { |
|
446 |
return kind == SUPER || |
|
447 |
kind == UNBOUND; |
|
448 |
} |
|
449 |
public boolean isExtendsBound() { |
|
450 |
return kind == EXTENDS || |
|
451 |
kind == UNBOUND; |
|
452 |
} |
|
453 |
public boolean isUnbound() { |
|
454 |
return kind == UNBOUND; |
|
455 |
} |
|
456 |
||
457 |
public Type withTypeVar(Type t) { |
|
458 |
//-System.err.println(this+".withTypeVar("+t+");");//DEBUG |
|
459 |
if (bound == t) |
|
460 |
return this; |
|
461 |
bound = (TypeVar)t; |
|
462 |
return this; |
|
463 |
} |
|
464 |
||
465 |
boolean isPrintingBound = false; |
|
466 |
public String toString() { |
|
467 |
StringBuffer s = new StringBuffer(); |
|
468 |
s.append(kind.toString()); |
|
469 |
if (kind != UNBOUND) |
|
470 |
s.append(type); |
|
471 |
if (moreInfo && bound != null && !isPrintingBound) |
|
472 |
try { |
|
473 |
isPrintingBound = true; |
|
474 |
s.append("{:").append(bound.bound).append(":}"); |
|
475 |
} finally { |
|
476 |
isPrintingBound = false; |
|
477 |
} |
|
478 |
return s.toString(); |
|
479 |
} |
|
480 |
||
481 |
public Type map(Mapping f) { |
|
482 |
//- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG |
|
483 |
Type t = type; |
|
484 |
if (t != null) |
|
485 |
t = f.apply(t); |
|
486 |
if (t == type) |
|
487 |
return this; |
|
488 |
else |
|
489 |
return new WildcardType(t, kind, tsym, bound); |
|
490 |
} |
|
491 |
||
492 |
public Type removeBounds() { |
|
493 |
return isUnbound() ? this : type; |
|
494 |
} |
|
495 |
||
496 |
public Type getExtendsBound() { |
|
497 |
if (kind == EXTENDS) |
|
498 |
return type; |
|
499 |
else |
|
500 |
return null; |
|
501 |
} |
|
502 |
||
503 |
public Type getSuperBound() { |
|
504 |
if (kind == SUPER) |
|
505 |
return type; |
|
506 |
else |
|
507 |
return null; |
|
508 |
} |
|
509 |
||
510 |
public TypeKind getKind() { |
|
511 |
return TypeKind.WILDCARD; |
|
512 |
} |
|
513 |
||
514 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
515 |
return v.visitWildcard(this, p); |
|
516 |
} |
|
517 |
} |
|
518 |
||
519 |
public static class ClassType extends Type implements DeclaredType { |
|
520 |
||
521 |
/** The enclosing type of this type. If this is the type of an inner |
|
522 |
* class, outer_field refers to the type of its enclosing |
|
523 |
* instance class, in all other cases it referes to noType. |
|
524 |
*/ |
|
525 |
private Type outer_field; |
|
526 |
||
527 |
/** The type parameters of this type (to be set once class is loaded). |
|
528 |
*/ |
|
529 |
public List<Type> typarams_field; |
|
530 |
||
531 |
/** A cache variable for the type parameters of this type, |
|
532 |
* appended to all parameters of its enclosing class. |
|
533 |
* @see #allparams |
|
534 |
*/ |
|
535 |
public List<Type> allparams_field; |
|
536 |
||
537 |
/** The supertype of this class (to be set once class is loaded). |
|
538 |
*/ |
|
539 |
public Type supertype_field; |
|
540 |
||
541 |
/** The interfaces of this class (to be set once class is loaded). |
|
542 |
*/ |
|
543 |
public List<Type> interfaces_field; |
|
544 |
||
545 |
public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) { |
|
546 |
super(CLASS, tsym); |
|
547 |
this.outer_field = outer; |
|
548 |
this.typarams_field = typarams; |
|
549 |
this.allparams_field = null; |
|
550 |
this.supertype_field = null; |
|
551 |
this.interfaces_field = null; |
|
552 |
/* |
|
553 |
// this can happen during error recovery |
|
554 |
assert |
|
555 |
outer.isParameterized() ? |
|
556 |
typarams.length() == tsym.type.typarams().length() : |
|
557 |
outer.isRaw() ? |
|
558 |
typarams.length() == 0 : |
|
559 |
true; |
|
560 |
*/ |
|
561 |
} |
|
562 |
||
563 |
@Override |
|
564 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
565 |
return v.visitClassType(this, s); |
|
566 |
} |
|
567 |
||
568 |
public Type constType(Object constValue) { |
|
569 |
final Object value = constValue; |
|
570 |
return new ClassType(getEnclosingType(), typarams_field, tsym) { |
|
571 |
@Override |
|
572 |
public Object constValue() { |
|
573 |
return value; |
|
574 |
} |
|
575 |
@Override |
|
576 |
public Type baseType() { |
|
577 |
return tsym.type; |
|
578 |
} |
|
579 |
}; |
|
580 |
} |
|
581 |
||
582 |
/** The Java source which this type represents. |
|
583 |
*/ |
|
584 |
public String toString() { |
|
585 |
StringBuffer buf = new StringBuffer(); |
|
586 |
if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) { |
|
587 |
buf.append(getEnclosingType().toString()); |
|
588 |
buf.append("."); |
|
589 |
buf.append(className(tsym, false)); |
|
590 |
} else { |
|
591 |
buf.append(className(tsym, true)); |
|
592 |
} |
|
593 |
if (getTypeArguments().nonEmpty()) { |
|
594 |
buf.append('<'); |
|
595 |
buf.append(getTypeArguments().toString()); |
|
596 |
buf.append(">"); |
|
597 |
} |
|
598 |
return buf.toString(); |
|
599 |
} |
|
600 |
//where |
|
601 |
private String className(Symbol sym, boolean longform) { |
|
602 |
if (sym.name.len == 0 && (sym.flags() & COMPOUND) != 0) { |
|
603 |
StringBuffer s = new StringBuffer(supertype_field.toString()); |
|
604 |
for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) { |
|
605 |
s.append("&"); |
|
606 |
s.append(is.head.toString()); |
|
607 |
} |
|
608 |
return s.toString(); |
|
609 |
} else if (sym.name.len == 0) { |
|
610 |
String s; |
|
611 |
ClassType norm = (ClassType) tsym.type; |
|
612 |
if (norm == null) { |
|
613 |
s = Log.getLocalizedString("anonymous.class", (Object)null); |
|
614 |
} else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) { |
|
615 |
s = Log.getLocalizedString("anonymous.class", |
|
616 |
norm.interfaces_field.head); |
|
617 |
} else { |
|
618 |
s = Log.getLocalizedString("anonymous.class", |
|
619 |
norm.supertype_field); |
|
620 |
} |
|
621 |
if (moreInfo) |
|
622 |
s += String.valueOf(sym.hashCode()); |
|
623 |
return s; |
|
624 |
} else if (longform) { |
|
625 |
return sym.getQualifiedName().toString(); |
|
626 |
} else { |
|
627 |
return sym.name.toString(); |
|
628 |
} |
|
629 |
} |
|
630 |
||
631 |
public List<Type> getTypeArguments() { |
|
632 |
if (typarams_field == null) { |
|
633 |
complete(); |
|
634 |
if (typarams_field == null) |
|
635 |
typarams_field = List.nil(); |
|
636 |
} |
|
637 |
return typarams_field; |
|
638 |
} |
|
639 |
||
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
640 |
public boolean hasErasedSupertypes() { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
641 |
return isRaw(); |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
642 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
643 |
|
10 | 644 |
public Type getEnclosingType() { |
645 |
return outer_field; |
|
646 |
} |
|
647 |
||
648 |
public void setEnclosingType(Type outer) { |
|
649 |
outer_field = outer; |
|
650 |
} |
|
651 |
||
652 |
public List<Type> allparams() { |
|
653 |
if (allparams_field == null) { |
|
654 |
allparams_field = getTypeArguments().prependList(getEnclosingType().allparams()); |
|
655 |
} |
|
656 |
return allparams_field; |
|
657 |
} |
|
658 |
||
659 |
public boolean isErroneous() { |
|
660 |
return |
|
661 |
getEnclosingType().isErroneous() || |
|
662 |
isErroneous(getTypeArguments()) || |
|
663 |
this != tsym.type && tsym.type.isErroneous(); |
|
664 |
} |
|
665 |
||
666 |
public boolean isParameterized() { |
|
667 |
return allparams().tail != null; |
|
668 |
// optimization, was: allparams().nonEmpty(); |
|
669 |
} |
|
670 |
||
671 |
/** A cache for the rank. */ |
|
672 |
int rank_field = -1; |
|
673 |
||
674 |
/** A class type is raw if it misses some |
|
675 |
* of its type parameter sections. |
|
676 |
* After validation, this is equivalent to: |
|
677 |
* allparams.isEmpty() && tsym.type.allparams.nonEmpty(); |
|
678 |
*/ |
|
679 |
public boolean isRaw() { |
|
680 |
return |
|
681 |
this != tsym.type && // necessary, but not sufficient condition |
|
682 |
tsym.type.allparams().nonEmpty() && |
|
683 |
allparams().isEmpty(); |
|
684 |
} |
|
685 |
||
686 |
public Type map(Mapping f) { |
|
687 |
Type outer = getEnclosingType(); |
|
688 |
Type outer1 = f.apply(outer); |
|
689 |
List<Type> typarams = getTypeArguments(); |
|
690 |
List<Type> typarams1 = map(typarams, f); |
|
691 |
if (outer1 == outer && typarams1 == typarams) return this; |
|
692 |
else return new ClassType(outer1, typarams1, tsym); |
|
693 |
} |
|
694 |
||
695 |
public boolean contains(Type elem) { |
|
696 |
return |
|
697 |
elem == this |
|
698 |
|| (isParameterized() |
|
699 |
&& (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem))); |
|
700 |
} |
|
701 |
||
702 |
public void complete() { |
|
703 |
if (tsym.completer != null) tsym.complete(); |
|
704 |
} |
|
705 |
||
706 |
public TypeKind getKind() { |
|
707 |
return TypeKind.DECLARED; |
|
708 |
} |
|
709 |
||
710 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
711 |
return v.visitDeclared(this, p); |
|
712 |
} |
|
713 |
} |
|
714 |
||
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
715 |
public static class ErasedClassType extends ClassType { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
716 |
public ErasedClassType(Type outer, TypeSymbol tsym) { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
717 |
super(outer, List.<Type>nil(), tsym); |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
718 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
719 |
|
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
720 |
@Override |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
721 |
public boolean hasErasedSupertypes() { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
722 |
return true; |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
723 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
724 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
725 |
|
10 | 726 |
public static class ArrayType extends Type |
727 |
implements javax.lang.model.type.ArrayType { |
|
728 |
||
729 |
public Type elemtype; |
|
730 |
||
731 |
public ArrayType(Type elemtype, TypeSymbol arrayClass) { |
|
732 |
super(ARRAY, arrayClass); |
|
733 |
this.elemtype = elemtype; |
|
734 |
} |
|
735 |
||
736 |
@Override |
|
737 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
738 |
return v.visitArrayType(this, s); |
|
739 |
} |
|
740 |
||
741 |
public String toString() { |
|
742 |
return elemtype + "[]"; |
|
743 |
} |
|
744 |
||
745 |
public boolean equals(Object obj) { |
|
746 |
return |
|
747 |
this == obj || |
|
748 |
(obj instanceof ArrayType && |
|
749 |
this.elemtype.equals(((ArrayType)obj).elemtype)); |
|
750 |
} |
|
751 |
||
752 |
public int hashCode() { |
|
753 |
return (ARRAY << 5) + elemtype.hashCode(); |
|
754 |
} |
|
755 |
||
756 |
public List<Type> allparams() { return elemtype.allparams(); } |
|
757 |
||
758 |
public boolean isErroneous() { |
|
759 |
return elemtype.isErroneous(); |
|
760 |
} |
|
761 |
||
762 |
public boolean isParameterized() { |
|
763 |
return elemtype.isParameterized(); |
|
764 |
} |
|
765 |
||
766 |
public boolean isRaw() { |
|
767 |
return elemtype.isRaw(); |
|
768 |
} |
|
769 |
||
770 |
public Type map(Mapping f) { |
|
771 |
Type elemtype1 = f.apply(elemtype); |
|
772 |
if (elemtype1 == elemtype) return this; |
|
773 |
else return new ArrayType(elemtype1, tsym); |
|
774 |
} |
|
775 |
||
776 |
public boolean contains(Type elem) { |
|
777 |
return elem == this || elemtype.contains(elem); |
|
778 |
} |
|
779 |
||
780 |
public void complete() { |
|
781 |
elemtype.complete(); |
|
782 |
} |
|
783 |
||
784 |
public Type getComponentType() { |
|
785 |
return elemtype; |
|
786 |
} |
|
787 |
||
788 |
public TypeKind getKind() { |
|
789 |
return TypeKind.ARRAY; |
|
790 |
} |
|
791 |
||
792 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
793 |
return v.visitArray(this, p); |
|
794 |
} |
|
795 |
} |
|
796 |
||
797 |
public static class MethodType extends Type |
|
798 |
implements Cloneable, ExecutableType { |
|
799 |
||
800 |
public List<Type> argtypes; |
|
801 |
public Type restype; |
|
802 |
public List<Type> thrown; |
|
803 |
||
804 |
public MethodType(List<Type> argtypes, |
|
805 |
Type restype, |
|
806 |
List<Type> thrown, |
|
807 |
TypeSymbol methodClass) { |
|
808 |
super(METHOD, methodClass); |
|
809 |
this.argtypes = argtypes; |
|
810 |
this.restype = restype; |
|
811 |
this.thrown = thrown; |
|
812 |
} |
|
813 |
||
814 |
@Override |
|
815 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
816 |
return v.visitMethodType(this, s); |
|
817 |
} |
|
818 |
||
819 |
/** The Java source which this type represents. |
|
820 |
* |
|
821 |
* XXX 06/09/99 iris This isn't correct Java syntax, but it probably |
|
822 |
* should be. |
|
823 |
*/ |
|
824 |
public String toString() { |
|
825 |
return "(" + argtypes + ")" + restype; |
|
826 |
} |
|
827 |
||
828 |
public boolean equals(Object obj) { |
|
829 |
if (this == obj) |
|
830 |
return true; |
|
831 |
if (!(obj instanceof MethodType)) |
|
832 |
return false; |
|
833 |
MethodType m = (MethodType)obj; |
|
834 |
List<Type> args1 = argtypes; |
|
835 |
List<Type> args2 = m.argtypes; |
|
836 |
while (!args1.isEmpty() && !args2.isEmpty()) { |
|
837 |
if (!args1.head.equals(args2.head)) |
|
838 |
return false; |
|
839 |
args1 = args1.tail; |
|
840 |
args2 = args2.tail; |
|
841 |
} |
|
842 |
if (!args1.isEmpty() || !args2.isEmpty()) |
|
843 |
return false; |
|
844 |
return restype.equals(m.restype); |
|
845 |
} |
|
846 |
||
847 |
public int hashCode() { |
|
848 |
int h = METHOD; |
|
849 |
for (List<Type> thisargs = this.argtypes; |
|
850 |
thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/ |
|
851 |
thisargs = thisargs.tail) |
|
852 |
h = (h << 5) + thisargs.head.hashCode(); |
|
853 |
return (h << 5) + this.restype.hashCode(); |
|
854 |
} |
|
855 |
||
856 |
public List<Type> getParameterTypes() { return argtypes; } |
|
857 |
public Type getReturnType() { return restype; } |
|
858 |
public List<Type> getThrownTypes() { return thrown; } |
|
859 |
||
860 |
public void setThrown(List<Type> t) { |
|
861 |
thrown = t; |
|
862 |
} |
|
863 |
||
864 |
public boolean isErroneous() { |
|
865 |
return |
|
866 |
isErroneous(argtypes) || |
|
867 |
restype != null && restype.isErroneous(); |
|
868 |
} |
|
869 |
||
870 |
public Type map(Mapping f) { |
|
871 |
List<Type> argtypes1 = map(argtypes, f); |
|
872 |
Type restype1 = f.apply(restype); |
|
873 |
List<Type> thrown1 = map(thrown, f); |
|
874 |
if (argtypes1 == argtypes && |
|
875 |
restype1 == restype && |
|
876 |
thrown1 == thrown) return this; |
|
877 |
else return new MethodType(argtypes1, restype1, thrown1, tsym); |
|
878 |
} |
|
879 |
||
880 |
public boolean contains(Type elem) { |
|
881 |
return elem == this || contains(argtypes, elem) || restype.contains(elem); |
|
882 |
} |
|
883 |
||
884 |
public MethodType asMethodType() { return this; } |
|
885 |
||
886 |
public void complete() { |
|
887 |
for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail) |
|
888 |
l.head.complete(); |
|
889 |
restype.complete(); |
|
890 |
for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) |
|
891 |
l.head.complete(); |
|
892 |
} |
|
893 |
||
894 |
public List<TypeVar> getTypeVariables() { |
|
895 |
return List.nil(); |
|
896 |
} |
|
897 |
||
898 |
public TypeSymbol asElement() { |
|
899 |
return null; |
|
900 |
} |
|
901 |
||
902 |
public TypeKind getKind() { |
|
903 |
return TypeKind.EXECUTABLE; |
|
904 |
} |
|
905 |
||
906 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
907 |
return v.visitExecutable(this, p); |
|
908 |
} |
|
909 |
} |
|
910 |
||
911 |
public static class PackageType extends Type implements NoType { |
|
912 |
||
913 |
PackageType(TypeSymbol tsym) { |
|
914 |
super(PACKAGE, tsym); |
|
915 |
} |
|
916 |
||
917 |
@Override |
|
918 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
919 |
return v.visitPackageType(this, s); |
|
920 |
} |
|
921 |
||
922 |
public String toString() { |
|
923 |
return tsym.getQualifiedName().toString(); |
|
924 |
} |
|
925 |
||
926 |
public TypeKind getKind() { |
|
927 |
return TypeKind.PACKAGE; |
|
928 |
} |
|
929 |
||
930 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
931 |
return v.visitNoType(this, p); |
|
932 |
} |
|
933 |
} |
|
934 |
||
935 |
public static class TypeVar extends Type implements TypeVariable { |
|
936 |
||
937 |
/** The bound of this type variable; set from outside. |
|
938 |
* Must be nonempty once it is set. |
|
939 |
* For a bound, `bound' is the bound type itself. |
|
940 |
* Multiple bounds are expressed as a single class type which has the |
|
941 |
* individual bounds as superclass, respectively interfaces. |
|
942 |
* The class type then has as `tsym' a compiler generated class `c', |
|
943 |
* which has a flag COMPOUND and whose owner is the type variable |
|
944 |
* itself. Furthermore, the erasure_field of the class |
|
945 |
* points to the first class or interface bound. |
|
946 |
*/ |
|
947 |
public Type bound = null; |
|
948 |
public Type lower; |
|
949 |
||
950 |
public TypeVar(Name name, Symbol owner, Type lower) { |
|
951 |
super(TYPEVAR, null); |
|
952 |
tsym = new TypeSymbol(0, name, this, owner); |
|
953 |
this.lower = lower; |
|
954 |
} |
|
955 |
||
956 |
public TypeVar(TypeSymbol tsym, Type bound, Type lower) { |
|
957 |
super(TYPEVAR, tsym); |
|
958 |
this.bound = bound; |
|
959 |
this.lower = lower; |
|
960 |
} |
|
961 |
||
962 |
@Override |
|
963 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
964 |
return v.visitTypeVar(this, s); |
|
965 |
} |
|
966 |
||
967 |
public Type getUpperBound() { return bound; } |
|
968 |
||
969 |
int rank_field = -1; |
|
970 |
||
971 |
public Type getLowerBound() { |
|
972 |
return lower; |
|
973 |
} |
|
974 |
||
975 |
public TypeKind getKind() { |
|
976 |
return TypeKind.TYPEVAR; |
|
977 |
} |
|
978 |
||
938
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
979 |
public boolean isCaptured() { |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
980 |
return false; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
981 |
} |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
982 |
|
10 | 983 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
984 |
return v.visitTypeVariable(this, p); |
|
985 |
} |
|
986 |
} |
|
987 |
||
988 |
/** A captured type variable comes from wildcards which can have |
|
989 |
* both upper and lower bound. CapturedType extends TypeVar with |
|
990 |
* a lower bound. |
|
991 |
*/ |
|
992 |
public static class CapturedType extends TypeVar { |
|
993 |
||
994 |
public Type lower; |
|
995 |
public WildcardType wildcard; |
|
996 |
||
997 |
public CapturedType(Name name, |
|
998 |
Symbol owner, |
|
999 |
Type upper, |
|
1000 |
Type lower, |
|
1001 |
WildcardType wildcard) { |
|
1002 |
super(name, owner, lower); |
|
1003 |
assert lower != null; |
|
1004 |
this.bound = upper; |
|
1005 |
this.lower = lower; |
|
1006 |
this.wildcard = wildcard; |
|
1007 |
} |
|
1008 |
||
1009 |
@Override |
|
1010 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1011 |
return v.visitCapturedType(this, s); |
|
1012 |
} |
|
1013 |
||
1014 |
public Type getLowerBound() { |
|
1015 |
return lower; |
|
1016 |
} |
|
1017 |
||
1018 |
@Override |
|
938
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1019 |
public boolean isCaptured() { |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1020 |
return true; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1021 |
} |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1022 |
|
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1023 |
@Override |
10 | 1024 |
public String toString() { |
1025 |
return "capture#" |
|
1026 |
+ (hashCode() & 0xFFFFFFFFL) % PRIME |
|
1027 |
+ " of " |
|
1028 |
+ wildcard; |
|
1029 |
} |
|
1030 |
static final int PRIME = 997; // largest prime less than 1000 |
|
1031 |
} |
|
1032 |
||
1033 |
public static abstract class DelegatedType extends Type { |
|
1034 |
public Type qtype; |
|
1035 |
public DelegatedType(int tag, Type qtype) { |
|
1036 |
super(tag, qtype.tsym); |
|
1037 |
this.qtype = qtype; |
|
1038 |
} |
|
1039 |
public String toString() { return qtype.toString(); } |
|
1040 |
public List<Type> getTypeArguments() { return qtype.getTypeArguments(); } |
|
1041 |
public Type getEnclosingType() { return qtype.getEnclosingType(); } |
|
1042 |
public List<Type> getParameterTypes() { return qtype.getParameterTypes(); } |
|
1043 |
public Type getReturnType() { return qtype.getReturnType(); } |
|
1044 |
public List<Type> getThrownTypes() { return qtype.getThrownTypes(); } |
|
1045 |
public List<Type> allparams() { return qtype.allparams(); } |
|
1046 |
public Type getUpperBound() { return qtype.getUpperBound(); } |
|
1047 |
public Object clone() { DelegatedType t = (DelegatedType)super.clone(); t.qtype = (Type)qtype.clone(); return t; } |
|
1048 |
public boolean isErroneous() { return qtype.isErroneous(); } |
|
1049 |
} |
|
1050 |
||
1051 |
public static class ForAll extends DelegatedType |
|
1052 |
implements Cloneable, ExecutableType { |
|
1053 |
public List<Type> tvars; |
|
1054 |
||
1055 |
public ForAll(List<Type> tvars, Type qtype) { |
|
1056 |
super(FORALL, qtype); |
|
1057 |
this.tvars = tvars; |
|
1058 |
} |
|
1059 |
||
1060 |
@Override |
|
1061 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1062 |
return v.visitForAll(this, s); |
|
1063 |
} |
|
1064 |
||
1065 |
public String toString() { |
|
1066 |
return "<" + tvars + ">" + qtype; |
|
1067 |
} |
|
1068 |
||
1069 |
public List<Type> getTypeArguments() { return tvars; } |
|
1070 |
||
1071 |
public void setThrown(List<Type> t) { |
|
1072 |
qtype.setThrown(t); |
|
1073 |
} |
|
1074 |
||
1075 |
public Object clone() { |
|
1076 |
ForAll result = (ForAll)super.clone(); |
|
1077 |
result.qtype = (Type)result.qtype.clone(); |
|
1078 |
return result; |
|
1079 |
} |
|
1080 |
||
1081 |
public boolean isErroneous() { |
|
1082 |
return qtype.isErroneous(); |
|
1083 |
} |
|
1084 |
||
1085 |
public Type map(Mapping f) { |
|
1086 |
return f.apply(qtype); |
|
1087 |
} |
|
1088 |
||
1089 |
public boolean contains(Type elem) { |
|
1090 |
return qtype.contains(elem); |
|
1091 |
} |
|
1092 |
||
1093 |
public MethodType asMethodType() { |
|
1094 |
return qtype.asMethodType(); |
|
1095 |
} |
|
1096 |
||
1097 |
public void complete() { |
|
1098 |
for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) { |
|
1099 |
((TypeVar)l.head).bound.complete(); |
|
1100 |
} |
|
1101 |
qtype.complete(); |
|
1102 |
} |
|
1103 |
||
1104 |
public List<TypeVar> getTypeVariables() { |
|
1105 |
return List.convert(TypeVar.class, getTypeArguments()); |
|
1106 |
} |
|
1107 |
||
1108 |
public TypeKind getKind() { |
|
1109 |
return TypeKind.EXECUTABLE; |
|
1110 |
} |
|
1111 |
||
1112 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1113 |
return v.visitExecutable(this, p); |
|
1114 |
} |
|
1115 |
} |
|
1116 |
||
1117 |
/** A class for instantiatable variables, for use during type |
|
1118 |
* inference. |
|
1119 |
*/ |
|
1120 |
public static class UndetVar extends DelegatedType { |
|
1121 |
public List<Type> lobounds = List.nil(); |
|
1122 |
public List<Type> hibounds = List.nil(); |
|
1123 |
public Type inst = null; |
|
1124 |
||
1125 |
@Override |
|
1126 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1127 |
return v.visitUndetVar(this, s); |
|
1128 |
} |
|
1129 |
||
1130 |
public UndetVar(Type origin) { |
|
1131 |
super(UNDETVAR, origin); |
|
1132 |
} |
|
1133 |
||
1134 |
public String toString() { |
|
1135 |
if (inst != null) return inst.toString(); |
|
1136 |
else return qtype + "?"; |
|
1137 |
} |
|
1138 |
||
1139 |
public Type baseType() { |
|
1140 |
if (inst != null) return inst.baseType(); |
|
1141 |
else return this; |
|
1142 |
} |
|
1143 |
} |
|
1144 |
||
1145 |
/** Represents VOID or NONE. |
|
1146 |
*/ |
|
1147 |
static class JCNoType extends Type implements NoType { |
|
1148 |
public JCNoType(int tag) { |
|
1149 |
super(tag, null); |
|
1150 |
} |
|
1151 |
||
1152 |
@Override |
|
1153 |
public TypeKind getKind() { |
|
1154 |
switch (tag) { |
|
1155 |
case VOID: return TypeKind.VOID; |
|
1156 |
case NONE: return TypeKind.NONE; |
|
1157 |
default: |
|
1158 |
throw new AssertionError("Unexpected tag: " + tag); |
|
1159 |
} |
|
1160 |
} |
|
1161 |
||
1162 |
@Override |
|
1163 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1164 |
return v.visitNoType(this, p); |
|
1165 |
} |
|
1166 |
} |
|
1167 |
||
1168 |
static class BottomType extends Type implements NullType { |
|
1169 |
public BottomType() { |
|
1170 |
super(TypeTags.BOT, null); |
|
1171 |
} |
|
1172 |
||
1173 |
@Override |
|
1174 |
public TypeKind getKind() { |
|
1175 |
return TypeKind.NULL; |
|
1176 |
} |
|
1177 |
||
1178 |
@Override |
|
1179 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1180 |
return v.visitNull(this, p); |
|
1181 |
} |
|
1182 |
||
1183 |
@Override |
|
1184 |
public Type constType(Object value) { |
|
1185 |
return this; |
|
1186 |
} |
|
1187 |
||
1188 |
@Override |
|
1189 |
public String stringValue() { |
|
1190 |
return "null"; |
|
1191 |
} |
|
1192 |
} |
|
1193 |
||
1194 |
public static class ErrorType extends ClassType |
|
1195 |
implements javax.lang.model.type.ErrorType { |
|
1196 |
||
1197 |
public ErrorType() { |
|
1198 |
super(noType, List.<Type>nil(), null); |
|
1199 |
tag = ERROR; |
|
1200 |
} |
|
1201 |
||
1202 |
public ErrorType(ClassSymbol c) { |
|
1203 |
this(); |
|
1204 |
tsym = c; |
|
1205 |
c.type = this; |
|
1206 |
c.kind = ERR; |
|
1207 |
c.members_field = new Scope.ErrorScope(c); |
|
1208 |
} |
|
1209 |
||
1210 |
public ErrorType(Name name, TypeSymbol container) { |
|
1211 |
this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container)); |
|
1212 |
} |
|
1213 |
||
1214 |
@Override |
|
1215 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1216 |
return v.visitErrorType(this, s); |
|
1217 |
} |
|
1218 |
||
1219 |
public Type constType(Object constValue) { return this; } |
|
1220 |
public Type getEnclosingType() { return this; } |
|
1221 |
public Type getReturnType() { return this; } |
|
1222 |
public Type asSub(Symbol sym) { return this; } |
|
1223 |
public Type map(Mapping f) { return this; } |
|
1224 |
||
1225 |
public boolean isGenType(Type t) { return true; } |
|
1226 |
public boolean isErroneous() { return true; } |
|
1227 |
public boolean isCompound() { return false; } |
|
1228 |
public boolean isInterface() { return false; } |
|
1229 |
||
1230 |
public List<Type> allparams() { return List.nil(); } |
|
1231 |
public List<Type> getTypeArguments() { return List.nil(); } |
|
1232 |
||
1233 |
public TypeKind getKind() { |
|
1234 |
return TypeKind.ERROR; |
|
1235 |
} |
|
1236 |
||
1237 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1238 |
return v.visitError(this, p); |
|
1239 |
} |
|
1240 |
} |
|
1241 |
||
1242 |
/** |
|
1243 |
* A visitor for types. A visitor is used to implement operations |
|
1244 |
* (or relations) on types. Most common operations on types are |
|
1245 |
* binary relations and this interface is designed for binary |
|
1246 |
* relations, that is, operations on the form |
|
1247 |
* Type × S → R. |
|
1248 |
* <!-- In plain text: Type x S -> R --> |
|
1249 |
* |
|
1250 |
* @param <R> the return type of the operation implemented by this |
|
1251 |
* visitor; use Void if no return type is needed. |
|
1252 |
* @param <S> the type of the second argument (the first being the |
|
1253 |
* type itself) of the operation implemented by this visitor; use |
|
1254 |
* Void if a second argument is not needed. |
|
1255 |
*/ |
|
1256 |
public interface Visitor<R,S> { |
|
1257 |
R visitClassType(ClassType t, S s); |
|
1258 |
R visitWildcardType(WildcardType t, S s); |
|
1259 |
R visitArrayType(ArrayType t, S s); |
|
1260 |
R visitMethodType(MethodType t, S s); |
|
1261 |
R visitPackageType(PackageType t, S s); |
|
1262 |
R visitTypeVar(TypeVar t, S s); |
|
1263 |
R visitCapturedType(CapturedType t, S s); |
|
1264 |
R visitForAll(ForAll t, S s); |
|
1265 |
R visitUndetVar(UndetVar t, S s); |
|
1266 |
R visitErrorType(ErrorType t, S s); |
|
1267 |
R visitType(Type t, S s); |
|
1268 |
} |
|
1269 |
} |