src/share/classes/com/sun/tools/javac/code/Type.java

Thu, 04 Jul 2013 10:41:08 +0100

author
vromero
date
Thu, 04 Jul 2013 10:41:08 +0100
changeset 1886
79c3146e417b
parent 1853
831467c4c6a7
child 1889
b0386f0dc28e
permissions
-rw-r--r--

6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
Reviewed-by: mcimadamore

duke@1 1 /*
jjg@1521 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.code;
duke@1 27
jjg@1645 28 import java.lang.annotation.Annotation;
jjg@988 29 import java.util.Collections;
mcimadamore@1338 30 import java.util.EnumMap;
mcimadamore@1338 31 import java.util.EnumSet;
mcimadamore@1338 32 import java.util.Map;
mcimadamore@1338 33 import java.util.Set;
mcimadamore@1342 34
duke@1 35 import javax.lang.model.type.*;
duke@1 36
jjg@1357 37 import com.sun.tools.javac.code.Symbol.*;
jjg@1755 38 import com.sun.tools.javac.model.JavacAnnoConstructs;
jjg@1357 39 import com.sun.tools.javac.util.*;
mcimadamore@1342 40 import static com.sun.tools.javac.code.BoundKind.*;
duke@1 41 import static com.sun.tools.javac.code.Flags.*;
duke@1 42 import static com.sun.tools.javac.code.Kinds.*;
jjg@1374 43 import static com.sun.tools.javac.code.TypeTag.*;
duke@1 44
duke@1 45 /** This class represents Java types. The class itself defines the behavior of
duke@1 46 * the following types:
duke@1 47 * <pre>
duke@1 48 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
duke@1 49 * type `void' (tag: VOID),
duke@1 50 * the bottom type (tag: BOT),
duke@1 51 * the missing type (tag: NONE).
duke@1 52 * </pre>
duke@1 53 * <p>The behavior of the following types is defined in subclasses, which are
duke@1 54 * all static inner classes of this class:
duke@1 55 * <pre>
duke@1 56 * class types (tag: CLASS, class: ClassType),
duke@1 57 * array types (tag: ARRAY, class: ArrayType),
duke@1 58 * method types (tag: METHOD, class: MethodType),
duke@1 59 * package types (tag: PACKAGE, class: PackageType),
duke@1 60 * type variables (tag: TYPEVAR, class: TypeVar),
duke@1 61 * type arguments (tag: WILDCARD, class: WildcardType),
mcimadamore@1268 62 * generic method types (tag: FORALL, class: ForAll),
duke@1 63 * the error type (tag: ERROR, class: ErrorType).
duke@1 64 * </pre>
duke@1 65 *
jjg@581 66 * <p><b>This is NOT part of any supported API.
jjg@581 67 * If you write code that depends on this, you do so at your own risk.
duke@1 68 * This code and its internal interfaces are subject to change or
duke@1 69 * deletion without notice.</b>
duke@1 70 *
jjg@1374 71 * @see TypeTag
duke@1 72 */
vromero@1853 73 public abstract class Type implements TypeMirror {
duke@1 74
duke@1 75 /** Constant type: no type at all. */
vromero@1853 76 public static final JCNoType noType = new JCNoType();
duke@1 77
mcimadamore@1347 78 /** Constant type: special type to be used during recovery of deferred expressions. */
vromero@1853 79 public static final JCNoType recoveryType = new JCNoType();
mcimadamore@1347 80
duke@1 81 /** If this switch is turned on, the names of type variables
duke@1 82 * and anonymous classes are printed with hashcodes appended.
duke@1 83 */
duke@1 84 public static boolean moreInfo = false;
duke@1 85
jjg@1521 86 /** The defining class / interface / package / type variable.
duke@1 87 */
duke@1 88 public TypeSymbol tsym;
duke@1 89
duke@1 90 /**
jjg@1374 91 * Checks if the current type tag is equal to the given tag.
jjg@1374 92 * @return true if tag is equal to the current type tag.
jjg@1374 93 */
jjg@1374 94 public boolean hasTag(TypeTag tag) {
vromero@1853 95 return tag == getTag();
jjg@1374 96 }
jjg@1374 97
jjg@1374 98 /**
jjg@1374 99 * Returns the current type tag.
jjg@1374 100 * @return the value of the current type tag.
jjg@1374 101 */
vromero@1853 102 public abstract TypeTag getTag();
jjg@1374 103
jjg@1374 104 public boolean isNumeric() {
vromero@1853 105 return false;
jjg@1374 106 }
jjg@1374 107
jjg@1374 108 public boolean isPrimitive() {
vromero@1853 109 return false;
jjg@1374 110 }
jjg@1374 111
jjg@1374 112 public boolean isPrimitiveOrVoid() {
vromero@1853 113 return false;
jjg@1374 114 }
jjg@1374 115
jjg@1374 116 public boolean isReference() {
vromero@1853 117 return false;
jjg@1374 118 }
jjg@1374 119
jjg@1374 120 public boolean isNullOrReference() {
vromero@1853 121 return false;
jjg@1374 122 }
jjg@1374 123
jjg@1374 124 public boolean isPartial() {
vromero@1853 125 return false;
jjg@1374 126 }
jjg@1374 127
jjg@1374 128 /**
duke@1 129 * The constant value of this type, null if this type does not
duke@1 130 * have a constant value attribute. Only primitive types and
duke@1 131 * strings (ClassType) can have a constant value attribute.
duke@1 132 * @return the constant value attribute of this type
duke@1 133 */
duke@1 134 public Object constValue() {
duke@1 135 return null;
duke@1 136 }
duke@1 137
vromero@1853 138 /** Is this a constant type whose value is false?
vromero@1853 139 */
vromero@1853 140 public boolean isFalse() {
vromero@1853 141 return false;
vromero@1853 142 }
vromero@1853 143
vromero@1853 144 /** Is this a constant type whose value is true?
vromero@1853 145 */
vromero@1853 146 public boolean isTrue() {
vromero@1853 147 return false;
vromero@1853 148 }
vromero@1853 149
jjg@904 150 /**
jjg@904 151 * Get the representation of this type used for modelling purposes.
jjg@904 152 * By default, this is itself. For ErrorType, a different value
jjg@1521 153 * may be provided.
jjg@904 154 */
jjg@904 155 public Type getModelType() {
jjg@904 156 return this;
jjg@904 157 }
jjg@904 158
jjg@904 159 public static List<Type> getModelTypes(List<Type> ts) {
vromero@1853 160 ListBuffer<Type> lb = new ListBuffer<>();
jjg@904 161 for (Type t: ts)
jjg@904 162 lb.append(t.getModelType());
jjg@904 163 return lb.toList();
jjg@904 164 }
jjg@904 165
duke@1 166 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
duke@1 167
duke@1 168 /** Define a type given its tag and type symbol
duke@1 169 */
vromero@1853 170 public Type(TypeSymbol tsym) {
duke@1 171 this.tsym = tsym;
duke@1 172 }
duke@1 173
duke@1 174 /** An abstract class for mappings from types to types
duke@1 175 */
duke@1 176 public static abstract class Mapping {
duke@1 177 private String name;
duke@1 178 public Mapping(String name) {
duke@1 179 this.name = name;
duke@1 180 }
duke@1 181 public abstract Type apply(Type t);
duke@1 182 public String toString() {
duke@1 183 return name;
duke@1 184 }
duke@1 185 }
duke@1 186
duke@1 187 /** map a type function over all immediate descendants of this type
duke@1 188 */
duke@1 189 public Type map(Mapping f) {
duke@1 190 return this;
duke@1 191 }
duke@1 192
duke@1 193 /** map a type function over a list of types
duke@1 194 */
duke@1 195 public static List<Type> map(List<Type> ts, Mapping f) {
duke@1 196 if (ts.nonEmpty()) {
duke@1 197 List<Type> tail1 = map(ts.tail, f);
duke@1 198 Type t = f.apply(ts.head);
duke@1 199 if (tail1 != ts.tail || t != ts.head)
duke@1 200 return tail1.prepend(t);
duke@1 201 }
duke@1 202 return ts;
duke@1 203 }
duke@1 204
duke@1 205 /** Define a constant type, of the same kind as this type
duke@1 206 * and with given constant value
duke@1 207 */
duke@1 208 public Type constType(Object constValue) {
vromero@1853 209 throw new AssertionError();
duke@1 210 }
duke@1 211
duke@1 212 /**
duke@1 213 * If this is a constant type, return its underlying type.
duke@1 214 * Otherwise, return the type itself.
duke@1 215 */
duke@1 216 public Type baseType() {
duke@1 217 return this;
duke@1 218 }
duke@1 219
jjg@1644 220 public boolean isAnnotated() {
jjg@1644 221 return false;
jjg@1644 222 }
jjg@1644 223
jjg@1521 224 /**
jjg@1521 225 * If this is an annotated type, return the underlying type.
jjg@1521 226 * Otherwise, return the type itself.
jjg@1521 227 */
jjg@1521 228 public Type unannotatedType() {
jjg@1521 229 return this;
jjg@1521 230 }
jjg@1521 231
jjg@1645 232 @Override
jjg@1645 233 public List<? extends Attribute.TypeCompound> getAnnotationMirrors() {
jjg@1645 234 return List.nil();
jjg@1645 235 }
jjg@1645 236
jjg@1645 237 @Override
jjg@1645 238 public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
jjg@1645 239 return null;
jjg@1645 240 }
jjg@1645 241
jjg@1645 242 @Override
jjg@1645 243 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
jjg@1645 244 @SuppressWarnings("unchecked")
jjg@1645 245 A[] tmp = (A[]) java.lang.reflect.Array.newInstance(annotationType, 0);
jjg@1645 246 return tmp;
jjg@1645 247 }
jjg@1645 248
duke@1 249 /** Return the base types of a list of types.
duke@1 250 */
duke@1 251 public static List<Type> baseTypes(List<Type> ts) {
duke@1 252 if (ts.nonEmpty()) {
duke@1 253 Type t = ts.head.baseType();
duke@1 254 List<Type> baseTypes = baseTypes(ts.tail);
duke@1 255 if (t != ts.head || baseTypes != ts.tail)
duke@1 256 return baseTypes.prepend(t);
duke@1 257 }
duke@1 258 return ts;
duke@1 259 }
duke@1 260
duke@1 261 /** The Java source which this type represents.
duke@1 262 */
duke@1 263 public String toString() {
duke@1 264 String s = (tsym == null || tsym.name == null)
duke@1 265 ? "<none>"
duke@1 266 : tsym.name.toString();
vromero@1853 267 if (moreInfo && hasTag(TYPEVAR)) {
vromero@1853 268 s = s + hashCode();
vromero@1853 269 }
duke@1 270 return s;
duke@1 271 }
duke@1 272
duke@1 273 /**
duke@1 274 * The Java source which this type list represents. A List is
duke@1 275 * represented as a comma-spearated listing of the elements in
duke@1 276 * that list.
duke@1 277 */
duke@1 278 public static String toString(List<Type> ts) {
duke@1 279 if (ts.isEmpty()) {
duke@1 280 return "";
duke@1 281 } else {
jjg@904 282 StringBuilder buf = new StringBuilder();
duke@1 283 buf.append(ts.head.toString());
duke@1 284 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
duke@1 285 buf.append(",").append(l.head.toString());
duke@1 286 return buf.toString();
duke@1 287 }
duke@1 288 }
duke@1 289
duke@1 290 /**
duke@1 291 * The constant value of this type, converted to String
duke@1 292 */
duke@1 293 public String stringValue() {
jjg@816 294 Object cv = Assert.checkNonNull(constValue());
vromero@1853 295 return cv.toString();
duke@1 296 }
duke@1 297
duke@1 298 /**
duke@1 299 * This method is analogous to isSameType, but weaker, since we
duke@1 300 * never complete classes. Where isSameType would complete a
duke@1 301 * class, equals assumes that the two types are different.
duke@1 302 */
vromero@1452 303 @Override
duke@1 304 public boolean equals(Object t) {
duke@1 305 return super.equals(t);
duke@1 306 }
duke@1 307
vromero@1452 308 @Override
duke@1 309 public int hashCode() {
duke@1 310 return super.hashCode();
duke@1 311 }
duke@1 312
duke@1 313 public String argtypes(boolean varargs) {
duke@1 314 List<Type> args = getParameterTypes();
duke@1 315 if (!varargs) return args.toString();
jjg@789 316 StringBuilder buf = new StringBuilder();
duke@1 317 while (args.tail.nonEmpty()) {
duke@1 318 buf.append(args.head);
duke@1 319 args = args.tail;
duke@1 320 buf.append(',');
duke@1 321 }
vromero@1853 322 if (args.head.unannotatedType().hasTag(ARRAY)) {
jjg@1521 323 buf.append(((ArrayType)args.head.unannotatedType()).elemtype);
jjg@1645 324 if (args.head.getAnnotationMirrors().nonEmpty()) {
jjg@1645 325 buf.append(args.head.getAnnotationMirrors());
jjg@1521 326 }
duke@1 327 buf.append("...");
duke@1 328 } else {
duke@1 329 buf.append(args.head);
duke@1 330 }
duke@1 331 return buf.toString();
duke@1 332 }
duke@1 333
duke@1 334 /** Access methods.
duke@1 335 */
duke@1 336 public List<Type> getTypeArguments() { return List.nil(); }
jjg@1521 337 public Type getEnclosingType() { return null; }
duke@1 338 public List<Type> getParameterTypes() { return List.nil(); }
duke@1 339 public Type getReturnType() { return null; }
jjg@1521 340 public Type getReceiverType() { return null; }
duke@1 341 public List<Type> getThrownTypes() { return List.nil(); }
duke@1 342 public Type getUpperBound() { return null; }
duke@1 343 public Type getLowerBound() { return null; }
duke@1 344
duke@1 345 /** Navigation methods, these will work for classes, type variables,
duke@1 346 * foralls, but will return null for arrays and methods.
duke@1 347 */
duke@1 348
duke@1 349 /** Return all parameters of this type and all its outer types in order
duke@1 350 * outer (first) to inner (last).
duke@1 351 */
duke@1 352 public List<Type> allparams() { return List.nil(); }
duke@1 353
duke@1 354 /** Does this type contain "error" elements?
duke@1 355 */
duke@1 356 public boolean isErroneous() {
duke@1 357 return false;
duke@1 358 }
duke@1 359
duke@1 360 public static boolean isErroneous(List<Type> ts) {
duke@1 361 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
duke@1 362 if (l.head.isErroneous()) return true;
duke@1 363 return false;
duke@1 364 }
duke@1 365
duke@1 366 /** Is this type parameterized?
duke@1 367 * A class type is parameterized if it has some parameters.
duke@1 368 * An array type is parameterized if its element type is parameterized.
duke@1 369 * All other types are not parameterized.
duke@1 370 */
duke@1 371 public boolean isParameterized() {
duke@1 372 return false;
duke@1 373 }
duke@1 374
duke@1 375 /** Is this type a raw type?
duke@1 376 * A class type is a raw type if it misses some of its parameters.
duke@1 377 * An array type is a raw type if its element type is raw.
duke@1 378 * All other types are not raw.
duke@1 379 * Type validation will ensure that the only raw types
duke@1 380 * in a program are types that miss all their type variables.
duke@1 381 */
duke@1 382 public boolean isRaw() {
duke@1 383 return false;
duke@1 384 }
duke@1 385
duke@1 386 public boolean isCompound() {
duke@1 387 return tsym.completer == null
duke@1 388 // Compound types can't have a completer. Calling
duke@1 389 // flags() will complete the symbol causing the
duke@1 390 // compiler to load classes unnecessarily. This led
duke@1 391 // to regression 6180021.
duke@1 392 && (tsym.flags() & COMPOUND) != 0;
duke@1 393 }
duke@1 394
duke@1 395 public boolean isInterface() {
duke@1 396 return (tsym.flags() & INTERFACE) != 0;
duke@1 397 }
duke@1 398
mcimadamore@640 399 public boolean isFinal() {
mcimadamore@640 400 return (tsym.flags() & FINAL) != 0;
mcimadamore@640 401 }
mcimadamore@640 402
duke@1 403 /**
duke@1 404 * Does this type contain occurrences of type t?
duke@1 405 */
duke@1 406 public boolean contains(Type t) {
duke@1 407 return t == this;
duke@1 408 }
duke@1 409
duke@1 410 public static boolean contains(List<Type> ts, Type t) {
duke@1 411 for (List<Type> l = ts;
duke@1 412 l.tail != null /*inlined: l.nonEmpty()*/;
duke@1 413 l = l.tail)
duke@1 414 if (l.head.contains(t)) return true;
duke@1 415 return false;
duke@1 416 }
duke@1 417
mcimadamore@635 418 /** Does this type contain an occurrence of some type in 'ts'?
duke@1 419 */
mcimadamore@635 420 public boolean containsAny(List<Type> ts) {
mcimadamore@635 421 for (Type t : ts)
mcimadamore@635 422 if (this.contains(t)) return true;
mcimadamore@635 423 return false;
mcimadamore@635 424 }
mcimadamore@635 425
mcimadamore@635 426 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
mcimadamore@635 427 for (Type t : ts1)
mcimadamore@635 428 if (t.containsAny(ts2)) return true;
duke@1 429 return false;
duke@1 430 }
duke@1 431
mcimadamore@828 432 public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
mcimadamore@828 433 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@828 434 for (Type t : ts) {
mcimadamore@828 435 if (tf.accepts(t)) {
mcimadamore@828 436 buf.append(t);
mcimadamore@828 437 }
mcimadamore@828 438 }
mcimadamore@828 439 return buf.toList();
mcimadamore@828 440 }
mcimadamore@828 441
duke@1 442 public boolean isSuperBound() { return false; }
duke@1 443 public boolean isExtendsBound() { return false; }
duke@1 444 public boolean isUnbound() { return false; }
duke@1 445 public Type withTypeVar(Type t) { return this; }
duke@1 446
duke@1 447 /** The underlying method type of this type.
duke@1 448 */
duke@1 449 public MethodType asMethodType() { throw new AssertionError(); }
duke@1 450
duke@1 451 /** Complete loading all classes in this type.
duke@1 452 */
duke@1 453 public void complete() {}
duke@1 454
duke@1 455 public TypeSymbol asElement() {
duke@1 456 return tsym;
duke@1 457 }
duke@1 458
vromero@1853 459 @Override
duke@1 460 public TypeKind getKind() {
vromero@1853 461 return TypeKind.OTHER;
duke@1 462 }
duke@1 463
vromero@1853 464 @Override
duke@1 465 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
vromero@1853 466 throw new AssertionError();
vromero@1853 467 }
vromero@1853 468
vromero@1853 469 public static class JCPrimitiveType extends Type
vromero@1853 470 implements javax.lang.model.type.PrimitiveType {
vromero@1853 471
vromero@1853 472 TypeTag tag;
vromero@1853 473
vromero@1853 474 public JCPrimitiveType(TypeTag tag, TypeSymbol tsym) {
vromero@1853 475 super(tsym);
vromero@1853 476 this.tag = tag;
vromero@1853 477 Assert.check(tag.isPrimitive);
vromero@1853 478 }
vromero@1853 479
vromero@1853 480 @Override
vromero@1853 481 public boolean isNumeric() {
vromero@1853 482 return tag != BOOLEAN;
vromero@1853 483 }
vromero@1853 484
vromero@1853 485 @Override
vromero@1853 486 public boolean isPrimitive() {
vromero@1853 487 return true;
vromero@1853 488 }
vromero@1853 489
vromero@1853 490 @Override
vromero@1853 491 public TypeTag getTag() {
vromero@1853 492 return tag;
vromero@1853 493 }
vromero@1853 494
vromero@1853 495 @Override
vromero@1853 496 public boolean isPrimitiveOrVoid() {
vromero@1853 497 return true;
vromero@1853 498 }
vromero@1853 499
vromero@1853 500 /** Define a constant type, of the same kind as this type
vromero@1853 501 * and with given constant value
vromero@1853 502 */
vromero@1853 503 @Override
vromero@1853 504 public Type constType(Object constValue) {
vromero@1853 505 final Object value = constValue;
vromero@1853 506 return new JCPrimitiveType(tag, tsym) {
vromero@1853 507 @Override
vromero@1853 508 public Object constValue() {
vromero@1853 509 return value;
vromero@1853 510 }
vromero@1853 511 @Override
vromero@1853 512 public Type baseType() {
vromero@1853 513 return tsym.type;
vromero@1853 514 }
vromero@1853 515 };
vromero@1853 516 }
vromero@1853 517
vromero@1853 518 /**
vromero@1853 519 * The constant value of this type, converted to String
vromero@1853 520 */
vromero@1853 521 @Override
vromero@1853 522 public String stringValue() {
vromero@1853 523 Object cv = Assert.checkNonNull(constValue());
vromero@1853 524 if (tag == BOOLEAN) {
vromero@1853 525 return ((Integer) cv).intValue() == 0 ? "false" : "true";
vromero@1853 526 }
vromero@1853 527 else if (tag == CHAR) {
vromero@1853 528 return String.valueOf((char) ((Integer) cv).intValue());
vromero@1853 529 }
vromero@1853 530 else {
vromero@1853 531 return cv.toString();
vromero@1853 532 }
vromero@1853 533 }
vromero@1853 534
vromero@1853 535 /** Is this a constant type whose value is false?
vromero@1853 536 */
vromero@1853 537 @Override
vromero@1853 538 public boolean isFalse() {
vromero@1853 539 return
vromero@1853 540 tag == BOOLEAN &&
vromero@1853 541 constValue() != null &&
vromero@1853 542 ((Integer)constValue()).intValue() == 0;
vromero@1853 543 }
vromero@1853 544
vromero@1853 545 /** Is this a constant type whose value is true?
vromero@1853 546 */
vromero@1853 547 @Override
vromero@1853 548 public boolean isTrue() {
vromero@1853 549 return
vromero@1853 550 tag == BOOLEAN &&
vromero@1853 551 constValue() != null &&
vromero@1853 552 ((Integer)constValue()).intValue() != 0;
vromero@1853 553 }
vromero@1853 554
vromero@1853 555 @Override
vromero@1853 556 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 557 return v.visitPrimitive(this, p);
vromero@1853 558 }
vromero@1853 559
vromero@1853 560 @Override
vromero@1853 561 public TypeKind getKind() {
vromero@1853 562 switch (tag) {
vromero@1853 563 case BYTE: return TypeKind.BYTE;
vromero@1853 564 case CHAR: return TypeKind.CHAR;
vromero@1853 565 case SHORT: return TypeKind.SHORT;
vromero@1853 566 case INT: return TypeKind.INT;
vromero@1853 567 case LONG: return TypeKind.LONG;
vromero@1853 568 case FLOAT: return TypeKind.FLOAT;
vromero@1853 569 case DOUBLE: return TypeKind.DOUBLE;
vromero@1853 570 case BOOLEAN: return TypeKind.BOOLEAN;
vromero@1853 571 }
duke@1 572 throw new AssertionError();
vromero@1853 573 }
vromero@1853 574
duke@1 575 }
duke@1 576
duke@1 577 public static class WildcardType extends Type
duke@1 578 implements javax.lang.model.type.WildcardType {
duke@1 579
duke@1 580 public Type type;
duke@1 581 public BoundKind kind;
duke@1 582 public TypeVar bound;
duke@1 583
duke@1 584 @Override
duke@1 585 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 586 return v.visitWildcardType(this, s);
duke@1 587 }
duke@1 588
duke@1 589 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
vromero@1853 590 super(tsym);
jjg@816 591 this.type = Assert.checkNonNull(type);
duke@1 592 this.kind = kind;
duke@1 593 }
duke@1 594 public WildcardType(WildcardType t, TypeVar bound) {
duke@1 595 this(t.type, t.kind, t.tsym, bound);
duke@1 596 }
duke@1 597
duke@1 598 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
duke@1 599 this(type, kind, tsym);
duke@1 600 this.bound = bound;
duke@1 601 }
duke@1 602
vromero@1853 603 @Override
vromero@1853 604 public TypeTag getTag() {
vromero@1853 605 return WILDCARD;
vromero@1853 606 }
vromero@1853 607
vromero@1853 608 @Override
mcimadamore@635 609 public boolean contains(Type t) {
mcimadamore@635 610 return kind != UNBOUND && type.contains(t);
mcimadamore@635 611 }
mcimadamore@635 612
duke@1 613 public boolean isSuperBound() {
duke@1 614 return kind == SUPER ||
duke@1 615 kind == UNBOUND;
duke@1 616 }
duke@1 617 public boolean isExtendsBound() {
duke@1 618 return kind == EXTENDS ||
duke@1 619 kind == UNBOUND;
duke@1 620 }
duke@1 621 public boolean isUnbound() {
duke@1 622 return kind == UNBOUND;
duke@1 623 }
duke@1 624
vromero@1853 625 @Override
vromero@1853 626 public boolean isReference() {
vromero@1853 627 return true;
vromero@1853 628 }
vromero@1853 629
vromero@1853 630 @Override
vromero@1853 631 public boolean isNullOrReference() {
vromero@1853 632 return true;
vromero@1853 633 }
vromero@1853 634
vromero@1853 635 @Override
duke@1 636 public Type withTypeVar(Type t) {
duke@1 637 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
duke@1 638 if (bound == t)
duke@1 639 return this;
duke@1 640 bound = (TypeVar)t;
duke@1 641 return this;
duke@1 642 }
duke@1 643
duke@1 644 boolean isPrintingBound = false;
duke@1 645 public String toString() {
jjg@904 646 StringBuilder s = new StringBuilder();
duke@1 647 s.append(kind.toString());
duke@1 648 if (kind != UNBOUND)
duke@1 649 s.append(type);
duke@1 650 if (moreInfo && bound != null && !isPrintingBound)
duke@1 651 try {
duke@1 652 isPrintingBound = true;
duke@1 653 s.append("{:").append(bound.bound).append(":}");
duke@1 654 } finally {
duke@1 655 isPrintingBound = false;
duke@1 656 }
duke@1 657 return s.toString();
duke@1 658 }
duke@1 659
duke@1 660 public Type map(Mapping f) {
duke@1 661 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
duke@1 662 Type t = type;
duke@1 663 if (t != null)
duke@1 664 t = f.apply(t);
duke@1 665 if (t == type)
duke@1 666 return this;
duke@1 667 else
duke@1 668 return new WildcardType(t, kind, tsym, bound);
duke@1 669 }
duke@1 670
duke@1 671 public Type getExtendsBound() {
duke@1 672 if (kind == EXTENDS)
duke@1 673 return type;
duke@1 674 else
duke@1 675 return null;
duke@1 676 }
duke@1 677
duke@1 678 public Type getSuperBound() {
duke@1 679 if (kind == SUPER)
duke@1 680 return type;
duke@1 681 else
duke@1 682 return null;
duke@1 683 }
duke@1 684
duke@1 685 public TypeKind getKind() {
duke@1 686 return TypeKind.WILDCARD;
duke@1 687 }
duke@1 688
duke@1 689 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 690 return v.visitWildcard(this, p);
duke@1 691 }
duke@1 692 }
duke@1 693
duke@1 694 public static class ClassType extends Type implements DeclaredType {
duke@1 695
duke@1 696 /** The enclosing type of this type. If this is the type of an inner
duke@1 697 * class, outer_field refers to the type of its enclosing
jjg@1521 698 * instance class, in all other cases it refers to noType.
duke@1 699 */
duke@1 700 private Type outer_field;
duke@1 701
duke@1 702 /** The type parameters of this type (to be set once class is loaded).
duke@1 703 */
duke@1 704 public List<Type> typarams_field;
duke@1 705
duke@1 706 /** A cache variable for the type parameters of this type,
duke@1 707 * appended to all parameters of its enclosing class.
duke@1 708 * @see #allparams
duke@1 709 */
duke@1 710 public List<Type> allparams_field;
duke@1 711
duke@1 712 /** The supertype of this class (to be set once class is loaded).
duke@1 713 */
duke@1 714 public Type supertype_field;
duke@1 715
duke@1 716 /** The interfaces of this class (to be set once class is loaded).
duke@1 717 */
duke@1 718 public List<Type> interfaces_field;
duke@1 719
jjg@904 720 /** All the interfaces of this class, including missing ones.
jjg@904 721 */
jjg@904 722 public List<Type> all_interfaces_field;
jjg@904 723
duke@1 724 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
vromero@1853 725 super(tsym);
duke@1 726 this.outer_field = outer;
duke@1 727 this.typarams_field = typarams;
duke@1 728 this.allparams_field = null;
duke@1 729 this.supertype_field = null;
duke@1 730 this.interfaces_field = null;
duke@1 731 /*
duke@1 732 // this can happen during error recovery
duke@1 733 assert
duke@1 734 outer.isParameterized() ?
duke@1 735 typarams.length() == tsym.type.typarams().length() :
duke@1 736 outer.isRaw() ?
duke@1 737 typarams.length() == 0 :
duke@1 738 true;
duke@1 739 */
duke@1 740 }
duke@1 741
duke@1 742 @Override
vromero@1853 743 public TypeTag getTag() {
vromero@1853 744 return CLASS;
vromero@1853 745 }
vromero@1853 746
vromero@1853 747 @Override
duke@1 748 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 749 return v.visitClassType(this, s);
duke@1 750 }
duke@1 751
duke@1 752 public Type constType(Object constValue) {
duke@1 753 final Object value = constValue;
duke@1 754 return new ClassType(getEnclosingType(), typarams_field, tsym) {
duke@1 755 @Override
duke@1 756 public Object constValue() {
duke@1 757 return value;
duke@1 758 }
duke@1 759 @Override
duke@1 760 public Type baseType() {
duke@1 761 return tsym.type;
duke@1 762 }
duke@1 763 };
duke@1 764 }
duke@1 765
duke@1 766 /** The Java source which this type represents.
duke@1 767 */
duke@1 768 public String toString() {
jjg@904 769 StringBuilder buf = new StringBuilder();
vromero@1853 770 if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) {
duke@1 771 buf.append(getEnclosingType().toString());
duke@1 772 buf.append(".");
duke@1 773 buf.append(className(tsym, false));
duke@1 774 } else {
duke@1 775 buf.append(className(tsym, true));
duke@1 776 }
duke@1 777 if (getTypeArguments().nonEmpty()) {
duke@1 778 buf.append('<');
duke@1 779 buf.append(getTypeArguments().toString());
duke@1 780 buf.append(">");
duke@1 781 }
duke@1 782 return buf.toString();
duke@1 783 }
duke@1 784 //where
duke@1 785 private String className(Symbol sym, boolean longform) {
jjg@113 786 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
jjg@904 787 StringBuilder s = new StringBuilder(supertype_field.toString());
duke@1 788 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
duke@1 789 s.append("&");
duke@1 790 s.append(is.head.toString());
duke@1 791 }
duke@1 792 return s.toString();
jjg@113 793 } else if (sym.name.isEmpty()) {
duke@1 794 String s;
jjg@1755 795 ClassType norm = (ClassType) tsym.type.unannotatedType();
duke@1 796 if (norm == null) {
duke@1 797 s = Log.getLocalizedString("anonymous.class", (Object)null);
duke@1 798 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
duke@1 799 s = Log.getLocalizedString("anonymous.class",
duke@1 800 norm.interfaces_field.head);
duke@1 801 } else {
duke@1 802 s = Log.getLocalizedString("anonymous.class",
duke@1 803 norm.supertype_field);
duke@1 804 }
duke@1 805 if (moreInfo)
duke@1 806 s += String.valueOf(sym.hashCode());
duke@1 807 return s;
duke@1 808 } else if (longform) {
duke@1 809 return sym.getQualifiedName().toString();
duke@1 810 } else {
duke@1 811 return sym.name.toString();
duke@1 812 }
duke@1 813 }
duke@1 814
duke@1 815 public List<Type> getTypeArguments() {
duke@1 816 if (typarams_field == null) {
duke@1 817 complete();
duke@1 818 if (typarams_field == null)
duke@1 819 typarams_field = List.nil();
duke@1 820 }
duke@1 821 return typarams_field;
duke@1 822 }
duke@1 823
mcimadamore@30 824 public boolean hasErasedSupertypes() {
mcimadamore@30 825 return isRaw();
mcimadamore@30 826 }
mcimadamore@30 827
duke@1 828 public Type getEnclosingType() {
duke@1 829 return outer_field;
duke@1 830 }
duke@1 831
duke@1 832 public void setEnclosingType(Type outer) {
duke@1 833 outer_field = outer;
duke@1 834 }
duke@1 835
duke@1 836 public List<Type> allparams() {
duke@1 837 if (allparams_field == null) {
duke@1 838 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
duke@1 839 }
duke@1 840 return allparams_field;
duke@1 841 }
duke@1 842
duke@1 843 public boolean isErroneous() {
duke@1 844 return
duke@1 845 getEnclosingType().isErroneous() ||
duke@1 846 isErroneous(getTypeArguments()) ||
jjg@1755 847 this != tsym.type.unannotatedType() && tsym.type.isErroneous();
duke@1 848 }
duke@1 849
duke@1 850 public boolean isParameterized() {
duke@1 851 return allparams().tail != null;
duke@1 852 // optimization, was: allparams().nonEmpty();
duke@1 853 }
duke@1 854
vromero@1853 855 @Override
vromero@1853 856 public boolean isReference() {
vromero@1853 857 return true;
vromero@1853 858 }
vromero@1853 859
vromero@1853 860 @Override
vromero@1853 861 public boolean isNullOrReference() {
vromero@1853 862 return true;
vromero@1853 863 }
vromero@1853 864
duke@1 865 /** A cache for the rank. */
duke@1 866 int rank_field = -1;
duke@1 867
duke@1 868 /** A class type is raw if it misses some
duke@1 869 * of its type parameter sections.
duke@1 870 * After validation, this is equivalent to:
jjg@1326 871 * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
duke@1 872 */
duke@1 873 public boolean isRaw() {
duke@1 874 return
duke@1 875 this != tsym.type && // necessary, but not sufficient condition
duke@1 876 tsym.type.allparams().nonEmpty() &&
duke@1 877 allparams().isEmpty();
duke@1 878 }
duke@1 879
duke@1 880 public Type map(Mapping f) {
duke@1 881 Type outer = getEnclosingType();
duke@1 882 Type outer1 = f.apply(outer);
duke@1 883 List<Type> typarams = getTypeArguments();
duke@1 884 List<Type> typarams1 = map(typarams, f);
duke@1 885 if (outer1 == outer && typarams1 == typarams) return this;
duke@1 886 else return new ClassType(outer1, typarams1, tsym);
duke@1 887 }
duke@1 888
duke@1 889 public boolean contains(Type elem) {
duke@1 890 return
duke@1 891 elem == this
duke@1 892 || (isParameterized()
mcimadamore@635 893 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
mcimadamore@635 894 || (isCompound()
mcimadamore@635 895 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
duke@1 896 }
duke@1 897
duke@1 898 public void complete() {
duke@1 899 if (tsym.completer != null) tsym.complete();
duke@1 900 }
duke@1 901
duke@1 902 public TypeKind getKind() {
duke@1 903 return TypeKind.DECLARED;
duke@1 904 }
duke@1 905
duke@1 906 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 907 return v.visitDeclared(this, p);
duke@1 908 }
duke@1 909 }
duke@1 910
mcimadamore@30 911 public static class ErasedClassType extends ClassType {
mcimadamore@30 912 public ErasedClassType(Type outer, TypeSymbol tsym) {
mcimadamore@30 913 super(outer, List.<Type>nil(), tsym);
mcimadamore@30 914 }
mcimadamore@30 915
mcimadamore@30 916 @Override
mcimadamore@30 917 public boolean hasErasedSupertypes() {
mcimadamore@30 918 return true;
mcimadamore@30 919 }
mcimadamore@30 920 }
mcimadamore@30 921
jjg@988 922 // a clone of a ClassType that knows about the alternatives of a union type.
jjg@988 923 public static class UnionClassType extends ClassType implements UnionType {
jjg@988 924 final List<? extends Type> alternatives_field;
jjg@988 925
jjg@988 926 public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
jjg@988 927 super(ct.outer_field, ct.typarams_field, ct.tsym);
jjg@988 928 allparams_field = ct.allparams_field;
jjg@988 929 supertype_field = ct.supertype_field;
jjg@988 930 interfaces_field = ct.interfaces_field;
jjg@988 931 all_interfaces_field = ct.interfaces_field;
jjg@988 932 alternatives_field = alternatives;
jjg@988 933 }
jjg@988 934
jjg@988 935 public Type getLub() {
jjg@988 936 return tsym.type;
jjg@988 937 }
jjg@988 938
jjg@988 939 public java.util.List<? extends TypeMirror> getAlternatives() {
jjg@988 940 return Collections.unmodifiableList(alternatives_field);
jjg@988 941 }
jjg@988 942
jjg@988 943 @Override
jjg@988 944 public TypeKind getKind() {
jjg@988 945 return TypeKind.UNION;
jjg@988 946 }
jjg@988 947
jjg@988 948 @Override
jjg@988 949 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
jjg@988 950 return v.visitUnion(this, p);
jjg@988 951 }
jjg@988 952 }
jjg@988 953
mcimadamore@1436 954 // a clone of a ClassType that knows about the bounds of an intersection type.
mcimadamore@1436 955 public static class IntersectionClassType extends ClassType implements IntersectionType {
mcimadamore@1436 956
mcimadamore@1436 957 public boolean allInterfaces;
mcimadamore@1436 958
mcimadamore@1436 959 public enum IntersectionKind {
mcimadamore@1436 960 EXPLICIT,
mcimadamore@1436 961 IMPLICT;
mcimadamore@1436 962 }
mcimadamore@1436 963
mcimadamore@1436 964 public IntersectionKind intersectionKind;
mcimadamore@1436 965
mcimadamore@1436 966 public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
mcimadamore@1436 967 super(Type.noType, List.<Type>nil(), csym);
mcimadamore@1436 968 this.allInterfaces = allInterfaces;
mcimadamore@1436 969 Assert.check((csym.flags() & COMPOUND) != 0);
mcimadamore@1436 970 supertype_field = bounds.head;
mcimadamore@1436 971 interfaces_field = bounds.tail;
mcimadamore@1436 972 Assert.check(supertype_field.tsym.completer != null ||
mcimadamore@1436 973 !supertype_field.isInterface(), supertype_field);
mcimadamore@1436 974 }
mcimadamore@1436 975
mcimadamore@1436 976 public java.util.List<? extends TypeMirror> getBounds() {
mcimadamore@1436 977 return Collections.unmodifiableList(getComponents());
mcimadamore@1436 978 }
mcimadamore@1436 979
mcimadamore@1436 980 public List<Type> getComponents() {
mcimadamore@1436 981 return interfaces_field.prepend(supertype_field);
mcimadamore@1436 982 }
mcimadamore@1436 983
mcimadamore@1678 984 public List<Type> getExplicitComponents() {
mcimadamore@1678 985 return allInterfaces ?
mcimadamore@1678 986 interfaces_field :
mcimadamore@1678 987 getComponents();
mcimadamore@1678 988 }
mcimadamore@1678 989
mcimadamore@1436 990 @Override
mcimadamore@1436 991 public TypeKind getKind() {
mcimadamore@1436 992 return TypeKind.INTERSECTION;
mcimadamore@1436 993 }
mcimadamore@1436 994
mcimadamore@1436 995 @Override
mcimadamore@1436 996 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
mcimadamore@1436 997 return intersectionKind == IntersectionKind.EXPLICIT ?
mcimadamore@1436 998 v.visitIntersection(this, p) :
mcimadamore@1436 999 v.visitDeclared(this, p);
mcimadamore@1436 1000 }
mcimadamore@1436 1001 }
mcimadamore@1436 1002
duke@1 1003 public static class ArrayType extends Type
duke@1 1004 implements javax.lang.model.type.ArrayType {
duke@1 1005
duke@1 1006 public Type elemtype;
duke@1 1007
duke@1 1008 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
vromero@1853 1009 super(arrayClass);
duke@1 1010 this.elemtype = elemtype;
duke@1 1011 }
duke@1 1012
duke@1 1013 @Override
vromero@1853 1014 public TypeTag getTag() {
vromero@1853 1015 return ARRAY;
vromero@1853 1016 }
vromero@1853 1017
duke@1 1018 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1019 return v.visitArrayType(this, s);
duke@1 1020 }
duke@1 1021
duke@1 1022 public String toString() {
duke@1 1023 return elemtype + "[]";
duke@1 1024 }
duke@1 1025
duke@1 1026 public boolean equals(Object obj) {
duke@1 1027 return
duke@1 1028 this == obj ||
duke@1 1029 (obj instanceof ArrayType &&
duke@1 1030 this.elemtype.equals(((ArrayType)obj).elemtype));
duke@1 1031 }
duke@1 1032
duke@1 1033 public int hashCode() {
jjg@1374 1034 return (ARRAY.ordinal() << 5) + elemtype.hashCode();
duke@1 1035 }
duke@1 1036
mcimadamore@795 1037 public boolean isVarargs() {
mcimadamore@795 1038 return false;
mcimadamore@795 1039 }
mcimadamore@795 1040
duke@1 1041 public List<Type> allparams() { return elemtype.allparams(); }
duke@1 1042
duke@1 1043 public boolean isErroneous() {
duke@1 1044 return elemtype.isErroneous();
duke@1 1045 }
duke@1 1046
duke@1 1047 public boolean isParameterized() {
duke@1 1048 return elemtype.isParameterized();
duke@1 1049 }
duke@1 1050
vromero@1853 1051 @Override
vromero@1853 1052 public boolean isReference() {
vromero@1853 1053 return true;
vromero@1853 1054 }
vromero@1853 1055
vromero@1853 1056 @Override
vromero@1853 1057 public boolean isNullOrReference() {
vromero@1853 1058 return true;
vromero@1853 1059 }
vromero@1853 1060
duke@1 1061 public boolean isRaw() {
duke@1 1062 return elemtype.isRaw();
duke@1 1063 }
duke@1 1064
mcimadamore@795 1065 public ArrayType makeVarargs() {
mcimadamore@795 1066 return new ArrayType(elemtype, tsym) {
mcimadamore@795 1067 @Override
mcimadamore@795 1068 public boolean isVarargs() {
mcimadamore@795 1069 return true;
mcimadamore@795 1070 }
mcimadamore@795 1071 };
mcimadamore@795 1072 }
mcimadamore@795 1073
duke@1 1074 public Type map(Mapping f) {
duke@1 1075 Type elemtype1 = f.apply(elemtype);
duke@1 1076 if (elemtype1 == elemtype) return this;
duke@1 1077 else return new ArrayType(elemtype1, tsym);
duke@1 1078 }
duke@1 1079
duke@1 1080 public boolean contains(Type elem) {
duke@1 1081 return elem == this || elemtype.contains(elem);
duke@1 1082 }
duke@1 1083
duke@1 1084 public void complete() {
duke@1 1085 elemtype.complete();
duke@1 1086 }
duke@1 1087
duke@1 1088 public Type getComponentType() {
duke@1 1089 return elemtype;
duke@1 1090 }
duke@1 1091
duke@1 1092 public TypeKind getKind() {
duke@1 1093 return TypeKind.ARRAY;
duke@1 1094 }
duke@1 1095
duke@1 1096 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1097 return v.visitArray(this, p);
duke@1 1098 }
duke@1 1099 }
duke@1 1100
dlsmith@880 1101 public static class MethodType extends Type implements ExecutableType {
duke@1 1102
duke@1 1103 public List<Type> argtypes;
duke@1 1104 public Type restype;
duke@1 1105 public List<Type> thrown;
duke@1 1106
jjg@1521 1107 /** The type annotations on the method receiver.
jjg@1521 1108 */
jjg@1521 1109 public Type recvtype;
jjg@1521 1110
duke@1 1111 public MethodType(List<Type> argtypes,
duke@1 1112 Type restype,
duke@1 1113 List<Type> thrown,
duke@1 1114 TypeSymbol methodClass) {
vromero@1853 1115 super(methodClass);
duke@1 1116 this.argtypes = argtypes;
duke@1 1117 this.restype = restype;
duke@1 1118 this.thrown = thrown;
duke@1 1119 }
duke@1 1120
duke@1 1121 @Override
vromero@1853 1122 public TypeTag getTag() {
vromero@1853 1123 return METHOD;
vromero@1853 1124 }
vromero@1853 1125
duke@1 1126 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1127 return v.visitMethodType(this, s);
duke@1 1128 }
duke@1 1129
duke@1 1130 /** The Java source which this type represents.
duke@1 1131 *
duke@1 1132 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
duke@1 1133 * should be.
duke@1 1134 */
duke@1 1135 public String toString() {
duke@1 1136 return "(" + argtypes + ")" + restype;
duke@1 1137 }
duke@1 1138
duke@1 1139 public List<Type> getParameterTypes() { return argtypes; }
duke@1 1140 public Type getReturnType() { return restype; }
jjg@1521 1141 public Type getReceiverType() { return recvtype; }
duke@1 1142 public List<Type> getThrownTypes() { return thrown; }
duke@1 1143
duke@1 1144 public boolean isErroneous() {
duke@1 1145 return
duke@1 1146 isErroneous(argtypes) ||
duke@1 1147 restype != null && restype.isErroneous();
duke@1 1148 }
duke@1 1149
duke@1 1150 public Type map(Mapping f) {
duke@1 1151 List<Type> argtypes1 = map(argtypes, f);
duke@1 1152 Type restype1 = f.apply(restype);
duke@1 1153 List<Type> thrown1 = map(thrown, f);
duke@1 1154 if (argtypes1 == argtypes &&
duke@1 1155 restype1 == restype &&
duke@1 1156 thrown1 == thrown) return this;
duke@1 1157 else return new MethodType(argtypes1, restype1, thrown1, tsym);
duke@1 1158 }
duke@1 1159
duke@1 1160 public boolean contains(Type elem) {
duke@1 1161 return elem == this || contains(argtypes, elem) || restype.contains(elem);
duke@1 1162 }
duke@1 1163
duke@1 1164 public MethodType asMethodType() { return this; }
duke@1 1165
duke@1 1166 public void complete() {
duke@1 1167 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
duke@1 1168 l.head.complete();
duke@1 1169 restype.complete();
jjg@1521 1170 recvtype.complete();
duke@1 1171 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
duke@1 1172 l.head.complete();
duke@1 1173 }
duke@1 1174
duke@1 1175 public List<TypeVar> getTypeVariables() {
duke@1 1176 return List.nil();
duke@1 1177 }
duke@1 1178
duke@1 1179 public TypeSymbol asElement() {
duke@1 1180 return null;
duke@1 1181 }
duke@1 1182
duke@1 1183 public TypeKind getKind() {
duke@1 1184 return TypeKind.EXECUTABLE;
duke@1 1185 }
duke@1 1186
duke@1 1187 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1188 return v.visitExecutable(this, p);
duke@1 1189 }
duke@1 1190 }
duke@1 1191
duke@1 1192 public static class PackageType extends Type implements NoType {
duke@1 1193
duke@1 1194 PackageType(TypeSymbol tsym) {
vromero@1853 1195 super(tsym);
vromero@1853 1196 }
vromero@1853 1197
vromero@1853 1198 @Override
vromero@1853 1199 public TypeTag getTag() {
vromero@1853 1200 return PACKAGE;
duke@1 1201 }
duke@1 1202
duke@1 1203 @Override
duke@1 1204 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1205 return v.visitPackageType(this, s);
duke@1 1206 }
duke@1 1207
duke@1 1208 public String toString() {
duke@1 1209 return tsym.getQualifiedName().toString();
duke@1 1210 }
duke@1 1211
duke@1 1212 public TypeKind getKind() {
duke@1 1213 return TypeKind.PACKAGE;
duke@1 1214 }
duke@1 1215
duke@1 1216 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1217 return v.visitNoType(this, p);
duke@1 1218 }
duke@1 1219 }
duke@1 1220
duke@1 1221 public static class TypeVar extends Type implements TypeVariable {
duke@1 1222
jjg@789 1223 /** The upper bound of this type variable; set from outside.
duke@1 1224 * Must be nonempty once it is set.
duke@1 1225 * For a bound, `bound' is the bound type itself.
duke@1 1226 * Multiple bounds are expressed as a single class type which has the
duke@1 1227 * individual bounds as superclass, respectively interfaces.
duke@1 1228 * The class type then has as `tsym' a compiler generated class `c',
duke@1 1229 * which has a flag COMPOUND and whose owner is the type variable
duke@1 1230 * itself. Furthermore, the erasure_field of the class
duke@1 1231 * points to the first class or interface bound.
duke@1 1232 */
duke@1 1233 public Type bound = null;
jjg@789 1234
jjg@789 1235 /** The lower bound of this type variable.
jjg@789 1236 * TypeVars don't normally have a lower bound, so it is normally set
jjg@789 1237 * to syms.botType.
jjg@789 1238 * Subtypes, such as CapturedType, may provide a different value.
jjg@789 1239 */
duke@1 1240 public Type lower;
duke@1 1241
duke@1 1242 public TypeVar(Name name, Symbol owner, Type lower) {
vromero@1853 1243 super(null);
jfranck@1689 1244 tsym = new TypeVariableSymbol(0, name, this, owner);
duke@1 1245 this.lower = lower;
duke@1 1246 }
duke@1 1247
duke@1 1248 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
vromero@1853 1249 super(tsym);
duke@1 1250 this.bound = bound;
duke@1 1251 this.lower = lower;
duke@1 1252 }
duke@1 1253
duke@1 1254 @Override
vromero@1853 1255 public TypeTag getTag() {
vromero@1853 1256 return TYPEVAR;
vromero@1853 1257 }
vromero@1853 1258
vromero@1853 1259 @Override
duke@1 1260 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1261 return v.visitTypeVar(this, s);
duke@1 1262 }
duke@1 1263
jjg@789 1264 @Override
jjg@1521 1265 public Type getUpperBound() {
vromero@1853 1266 if ((bound == null || bound.hasTag(NONE)) && this != tsym.type) {
jjg@1521 1267 bound = tsym.type.getUpperBound();
vromero@1853 1268 }
jjg@1521 1269 return bound;
jjg@1521 1270 }
duke@1 1271
duke@1 1272 int rank_field = -1;
duke@1 1273
jjg@789 1274 @Override
duke@1 1275 public Type getLowerBound() {
duke@1 1276 return lower;
duke@1 1277 }
duke@1 1278
duke@1 1279 public TypeKind getKind() {
duke@1 1280 return TypeKind.TYPEVAR;
duke@1 1281 }
duke@1 1282
mcimadamore@79 1283 public boolean isCaptured() {
mcimadamore@79 1284 return false;
mcimadamore@79 1285 }
mcimadamore@79 1286
vromero@1853 1287 @Override
vromero@1853 1288 public boolean isReference() {
vromero@1853 1289 return true;
vromero@1853 1290 }
vromero@1853 1291
vromero@1853 1292 @Override
vromero@1853 1293 public boolean isNullOrReference() {
vromero@1853 1294 return true;
vromero@1853 1295 }
vromero@1853 1296
vromero@1853 1297 @Override
duke@1 1298 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1299 return v.visitTypeVariable(this, p);
duke@1 1300 }
duke@1 1301 }
duke@1 1302
duke@1 1303 /** A captured type variable comes from wildcards which can have
duke@1 1304 * both upper and lower bound. CapturedType extends TypeVar with
duke@1 1305 * a lower bound.
duke@1 1306 */
duke@1 1307 public static class CapturedType extends TypeVar {
duke@1 1308
duke@1 1309 public WildcardType wildcard;
duke@1 1310
duke@1 1311 public CapturedType(Name name,
duke@1 1312 Symbol owner,
duke@1 1313 Type upper,
duke@1 1314 Type lower,
duke@1 1315 WildcardType wildcard) {
duke@1 1316 super(name, owner, lower);
jjg@816 1317 this.lower = Assert.checkNonNull(lower);
duke@1 1318 this.bound = upper;
duke@1 1319 this.wildcard = wildcard;
duke@1 1320 }
duke@1 1321
duke@1 1322 @Override
duke@1 1323 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1324 return v.visitCapturedType(this, s);
duke@1 1325 }
duke@1 1326
duke@1 1327 @Override
mcimadamore@79 1328 public boolean isCaptured() {
mcimadamore@79 1329 return true;
mcimadamore@79 1330 }
mcimadamore@79 1331
mcimadamore@79 1332 @Override
duke@1 1333 public String toString() {
duke@1 1334 return "capture#"
mcimadamore@288 1335 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
duke@1 1336 + " of "
duke@1 1337 + wildcard;
duke@1 1338 }
duke@1 1339 }
duke@1 1340
duke@1 1341 public static abstract class DelegatedType extends Type {
duke@1 1342 public Type qtype;
vromero@1853 1343 public TypeTag tag;
jjg@1374 1344 public DelegatedType(TypeTag tag, Type qtype) {
vromero@1853 1345 super(qtype.tsym);
vromero@1853 1346 this.tag = tag;
duke@1 1347 this.qtype = qtype;
duke@1 1348 }
vromero@1853 1349 public TypeTag getTag() { return tag; }
duke@1 1350 public String toString() { return qtype.toString(); }
duke@1 1351 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
duke@1 1352 public Type getEnclosingType() { return qtype.getEnclosingType(); }
duke@1 1353 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
duke@1 1354 public Type getReturnType() { return qtype.getReturnType(); }
jjg@1521 1355 public Type getReceiverType() { return qtype.getReceiverType(); }
duke@1 1356 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
duke@1 1357 public List<Type> allparams() { return qtype.allparams(); }
duke@1 1358 public Type getUpperBound() { return qtype.getUpperBound(); }
duke@1 1359 public boolean isErroneous() { return qtype.isErroneous(); }
duke@1 1360 }
duke@1 1361
mcimadamore@1268 1362 /**
mcimadamore@1268 1363 * The type of a generic method type. It consists of a method type and
mcimadamore@1268 1364 * a list of method type-parameters that are used within the method
mcimadamore@1268 1365 * type.
mcimadamore@1268 1366 */
dlsmith@880 1367 public static class ForAll extends DelegatedType implements ExecutableType {
duke@1 1368 public List<Type> tvars;
duke@1 1369
duke@1 1370 public ForAll(List<Type> tvars, Type qtype) {
mcimadamore@1268 1371 super(FORALL, (MethodType)qtype);
duke@1 1372 this.tvars = tvars;
duke@1 1373 }
duke@1 1374
duke@1 1375 @Override
duke@1 1376 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1377 return v.visitForAll(this, s);
duke@1 1378 }
duke@1 1379
duke@1 1380 public String toString() {
duke@1 1381 return "<" + tvars + ">" + qtype;
duke@1 1382 }
duke@1 1383
duke@1 1384 public List<Type> getTypeArguments() { return tvars; }
duke@1 1385
duke@1 1386 public boolean isErroneous() {
duke@1 1387 return qtype.isErroneous();
duke@1 1388 }
duke@1 1389
duke@1 1390 public Type map(Mapping f) {
duke@1 1391 return f.apply(qtype);
duke@1 1392 }
duke@1 1393
duke@1 1394 public boolean contains(Type elem) {
duke@1 1395 return qtype.contains(elem);
duke@1 1396 }
duke@1 1397
duke@1 1398 public MethodType asMethodType() {
mcimadamore@1268 1399 return (MethodType)qtype;
duke@1 1400 }
duke@1 1401
duke@1 1402 public void complete() {
duke@1 1403 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
duke@1 1404 ((TypeVar)l.head).bound.complete();
duke@1 1405 }
duke@1 1406 qtype.complete();
duke@1 1407 }
duke@1 1408
duke@1 1409 public List<TypeVar> getTypeVariables() {
duke@1 1410 return List.convert(TypeVar.class, getTypeArguments());
duke@1 1411 }
duke@1 1412
duke@1 1413 public TypeKind getKind() {
duke@1 1414 return TypeKind.EXECUTABLE;
duke@1 1415 }
duke@1 1416
duke@1 1417 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1418 return v.visitExecutable(this, p);
duke@1 1419 }
duke@1 1420 }
duke@1 1421
mcimadamore@1338 1422 /** A class for inference variables, for use during method/diamond type
mcimadamore@1338 1423 * inference. An inference variable has upper/lower bounds and a set
mcimadamore@1338 1424 * of equality constraints. Such bounds are set during subtyping, type-containment,
mcimadamore@1338 1425 * type-equality checks, when the types being tested contain inference variables.
mcimadamore@1338 1426 * A change listener can be attached to an inference variable, to receive notifications
mcimadamore@1338 1427 * whenever the bounds of an inference variable change.
duke@1 1428 */
duke@1 1429 public static class UndetVar extends DelegatedType {
mcimadamore@1338 1430
mcimadamore@1338 1431 /** Inference variable change listener. The listener method is called
mcimadamore@1338 1432 * whenever a change to the inference variable's bounds occurs
mcimadamore@1338 1433 */
mcimadamore@1338 1434 public interface UndetVarListener {
mcimadamore@1338 1435 /** called when some inference variable bounds (of given kinds ibs) change */
mcimadamore@1338 1436 void varChanged(UndetVar uv, Set<InferenceBound> ibs);
mcimadamore@1338 1437 }
mcimadamore@1338 1438
mcimadamore@1338 1439 /**
mcimadamore@1338 1440 * Inference variable bound kinds
mcimadamore@1338 1441 */
mcimadamore@1338 1442 public enum InferenceBound {
mcimadamore@1338 1443 /** upper bounds */
mcimadamore@1338 1444 UPPER,
mcimadamore@1338 1445 /** lower bounds */
mcimadamore@1338 1446 LOWER,
mcimadamore@1338 1447 /** equality constraints */
mcimadamore@1338 1448 EQ;
mcimadamore@1338 1449 }
mcimadamore@1338 1450
mcimadamore@1338 1451 /** inference variable bounds */
mcimadamore@1338 1452 private Map<InferenceBound, List<Type>> bounds;
mcimadamore@1338 1453
mcimadamore@1338 1454 /** inference variable's inferred type (set from Infer.java) */
duke@1 1455 public Type inst = null;
duke@1 1456
mcimadamore@1550 1457 /** number of declared (upper) bounds */
mcimadamore@1550 1458 public int declaredCount;
mcimadamore@1550 1459
mcimadamore@1338 1460 /** inference variable's change listener */
mcimadamore@1338 1461 public UndetVarListener listener = null;
mcimadamore@1338 1462
duke@1 1463 @Override
duke@1 1464 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1465 return v.visitUndetVar(this, s);
duke@1 1466 }
duke@1 1467
mcimadamore@1338 1468 public UndetVar(TypeVar origin, Types types) {
duke@1 1469 super(UNDETVAR, origin);
mcimadamore@1338 1470 bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
mcimadamore@1550 1471 List<Type> declaredBounds = types.getBounds(origin);
mcimadamore@1550 1472 declaredCount = declaredBounds.length();
mcimadamore@1550 1473 bounds.put(InferenceBound.UPPER, declaredBounds);
mcimadamore@1338 1474 bounds.put(InferenceBound.LOWER, List.<Type>nil());
mcimadamore@1338 1475 bounds.put(InferenceBound.EQ, List.<Type>nil());
duke@1 1476 }
duke@1 1477
duke@1 1478 public String toString() {
duke@1 1479 if (inst != null) return inst.toString();
duke@1 1480 else return qtype + "?";
duke@1 1481 }
duke@1 1482
vromero@1853 1483 @Override
vromero@1853 1484 public boolean isPartial() {
vromero@1853 1485 return true;
vromero@1853 1486 }
vromero@1853 1487
vromero@1853 1488 @Override
duke@1 1489 public Type baseType() {
duke@1 1490 if (inst != null) return inst.baseType();
duke@1 1491 else return this;
duke@1 1492 }
mcimadamore@1338 1493
mcimadamore@1338 1494 /** get all bounds of a given kind */
mcimadamore@1550 1495 public List<Type> getBounds(InferenceBound... ibs) {
mcimadamore@1550 1496 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1550 1497 for (InferenceBound ib : ibs) {
mcimadamore@1550 1498 buf.appendList(bounds.get(ib));
mcimadamore@1550 1499 }
mcimadamore@1550 1500 return buf.toList();
mcimadamore@1550 1501 }
mcimadamore@1550 1502
mcimadamore@1550 1503 /** get the list of declared (upper) bounds */
mcimadamore@1550 1504 public List<Type> getDeclaredBounds() {
mcimadamore@1550 1505 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1550 1506 int count = 0;
mcimadamore@1550 1507 for (Type b : getBounds(InferenceBound.UPPER)) {
mcimadamore@1550 1508 if (count++ == declaredCount) break;
mcimadamore@1550 1509 buf.append(b);
mcimadamore@1550 1510 }
mcimadamore@1550 1511 return buf.toList();
mcimadamore@1338 1512 }
mcimadamore@1338 1513
mcimadamore@1338 1514 /** add a bound of a given kind - this might trigger listener notification */
mcimadamore@1338 1515 public void addBound(InferenceBound ib, Type bound, Types types) {
mcimadamore@1550 1516 Type bound2 = toTypeVarMap.apply(bound);
mcimadamore@1338 1517 List<Type> prevBounds = bounds.get(ib);
mcimadamore@1338 1518 for (Type b : prevBounds) {
mcimadamore@1550 1519 //check for redundancy - use strict version of isSameType on tvars
mcimadamore@1550 1520 //(as the standard version will lead to false positives w.r.t. clones ivars)
mcimadamore@1628 1521 if (types.isSameType(b, bound2, true) || bound == qtype) return;
mcimadamore@1338 1522 }
mcimadamore@1550 1523 bounds.put(ib, prevBounds.prepend(bound2));
mcimadamore@1338 1524 notifyChange(EnumSet.of(ib));
mcimadamore@1338 1525 }
mcimadamore@1550 1526 //where
mcimadamore@1550 1527 Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") {
mcimadamore@1550 1528 @Override
mcimadamore@1550 1529 public Type apply(Type t) {
mcimadamore@1550 1530 if (t.hasTag(UNDETVAR)) {
mcimadamore@1550 1531 UndetVar uv = (UndetVar)t;
mcimadamore@1550 1532 return uv.qtype;
mcimadamore@1550 1533 } else {
mcimadamore@1550 1534 return t.map(this);
mcimadamore@1550 1535 }
mcimadamore@1550 1536 }
mcimadamore@1550 1537 };
mcimadamore@1338 1538
mcimadamore@1338 1539 /** replace types in all bounds - this might trigger listener notification */
mcimadamore@1338 1540 public void substBounds(List<Type> from, List<Type> to, Types types) {
mcimadamore@1550 1541 List<Type> instVars = from.diff(to);
mcimadamore@1550 1542 //if set of instantiated ivars is empty, there's nothing to do!
mcimadamore@1550 1543 if (instVars.isEmpty()) return;
mcimadamore@1550 1544 final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class);
mcimadamore@1550 1545 UndetVarListener prevListener = listener;
mcimadamore@1550 1546 try {
mcimadamore@1550 1547 //setup new listener for keeping track of changed bounds
mcimadamore@1550 1548 listener = new UndetVarListener() {
mcimadamore@1550 1549 public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
mcimadamore@1550 1550 boundsChanged.addAll(ibs);
mcimadamore@1550 1551 }
mcimadamore@1550 1552 };
mcimadamore@1550 1553 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
mcimadamore@1550 1554 InferenceBound ib = _entry.getKey();
mcimadamore@1550 1555 List<Type> prevBounds = _entry.getValue();
mcimadamore@1550 1556 ListBuffer<Type> newBounds = ListBuffer.lb();
mcimadamore@1550 1557 ListBuffer<Type> deps = ListBuffer.lb();
mcimadamore@1550 1558 //step 1 - re-add bounds that are not dependent on ivars
mcimadamore@1550 1559 for (Type t : prevBounds) {
mcimadamore@1550 1560 if (!t.containsAny(instVars)) {
mcimadamore@1550 1561 newBounds.append(t);
mcimadamore@1550 1562 } else {
mcimadamore@1550 1563 deps.append(t);
mcimadamore@1550 1564 }
mcimadamore@1550 1565 }
mcimadamore@1550 1566 //step 2 - replace bounds
mcimadamore@1550 1567 bounds.put(ib, newBounds.toList());
mcimadamore@1550 1568 //step 3 - for each dependency, add new replaced bound
mcimadamore@1550 1569 for (Type dep : deps) {
mcimadamore@1550 1570 addBound(ib, types.subst(dep, from, to), types);
mcimadamore@1550 1571 }
mcimadamore@1338 1572 }
mcimadamore@1550 1573 } finally {
mcimadamore@1550 1574 listener = prevListener;
mcimadamore@1550 1575 if (!boundsChanged.isEmpty()) {
mcimadamore@1550 1576 notifyChange(boundsChanged);
mcimadamore@1550 1577 }
mcimadamore@1338 1578 }
mcimadamore@1338 1579 }
mcimadamore@1338 1580
mcimadamore@1338 1581 private void notifyChange(EnumSet<InferenceBound> ibs) {
mcimadamore@1338 1582 if (listener != null) {
mcimadamore@1338 1583 listener.varChanged(this, ibs);
mcimadamore@1338 1584 }
mcimadamore@1338 1585 }
duke@1 1586 }
duke@1 1587
vromero@1853 1588 /** Represents NONE.
duke@1 1589 */
vromero@1853 1590 public static class JCNoType extends Type implements NoType {
vromero@1853 1591 public JCNoType() {
vromero@1853 1592 super(null);
vromero@1853 1593 }
vromero@1853 1594
vromero@1853 1595 @Override
vromero@1853 1596 public TypeTag getTag() {
vromero@1853 1597 return NONE;
duke@1 1598 }
duke@1 1599
duke@1 1600 @Override
duke@1 1601 public TypeKind getKind() {
vromero@1853 1602 return TypeKind.NONE;
duke@1 1603 }
duke@1 1604
duke@1 1605 @Override
duke@1 1606 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1607 return v.visitNoType(this, p);
duke@1 1608 }
duke@1 1609 }
duke@1 1610
vromero@1853 1611 /** Represents VOID.
vromero@1853 1612 */
vromero@1853 1613 public static class JCVoidType extends Type implements NoType {
vromero@1853 1614
vromero@1853 1615 public JCVoidType() {
vromero@1853 1616 super(null);
vromero@1853 1617 }
vromero@1853 1618
vromero@1853 1619 @Override
vromero@1853 1620 public TypeTag getTag() {
vromero@1853 1621 return VOID;
vromero@1853 1622 }
vromero@1853 1623
vromero@1853 1624 @Override
vromero@1853 1625 public TypeKind getKind() {
vromero@1853 1626 return TypeKind.VOID;
vromero@1853 1627 }
vromero@1853 1628
vromero@1853 1629 @Override
vromero@1853 1630 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
vromero@1853 1631 return v.visitNoType(this, p);
vromero@1853 1632 }
vromero@1853 1633
vromero@1853 1634 @Override
vromero@1853 1635 public boolean isPrimitiveOrVoid() {
vromero@1853 1636 return true;
vromero@1853 1637 }
vromero@1853 1638 }
vromero@1853 1639
duke@1 1640 static class BottomType extends Type implements NullType {
duke@1 1641 public BottomType() {
vromero@1853 1642 super(null);
vromero@1853 1643 }
vromero@1853 1644
vromero@1853 1645 @Override
vromero@1853 1646 public TypeTag getTag() {
vromero@1853 1647 return BOT;
duke@1 1648 }
duke@1 1649
duke@1 1650 @Override
duke@1 1651 public TypeKind getKind() {
duke@1 1652 return TypeKind.NULL;
duke@1 1653 }
duke@1 1654
duke@1 1655 @Override
duke@1 1656 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1657 return v.visitNull(this, p);
duke@1 1658 }
duke@1 1659
duke@1 1660 @Override
duke@1 1661 public Type constType(Object value) {
duke@1 1662 return this;
duke@1 1663 }
duke@1 1664
duke@1 1665 @Override
duke@1 1666 public String stringValue() {
duke@1 1667 return "null";
duke@1 1668 }
vromero@1853 1669
vromero@1853 1670 @Override
vromero@1853 1671 public boolean isNullOrReference() {
vromero@1853 1672 return true;
vromero@1853 1673 }
vromero@1853 1674
duke@1 1675 }
duke@1 1676
duke@1 1677 public static class ErrorType extends ClassType
duke@1 1678 implements javax.lang.model.type.ErrorType {
duke@1 1679
jjg@110 1680 private Type originalType = null;
jjg@110 1681
jjg@110 1682 public ErrorType(Type originalType, TypeSymbol tsym) {
duke@1 1683 super(noType, List.<Type>nil(), null);
jjg@110 1684 this.tsym = tsym;
jjg@110 1685 this.originalType = (originalType == null ? noType : originalType);
duke@1 1686 }
duke@1 1687
jjg@110 1688 public ErrorType(ClassSymbol c, Type originalType) {
jjg@110 1689 this(originalType, c);
duke@1 1690 c.type = this;
duke@1 1691 c.kind = ERR;
duke@1 1692 c.members_field = new Scope.ErrorScope(c);
duke@1 1693 }
duke@1 1694
vromero@1853 1695 @Override
vromero@1853 1696 public TypeTag getTag() {
vromero@1853 1697 return ERROR;
vromero@1853 1698 }
vromero@1853 1699
vromero@1853 1700 @Override
vromero@1853 1701 public boolean isPartial() {
vromero@1853 1702 return true;
vromero@1853 1703 }
vromero@1853 1704
vromero@1853 1705 @Override
vromero@1853 1706 public boolean isReference() {
vromero@1853 1707 return true;
vromero@1853 1708 }
vromero@1853 1709
vromero@1853 1710 @Override
vromero@1853 1711 public boolean isNullOrReference() {
vromero@1853 1712 return true;
vromero@1853 1713 }
vromero@1853 1714
jjg@110 1715 public ErrorType(Name name, TypeSymbol container, Type originalType) {
jjg@110 1716 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
duke@1 1717 }
duke@1 1718
duke@1 1719 @Override
duke@1 1720 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1721 return v.visitErrorType(this, s);
duke@1 1722 }
duke@1 1723
duke@1 1724 public Type constType(Object constValue) { return this; }
jjg@1521 1725 public Type getEnclosingType() { return this; }
duke@1 1726 public Type getReturnType() { return this; }
duke@1 1727 public Type asSub(Symbol sym) { return this; }
duke@1 1728 public Type map(Mapping f) { return this; }
duke@1 1729
duke@1 1730 public boolean isGenType(Type t) { return true; }
duke@1 1731 public boolean isErroneous() { return true; }
duke@1 1732 public boolean isCompound() { return false; }
duke@1 1733 public boolean isInterface() { return false; }
duke@1 1734
duke@1 1735 public List<Type> allparams() { return List.nil(); }
duke@1 1736 public List<Type> getTypeArguments() { return List.nil(); }
duke@1 1737
duke@1 1738 public TypeKind getKind() {
duke@1 1739 return TypeKind.ERROR;
duke@1 1740 }
duke@1 1741
jjg@110 1742 public Type getOriginalType() {
jjg@110 1743 return originalType;
jjg@110 1744 }
jjg@110 1745
duke@1 1746 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1747 return v.visitError(this, p);
duke@1 1748 }
duke@1 1749 }
duke@1 1750
jjg@1521 1751 public static class AnnotatedType extends Type
jjg@1644 1752 implements
jjg@1644 1753 javax.lang.model.type.ArrayType,
jjg@1644 1754 javax.lang.model.type.DeclaredType,
jjg@1644 1755 javax.lang.model.type.PrimitiveType,
jjg@1644 1756 javax.lang.model.type.TypeVariable,
jjg@1644 1757 javax.lang.model.type.WildcardType {
jjg@1521 1758 /** The type annotations on this type.
jjg@1521 1759 */
jjg@1521 1760 public List<Attribute.TypeCompound> typeAnnotations;
jjg@1521 1761
jjg@1521 1762 /** The underlying type that is annotated.
jjg@1521 1763 */
jjg@1521 1764 public Type underlyingType;
jjg@1521 1765
jjg@1521 1766 public AnnotatedType(Type underlyingType) {
vromero@1853 1767 super(underlyingType.tsym);
jjg@1521 1768 this.typeAnnotations = List.nil();
jjg@1521 1769 this.underlyingType = underlyingType;
jjg@1644 1770 Assert.check(!underlyingType.isAnnotated(),
jjg@1521 1771 "Can't annotate already annotated type: " + underlyingType);
jjg@1521 1772 }
jjg@1521 1773
jjg@1521 1774 public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
jjg@1521 1775 Type underlyingType) {
vromero@1853 1776 super(underlyingType.tsym);
jjg@1521 1777 this.typeAnnotations = typeAnnotations;
jjg@1521 1778 this.underlyingType = underlyingType;
jjg@1644 1779 Assert.check(!underlyingType.isAnnotated(),
jjg@1521 1780 "Can't annotate already annotated type: " + underlyingType +
jjg@1521 1781 "; adding: " + typeAnnotations);
jjg@1521 1782 }
jjg@1521 1783
jjg@1521 1784 @Override
vromero@1853 1785 public TypeTag getTag() {
vromero@1853 1786 return underlyingType.getTag();
vromero@1853 1787 }
vromero@1853 1788
vromero@1853 1789 @Override
jjg@1644 1790 public boolean isAnnotated() {
jjg@1644 1791 return true;
jjg@1644 1792 }
jjg@1644 1793
jjg@1644 1794 @Override
jjg@1645 1795 public List<? extends Attribute.TypeCompound> getAnnotationMirrors() {
jjg@1645 1796 return typeAnnotations;
jjg@1521 1797 }
jjg@1521 1798
jjg@1521 1799 @Override
jjg@1645 1800 public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
jjg@1645 1801 return JavacAnnoConstructs.getAnnotation(this, annotationType);
jjg@1645 1802 }
jjg@1645 1803
jjg@1645 1804 @Override
jjg@1645 1805 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
jjg@1645 1806 return JavacAnnoConstructs.getAnnotationsByType(this, annotationType);
jjg@1645 1807 }
jjg@1645 1808
jjg@1645 1809 @Override
jjg@1645 1810 public TypeKind getKind() {
jjg@1645 1811 return underlyingType.getKind();
jjg@1521 1812 }
jjg@1521 1813
jjg@1521 1814 @Override
jjg@1521 1815 public Type unannotatedType() {
jjg@1521 1816 return underlyingType;
jjg@1521 1817 }
jjg@1521 1818
jjg@1521 1819 @Override
jjg@1521 1820 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
jjg@1521 1821 return v.visitAnnotatedType(this, s);
jjg@1521 1822 }
jjg@1521 1823
jjg@1521 1824 @Override
jjg@1521 1825 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
jjg@1644 1826 return underlyingType.accept(v, p);
jjg@1521 1827 }
jjg@1521 1828
jjg@1521 1829 @Override
jjg@1521 1830 public Type map(Mapping f) {
jjg@1521 1831 underlyingType.map(f);
jjg@1521 1832 return this;
jjg@1521 1833 }
jjg@1521 1834
jjg@1521 1835 @Override
jjg@1521 1836 public Type constType(Object constValue) { return underlyingType.constType(constValue); }
jjg@1521 1837 @Override
jjg@1521 1838 public Type getEnclosingType() { return underlyingType.getEnclosingType(); }
jjg@1521 1839
jjg@1521 1840 @Override
jjg@1521 1841 public Type getReturnType() { return underlyingType.getReturnType(); }
jjg@1521 1842 @Override
jjg@1521 1843 public List<Type> getTypeArguments() { return underlyingType.getTypeArguments(); }
jjg@1521 1844 @Override
jjg@1521 1845 public List<Type> getParameterTypes() { return underlyingType.getParameterTypes(); }
jjg@1521 1846 @Override
jjg@1521 1847 public Type getReceiverType() { return underlyingType.getReceiverType(); }
jjg@1521 1848 @Override
jjg@1521 1849 public List<Type> getThrownTypes() { return underlyingType.getThrownTypes(); }
jjg@1521 1850 @Override
jjg@1521 1851 public Type getUpperBound() { return underlyingType.getUpperBound(); }
jjg@1521 1852 @Override
jjg@1521 1853 public Type getLowerBound() { return underlyingType.getLowerBound(); }
jjg@1521 1854
jjg@1521 1855 @Override
jjg@1521 1856 public boolean isErroneous() { return underlyingType.isErroneous(); }
jjg@1521 1857 @Override
jjg@1521 1858 public boolean isCompound() { return underlyingType.isCompound(); }
jjg@1521 1859 @Override
jjg@1521 1860 public boolean isInterface() { return underlyingType.isInterface(); }
jjg@1521 1861 @Override
jjg@1521 1862 public List<Type> allparams() { return underlyingType.allparams(); }
jjg@1521 1863 @Override
vromero@1853 1864 public boolean isPrimitive() { return underlyingType.isPrimitive(); }
vromero@1853 1865 @Override
vromero@1853 1866 public boolean isPrimitiveOrVoid() { return underlyingType.isPrimitiveOrVoid(); }
vromero@1853 1867 @Override
jjg@1521 1868 public boolean isNumeric() { return underlyingType.isNumeric(); }
jjg@1521 1869 @Override
jjg@1521 1870 public boolean isReference() { return underlyingType.isReference(); }
jjg@1521 1871 @Override
vromero@1853 1872 public boolean isNullOrReference() { return underlyingType.isNullOrReference(); }
vromero@1853 1873 @Override
vromero@1853 1874 public boolean isPartial() { return underlyingType.isPartial(); }
vromero@1853 1875 @Override
jjg@1521 1876 public boolean isParameterized() { return underlyingType.isParameterized(); }
jjg@1521 1877 @Override
jjg@1521 1878 public boolean isRaw() { return underlyingType.isRaw(); }
jjg@1521 1879 @Override
jjg@1521 1880 public boolean isFinal() { return underlyingType.isFinal(); }
jjg@1521 1881 @Override
jjg@1521 1882 public boolean isSuperBound() { return underlyingType.isSuperBound(); }
jjg@1521 1883 @Override
jjg@1521 1884 public boolean isExtendsBound() { return underlyingType.isExtendsBound(); }
jjg@1521 1885 @Override
jjg@1521 1886 public boolean isUnbound() { return underlyingType.isUnbound(); }
jjg@1521 1887
jjg@1521 1888 @Override
jjg@1521 1889 public String toString() {
jjg@1755 1890 // This method is only used for internal debugging output.
jjg@1755 1891 // See
jjg@1755 1892 // com.sun.tools.javac.code.Printer.visitAnnotatedType(AnnotatedType, Locale)
jjg@1755 1893 // for the user-visible logic.
jjg@1521 1894 if (typeAnnotations != null &&
jjg@1521 1895 !typeAnnotations.isEmpty()) {
jjg@1521 1896 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")";
jjg@1521 1897 } else {
jjg@1521 1898 return "({} :: " + underlyingType.toString() +")";
jjg@1521 1899 }
jjg@1521 1900 }
jjg@1521 1901
jjg@1521 1902 @Override
jjg@1521 1903 public boolean contains(Type t) { return underlyingType.contains(t); }
jjg@1521 1904
jjg@1521 1905 @Override
jjg@1755 1906 public Type withTypeVar(Type t) {
jjg@1755 1907 // Don't create a new AnnotatedType, as 'this' will
jjg@1755 1908 // get its annotations set later.
jjg@1755 1909 underlyingType = underlyingType.withTypeVar(t);
jjg@1755 1910 return this;
jjg@1755 1911 }
jjg@1521 1912
jjg@1521 1913 // TODO: attach annotations?
jjg@1521 1914 @Override
jjg@1521 1915 public TypeSymbol asElement() { return underlyingType.asElement(); }
jjg@1521 1916
jjg@1521 1917 // TODO: attach annotations?
jjg@1521 1918 @Override
jjg@1521 1919 public MethodType asMethodType() { return underlyingType.asMethodType(); }
jjg@1521 1920
jjg@1521 1921 @Override
jjg@1521 1922 public void complete() { underlyingType.complete(); }
jjg@1521 1923
jjg@1521 1924 @Override
jjg@1521 1925 public TypeMirror getComponentType() { return ((ArrayType)underlyingType).getComponentType(); }
jjg@1521 1926
jjg@1521 1927 // The result is an ArrayType, but only in the model sense, not the Type sense.
jjg@1521 1928 public AnnotatedType makeVarargs() {
jjg@1521 1929 AnnotatedType atype = new AnnotatedType(((ArrayType)underlyingType).makeVarargs());
jjg@1521 1930 atype.typeAnnotations = this.typeAnnotations;
jjg@1521 1931 return atype;
jjg@1521 1932 }
jjg@1521 1933
jjg@1521 1934 @Override
jjg@1521 1935 public TypeMirror getExtendsBound() { return ((WildcardType)underlyingType).getExtendsBound(); }
jjg@1521 1936 @Override
jjg@1521 1937 public TypeMirror getSuperBound() { return ((WildcardType)underlyingType).getSuperBound(); }
jjg@1521 1938 }
jjg@1521 1939
vromero@1853 1940 public static class UnknownType extends Type {
vromero@1853 1941
vromero@1853 1942 public UnknownType() {
vromero@1853 1943 super(null);
vromero@1853 1944 }
vromero@1853 1945
vromero@1853 1946 @Override
vromero@1853 1947 public TypeTag getTag() {
vromero@1853 1948 return UNKNOWN;
vromero@1853 1949 }
vromero@1853 1950
vromero@1853 1951 @Override
vromero@1853 1952 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
vromero@1853 1953 return v.visitUnknown(this, p);
vromero@1853 1954 }
vromero@1853 1955
vromero@1853 1956 @Override
vromero@1853 1957 public boolean isPartial() {
vromero@1853 1958 return true;
vromero@1853 1959 }
vromero@1853 1960 }
vromero@1853 1961
duke@1 1962 /**
duke@1 1963 * A visitor for types. A visitor is used to implement operations
duke@1 1964 * (or relations) on types. Most common operations on types are
duke@1 1965 * binary relations and this interface is designed for binary
jjg@1521 1966 * relations, that is, operations of the form
duke@1 1967 * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
duke@1 1968 * <!-- In plain text: Type x S -> R -->
duke@1 1969 *
duke@1 1970 * @param <R> the return type of the operation implemented by this
duke@1 1971 * visitor; use Void if no return type is needed.
duke@1 1972 * @param <S> the type of the second argument (the first being the
duke@1 1973 * type itself) of the operation implemented by this visitor; use
duke@1 1974 * Void if a second argument is not needed.
duke@1 1975 */
duke@1 1976 public interface Visitor<R,S> {
duke@1 1977 R visitClassType(ClassType t, S s);
duke@1 1978 R visitWildcardType(WildcardType t, S s);
duke@1 1979 R visitArrayType(ArrayType t, S s);
duke@1 1980 R visitMethodType(MethodType t, S s);
duke@1 1981 R visitPackageType(PackageType t, S s);
duke@1 1982 R visitTypeVar(TypeVar t, S s);
duke@1 1983 R visitCapturedType(CapturedType t, S s);
duke@1 1984 R visitForAll(ForAll t, S s);
duke@1 1985 R visitUndetVar(UndetVar t, S s);
duke@1 1986 R visitErrorType(ErrorType t, S s);
jjg@1521 1987 R visitAnnotatedType(AnnotatedType t, S s);
duke@1 1988 R visitType(Type t, S s);
duke@1 1989 }
duke@1 1990 }

mercurial