1569:475eb15dfdad | 1570:f91144b7da75 |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. | 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | 4 * |
5 * This code is free software; you can redistribute it and/or modify it | 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 | 6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this | 7 * published by the Free Software Foundation. Oracle designates this |
29 import java.util.EnumMap; | 29 import java.util.EnumMap; |
30 import java.util.EnumSet; | 30 import java.util.EnumSet; |
31 import java.util.Map; | 31 import java.util.Map; |
32 import java.util.Set; | 32 import java.util.Set; |
33 | 33 |
34 import javax.lang.model.element.AnnotationMirror; | |
34 import javax.lang.model.type.*; | 35 import javax.lang.model.type.*; |
35 | 36 |
36 import com.sun.tools.javac.code.Symbol.*; | 37 import com.sun.tools.javac.code.Symbol.*; |
37 import com.sun.tools.javac.util.*; | 38 import com.sun.tools.javac.util.*; |
38 import static com.sun.tools.javac.code.BoundKind.*; | 39 import static com.sun.tools.javac.code.BoundKind.*; |
85 * | 86 * |
86 * @see TypeTag | 87 * @see TypeTag |
87 */ | 88 */ |
88 protected TypeTag tag; | 89 protected TypeTag tag; |
89 | 90 |
90 /** The defining class / interface / package / type variable | 91 /** The defining class / interface / package / type variable. |
91 */ | 92 */ |
92 public TypeSymbol tsym; | 93 public TypeSymbol tsym; |
93 | 94 |
94 /** | 95 /** |
95 * Checks if the current type tag is equal to the given tag. | 96 * Checks if the current type tag is equal to the given tag. |
164 } | 165 } |
165 | 166 |
166 /** | 167 /** |
167 * Get the representation of this type used for modelling purposes. | 168 * Get the representation of this type used for modelling purposes. |
168 * By default, this is itself. For ErrorType, a different value | 169 * By default, this is itself. For ErrorType, a different value |
169 * may be provided, | 170 * may be provided. |
170 */ | 171 */ |
171 public Type getModelType() { | 172 public Type getModelType() { |
172 return this; | 173 return this; |
173 } | 174 } |
174 | 175 |
243 */ | 244 */ |
244 public Type baseType() { | 245 public Type baseType() { |
245 return this; | 246 return this; |
246 } | 247 } |
247 | 248 |
249 /** | |
250 * If this is an annotated type, return the underlying type. | |
251 * Otherwise, return the type itself. | |
252 */ | |
253 public Type unannotatedType() { | |
254 return this; | |
255 } | |
256 | |
248 /** Return the base types of a list of types. | 257 /** Return the base types of a list of types. |
249 */ | 258 */ |
250 public static List<Type> baseTypes(List<Type> ts) { | 259 public static List<Type> baseTypes(List<Type> ts) { |
251 if (ts.nonEmpty()) { | 260 if (ts.nonEmpty()) { |
252 Type t = ts.head.baseType(); | 261 Type t = ts.head.baseType(); |
337 while (args.tail.nonEmpty()) { | 346 while (args.tail.nonEmpty()) { |
338 buf.append(args.head); | 347 buf.append(args.head); |
339 args = args.tail; | 348 args = args.tail; |
340 buf.append(','); | 349 buf.append(','); |
341 } | 350 } |
342 if (args.head.tag == ARRAY) { | 351 if (args.head.unannotatedType().tag == ARRAY) { |
343 buf.append(((ArrayType)args.head).elemtype); | 352 buf.append(((ArrayType)args.head.unannotatedType()).elemtype); |
353 if (args.head.getAnnotations().nonEmpty()) { | |
354 buf.append(args.head.getAnnotations()); | |
355 } | |
344 buf.append("..."); | 356 buf.append("..."); |
345 } else { | 357 } else { |
346 buf.append(args.head); | 358 buf.append(args.head); |
347 } | 359 } |
348 return buf.toString(); | 360 return buf.toString(); |
349 } | 361 } |
350 | 362 |
351 /** Access methods. | 363 /** Access methods. |
352 */ | 364 */ |
365 public List<? extends AnnotationMirror> getAnnotations() { return List.nil(); } | |
353 public List<Type> getTypeArguments() { return List.nil(); } | 366 public List<Type> getTypeArguments() { return List.nil(); } |
354 public Type getEnclosingType() { return null; } | 367 public Type getEnclosingType() { return null; } |
355 public List<Type> getParameterTypes() { return List.nil(); } | 368 public List<Type> getParameterTypes() { return List.nil(); } |
356 public Type getReturnType() { return null; } | 369 public Type getReturnType() { return null; } |
370 public Type getReceiverType() { return null; } | |
357 public List<Type> getThrownTypes() { return List.nil(); } | 371 public List<Type> getThrownTypes() { return List.nil(); } |
358 public Type getUpperBound() { return null; } | 372 public Type getUpperBound() { return null; } |
359 public Type getLowerBound() { return null; } | 373 public Type getLowerBound() { return null; } |
360 | 374 |
361 /** Navigation methods, these will work for classes, type variables, | 375 /** Navigation methods, these will work for classes, type variables, |
598 | 612 |
599 public static class ClassType extends Type implements DeclaredType { | 613 public static class ClassType extends Type implements DeclaredType { |
600 | 614 |
601 /** The enclosing type of this type. If this is the type of an inner | 615 /** The enclosing type of this type. If this is the type of an inner |
602 * class, outer_field refers to the type of its enclosing | 616 * class, outer_field refers to the type of its enclosing |
603 * instance class, in all other cases it referes to noType. | 617 * instance class, in all other cases it refers to noType. |
604 */ | 618 */ |
605 private Type outer_field; | 619 private Type outer_field; |
606 | 620 |
607 /** The type parameters of this type (to be set once class is loaded). | 621 /** The type parameters of this type (to be set once class is loaded). |
608 */ | 622 */ |
972 | 986 |
973 public List<Type> argtypes; | 987 public List<Type> argtypes; |
974 public Type restype; | 988 public Type restype; |
975 public List<Type> thrown; | 989 public List<Type> thrown; |
976 | 990 |
991 /** The type annotations on the method receiver. | |
992 */ | |
993 public Type recvtype; | |
994 | |
977 public MethodType(List<Type> argtypes, | 995 public MethodType(List<Type> argtypes, |
978 Type restype, | 996 Type restype, |
979 List<Type> thrown, | 997 List<Type> thrown, |
980 TypeSymbol methodClass) { | 998 TypeSymbol methodClass) { |
981 super(METHOD, methodClass); | 999 super(METHOD, methodClass); |
998 return "(" + argtypes + ")" + restype; | 1016 return "(" + argtypes + ")" + restype; |
999 } | 1017 } |
1000 | 1018 |
1001 public List<Type> getParameterTypes() { return argtypes; } | 1019 public List<Type> getParameterTypes() { return argtypes; } |
1002 public Type getReturnType() { return restype; } | 1020 public Type getReturnType() { return restype; } |
1021 public Type getReceiverType() { return recvtype; } | |
1003 public List<Type> getThrownTypes() { return thrown; } | 1022 public List<Type> getThrownTypes() { return thrown; } |
1004 | 1023 |
1005 public boolean isErroneous() { | 1024 public boolean isErroneous() { |
1006 return | 1025 return |
1007 isErroneous(argtypes) || | 1026 isErroneous(argtypes) || |
1026 | 1045 |
1027 public void complete() { | 1046 public void complete() { |
1028 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail) | 1047 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail) |
1029 l.head.complete(); | 1048 l.head.complete(); |
1030 restype.complete(); | 1049 restype.complete(); |
1050 recvtype.complete(); | |
1031 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) | 1051 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) |
1032 l.head.complete(); | 1052 l.head.complete(); |
1033 } | 1053 } |
1034 | 1054 |
1035 public List<TypeVar> getTypeVariables() { | 1055 public List<TypeVar> getTypeVariables() { |
1110 public <R,S> R accept(Type.Visitor<R,S> v, S s) { | 1130 public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
1111 return v.visitTypeVar(this, s); | 1131 return v.visitTypeVar(this, s); |
1112 } | 1132 } |
1113 | 1133 |
1114 @Override | 1134 @Override |
1115 public Type getUpperBound() { return bound; } | 1135 public Type getUpperBound() { |
1136 if ((bound == null || bound.tag == NONE) && this != tsym.type) | |
1137 bound = tsym.type.getUpperBound(); | |
1138 return bound; | |
1139 } | |
1116 | 1140 |
1117 int rank_field = -1; | 1141 int rank_field = -1; |
1118 | 1142 |
1119 @Override | 1143 @Override |
1120 public Type getLowerBound() { | 1144 public Type getLowerBound() { |
1181 public String toString() { return qtype.toString(); } | 1205 public String toString() { return qtype.toString(); } |
1182 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); } | 1206 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); } |
1183 public Type getEnclosingType() { return qtype.getEnclosingType(); } | 1207 public Type getEnclosingType() { return qtype.getEnclosingType(); } |
1184 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); } | 1208 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); } |
1185 public Type getReturnType() { return qtype.getReturnType(); } | 1209 public Type getReturnType() { return qtype.getReturnType(); } |
1210 public Type getReceiverType() { return qtype.getReceiverType(); } | |
1186 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); } | 1211 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); } |
1187 public List<Type> allparams() { return qtype.allparams(); } | 1212 public List<Type> allparams() { return qtype.allparams(); } |
1188 public Type getUpperBound() { return qtype.getUpperBound(); } | 1213 public Type getUpperBound() { return qtype.getUpperBound(); } |
1189 public boolean isErroneous() { return qtype.isErroneous(); } | 1214 public boolean isErroneous() { return qtype.isErroneous(); } |
1190 } | 1215 } |
1433 public <R,S> R accept(Type.Visitor<R,S> v, S s) { | 1458 public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
1434 return v.visitErrorType(this, s); | 1459 return v.visitErrorType(this, s); |
1435 } | 1460 } |
1436 | 1461 |
1437 public Type constType(Object constValue) { return this; } | 1462 public Type constType(Object constValue) { return this; } |
1438 public Type getEnclosingType() { return this; } | 1463 public Type getEnclosingType() { return this; } |
1439 public Type getReturnType() { return this; } | 1464 public Type getReturnType() { return this; } |
1440 public Type asSub(Symbol sym) { return this; } | 1465 public Type asSub(Symbol sym) { return this; } |
1441 public Type map(Mapping f) { return this; } | 1466 public Type map(Mapping f) { return this; } |
1442 | 1467 |
1443 public boolean isGenType(Type t) { return true; } | 1468 public boolean isGenType(Type t) { return true; } |
1459 public <R, P> R accept(TypeVisitor<R, P> v, P p) { | 1484 public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
1460 return v.visitError(this, p); | 1485 return v.visitError(this, p); |
1461 } | 1486 } |
1462 } | 1487 } |
1463 | 1488 |
1489 public static class AnnotatedType extends Type | |
1490 implements javax.lang.model.type.AnnotatedType { | |
1491 /** The type annotations on this type. | |
1492 */ | |
1493 public List<Attribute.TypeCompound> typeAnnotations; | |
1494 | |
1495 /** The underlying type that is annotated. | |
1496 */ | |
1497 public Type underlyingType; | |
1498 | |
1499 public AnnotatedType(Type underlyingType) { | |
1500 super(underlyingType.tag, underlyingType.tsym); | |
1501 this.typeAnnotations = List.nil(); | |
1502 this.underlyingType = underlyingType; | |
1503 Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED, | |
1504 "Can't annotate already annotated type: " + underlyingType); | |
1505 } | |
1506 | |
1507 public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations, | |
1508 Type underlyingType) { | |
1509 super(underlyingType.tag, underlyingType.tsym); | |
1510 this.typeAnnotations = typeAnnotations; | |
1511 this.underlyingType = underlyingType; | |
1512 Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED, | |
1513 "Can't annotate already annotated type: " + underlyingType + | |
1514 "; adding: " + typeAnnotations); | |
1515 } | |
1516 | |
1517 @Override | |
1518 public TypeKind getKind() { | |
1519 return TypeKind.ANNOTATED; | |
1520 } | |
1521 | |
1522 @Override | |
1523 public List<? extends AnnotationMirror> getAnnotations() { | |
1524 return typeAnnotations; | |
1525 } | |
1526 | |
1527 @Override | |
1528 public TypeMirror getUnderlyingType() { | |
1529 return underlyingType; | |
1530 } | |
1531 | |
1532 @Override | |
1533 public Type unannotatedType() { | |
1534 return underlyingType; | |
1535 } | |
1536 | |
1537 @Override | |
1538 public <R,S> R accept(Type.Visitor<R,S> v, S s) { | |
1539 return v.visitAnnotatedType(this, s); | |
1540 } | |
1541 | |
1542 @Override | |
1543 public <R, P> R accept(TypeVisitor<R, P> v, P p) { | |
1544 return v.visitAnnotated(this, p); | |
1545 } | |
1546 | |
1547 @Override | |
1548 public Type map(Mapping f) { | |
1549 underlyingType.map(f); | |
1550 return this; | |
1551 } | |
1552 | |
1553 @Override | |
1554 public Type constType(Object constValue) { return underlyingType.constType(constValue); } | |
1555 @Override | |
1556 public Type getEnclosingType() { return underlyingType.getEnclosingType(); } | |
1557 | |
1558 @Override | |
1559 public Type getReturnType() { return underlyingType.getReturnType(); } | |
1560 @Override | |
1561 public List<Type> getTypeArguments() { return underlyingType.getTypeArguments(); } | |
1562 @Override | |
1563 public List<Type> getParameterTypes() { return underlyingType.getParameterTypes(); } | |
1564 @Override | |
1565 public Type getReceiverType() { return underlyingType.getReceiverType(); } | |
1566 @Override | |
1567 public List<Type> getThrownTypes() { return underlyingType.getThrownTypes(); } | |
1568 @Override | |
1569 public Type getUpperBound() { return underlyingType.getUpperBound(); } | |
1570 @Override | |
1571 public Type getLowerBound() { return underlyingType.getLowerBound(); } | |
1572 | |
1573 @Override | |
1574 public boolean isErroneous() { return underlyingType.isErroneous(); } | |
1575 @Override | |
1576 public boolean isCompound() { return underlyingType.isCompound(); } | |
1577 @Override | |
1578 public boolean isInterface() { return underlyingType.isInterface(); } | |
1579 @Override | |
1580 public List<Type> allparams() { return underlyingType.allparams(); } | |
1581 @Override | |
1582 public boolean isNumeric() { return underlyingType.isNumeric(); } | |
1583 @Override | |
1584 public boolean isReference() { return underlyingType.isReference(); } | |
1585 @Override | |
1586 public boolean isParameterized() { return underlyingType.isParameterized(); } | |
1587 @Override | |
1588 public boolean isRaw() { return underlyingType.isRaw(); } | |
1589 @Override | |
1590 public boolean isFinal() { return underlyingType.isFinal(); } | |
1591 @Override | |
1592 public boolean isSuperBound() { return underlyingType.isSuperBound(); } | |
1593 @Override | |
1594 public boolean isExtendsBound() { return underlyingType.isExtendsBound(); } | |
1595 @Override | |
1596 public boolean isUnbound() { return underlyingType.isUnbound(); } | |
1597 | |
1598 @Override | |
1599 public String toString() { | |
1600 // TODO more logic for arrays, etc. | |
1601 if (typeAnnotations != null && | |
1602 !typeAnnotations.isEmpty()) { | |
1603 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")"; | |
1604 } else { | |
1605 return "({} :: " + underlyingType.toString() +")"; | |
1606 } | |
1607 } | |
1608 | |
1609 @Override | |
1610 public boolean contains(Type t) { return underlyingType.contains(t); } | |
1611 | |
1612 // TODO: attach annotations? | |
1613 @Override | |
1614 public Type withTypeVar(Type t) { return underlyingType.withTypeVar(t); } | |
1615 | |
1616 // TODO: attach annotations? | |
1617 @Override | |
1618 public TypeSymbol asElement() { return underlyingType.asElement(); } | |
1619 | |
1620 // TODO: attach annotations? | |
1621 @Override | |
1622 public MethodType asMethodType() { return underlyingType.asMethodType(); } | |
1623 | |
1624 @Override | |
1625 public void complete() { underlyingType.complete(); } | |
1626 | |
1627 @Override | |
1628 public TypeMirror getComponentType() { return ((ArrayType)underlyingType).getComponentType(); } | |
1629 | |
1630 // The result is an ArrayType, but only in the model sense, not the Type sense. | |
1631 public AnnotatedType makeVarargs() { | |
1632 AnnotatedType atype = new AnnotatedType(((ArrayType)underlyingType).makeVarargs()); | |
1633 atype.typeAnnotations = this.typeAnnotations; | |
1634 return atype; | |
1635 } | |
1636 | |
1637 @Override | |
1638 public TypeMirror getExtendsBound() { return ((WildcardType)underlyingType).getExtendsBound(); } | |
1639 @Override | |
1640 public TypeMirror getSuperBound() { return ((WildcardType)underlyingType).getSuperBound(); } | |
1641 } | |
1642 | |
1464 /** | 1643 /** |
1465 * A visitor for types. A visitor is used to implement operations | 1644 * A visitor for types. A visitor is used to implement operations |
1466 * (or relations) on types. Most common operations on types are | 1645 * (or relations) on types. Most common operations on types are |
1467 * binary relations and this interface is designed for binary | 1646 * binary relations and this interface is designed for binary |
1468 * relations, that is, operations on the form | 1647 * relations, that is, operations of the form |
1469 * Type × S → R. | 1648 * Type × S → R. |
1470 * <!-- In plain text: Type x S -> R --> | 1649 * <!-- In plain text: Type x S -> R --> |
1471 * | 1650 * |
1472 * @param <R> the return type of the operation implemented by this | 1651 * @param <R> the return type of the operation implemented by this |
1473 * visitor; use Void if no return type is needed. | 1652 * visitor; use Void if no return type is needed. |
1484 R visitTypeVar(TypeVar t, S s); | 1663 R visitTypeVar(TypeVar t, S s); |
1485 R visitCapturedType(CapturedType t, S s); | 1664 R visitCapturedType(CapturedType t, S s); |
1486 R visitForAll(ForAll t, S s); | 1665 R visitForAll(ForAll t, S s); |
1487 R visitUndetVar(UndetVar t, S s); | 1666 R visitUndetVar(UndetVar t, S s); |
1488 R visitErrorType(ErrorType t, S s); | 1667 R visitErrorType(ErrorType t, S s); |
1668 R visitAnnotatedType(AnnotatedType t, S s); | |
1489 R visitType(Type t, S s); | 1669 R visitType(Type t, S s); |
1490 } | 1670 } |
1491 } | 1671 } |