src/share/classes/javax/lang/model/util/Types.java

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 972
694ff82ca68e
child 1644
40adaf938847
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

duke@1 1 /*
jjg@1521 2 * Copyright (c) 2005, 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 javax.lang.model.util;
duke@1 27
jjg@1521 28 import java.lang.annotation.Annotation;
jjg@1521 29 import java.lang.annotation.AnnotationTypeMismatchException;
jjg@1521 30 import java.lang.annotation.IncompleteAnnotationException;
duke@1 31 import java.util.List;
duke@1 32 import javax.lang.model.element.*;
duke@1 33 import javax.lang.model.type.*;
duke@1 34
duke@1 35 /**
duke@1 36 * Utility methods for operating on types.
duke@1 37 *
duke@1 38 * <p><b>Compatibility Note:</b> Methods may be added to this interface
duke@1 39 * in future releases of the platform.
duke@1 40 *
duke@1 41 * @author Joseph D. Darcy
duke@1 42 * @author Scott Seligman
duke@1 43 * @author Peter von der Ah&eacute;
duke@1 44 * @see javax.annotation.processing.ProcessingEnvironment#getTypeUtils
duke@1 45 * @since 1.6
duke@1 46 */
duke@1 47 public interface Types {
duke@1 48
duke@1 49 /**
duke@1 50 * Returns the element corresponding to a type.
duke@1 51 * The type may be a {@code DeclaredType} or {@code TypeVariable}.
duke@1 52 * Returns {@code null} if the type is not one with a
duke@1 53 * corresponding element.
duke@1 54 *
duke@1 55 * @return the element corresponding to the given type
duke@1 56 */
duke@1 57 Element asElement(TypeMirror t);
duke@1 58
duke@1 59 /**
duke@1 60 * Tests whether two {@code TypeMirror} objects represent the same type.
duke@1 61 *
duke@1 62 * <p>Caveat: if either of the arguments to this method represents a
duke@1 63 * wildcard, this method will return false. As a consequence, a wildcard
duke@1 64 * is not the same type as itself. This might be surprising at first,
duke@1 65 * but makes sense once you consider that an example like this must be
duke@1 66 * rejected by the compiler:
duke@1 67 * <pre>
duke@1 68 * {@code List<?> list = new ArrayList<Object>();}
duke@1 69 * {@code list.add(list.get(0));}
duke@1 70 * </pre>
duke@1 71 *
duke@1 72 * @param t1 the first type
duke@1 73 * @param t2 the second type
duke@1 74 * @return {@code true} if and only if the two types are the same
duke@1 75 */
duke@1 76 boolean isSameType(TypeMirror t1, TypeMirror t2);
duke@1 77
duke@1 78 /**
duke@1 79 * Tests whether one type is a subtype of another.
duke@1 80 * Any type is considered to be a subtype of itself.
duke@1 81 *
duke@1 82 * @param t1 the first type
duke@1 83 * @param t2 the second type
duke@1 84 * @return {@code true} if and only if the first type is a subtype
duke@1 85 * of the second
duke@1 86 * @throws IllegalArgumentException if given an executable or package type
jjh@972 87 * @jls 4.10 Subtyping
duke@1 88 */
duke@1 89 boolean isSubtype(TypeMirror t1, TypeMirror t2);
duke@1 90
duke@1 91 /**
duke@1 92 * Tests whether one type is assignable to another.
duke@1 93 *
duke@1 94 * @param t1 the first type
duke@1 95 * @param t2 the second type
duke@1 96 * @return {@code true} if and only if the first type is assignable
duke@1 97 * to the second
duke@1 98 * @throws IllegalArgumentException if given an executable or package type
jjh@972 99 * @jls 5.2 Assignment Conversion
duke@1 100 */
duke@1 101 boolean isAssignable(TypeMirror t1, TypeMirror t2);
duke@1 102
duke@1 103 /**
duke@1 104 * Tests whether one type argument <i>contains</i> another.
duke@1 105 *
duke@1 106 * @param t1 the first type
duke@1 107 * @param t2 the second type
duke@1 108 * @return {@code true} if and only if the first type contains the second
duke@1 109 * @throws IllegalArgumentException if given an executable or package type
jjh@972 110 * @jls 4.5.1.1 Type Argument Containment and Equivalence
duke@1 111 */
duke@1 112 boolean contains(TypeMirror t1, TypeMirror t2);
duke@1 113
duke@1 114 /**
duke@1 115 * Tests whether the signature of one method is a <i>subsignature</i>
duke@1 116 * of another.
duke@1 117 *
duke@1 118 * @param m1 the first method
duke@1 119 * @param m2 the second method
duke@1 120 * @return {@code true} if and only if the first signature is a
duke@1 121 * subsignature of the second
jjh@972 122 * @jls 8.4.2 Method Signature
duke@1 123 */
duke@1 124 boolean isSubsignature(ExecutableType m1, ExecutableType m2);
duke@1 125
duke@1 126 /**
duke@1 127 * Returns the direct supertypes of a type. The interface types, if any,
duke@1 128 * will appear last in the list.
duke@1 129 *
duke@1 130 * @param t the type being examined
duke@1 131 * @return the direct supertypes, or an empty list if none
duke@1 132 * @throws IllegalArgumentException if given an executable or package type
duke@1 133 */
duke@1 134 List<? extends TypeMirror> directSupertypes(TypeMirror t);
duke@1 135
duke@1 136 /**
duke@1 137 * Returns the erasure of a type.
duke@1 138 *
duke@1 139 * @param t the type to be erased
duke@1 140 * @return the erasure of the given type
duke@1 141 * @throws IllegalArgumentException if given a package type
jjh@972 142 * @jls 4.6 Type Erasure
duke@1 143 */
duke@1 144 TypeMirror erasure(TypeMirror t);
duke@1 145
duke@1 146 /**
duke@1 147 * Returns the class of a boxed value of a given primitive type.
duke@1 148 * That is, <i>boxing conversion</i> is applied.
duke@1 149 *
duke@1 150 * @param p the primitive type to be converted
duke@1 151 * @return the class of a boxed value of type {@code p}
jjh@972 152 * @jls 5.1.7 Boxing Conversion
duke@1 153 */
duke@1 154 TypeElement boxedClass(PrimitiveType p);
duke@1 155
duke@1 156 /**
duke@1 157 * Returns the type (a primitive type) of unboxed values of a given type.
duke@1 158 * That is, <i>unboxing conversion</i> is applied.
duke@1 159 *
duke@1 160 * @param t the type to be unboxed
duke@1 161 * @return the type of an unboxed value of type {@code t}
duke@1 162 * @throws IllegalArgumentException if the given type has no
duke@1 163 * unboxing conversion
jjh@972 164 * @jls 5.1.8 Unboxing Conversion
duke@1 165 */
duke@1 166 PrimitiveType unboxedType(TypeMirror t);
duke@1 167
duke@1 168 /**
duke@1 169 * Applies capture conversion to a type.
duke@1 170 *
duke@1 171 * @param t the type to be converted
duke@1 172 * @return the result of applying capture conversion
duke@1 173 * @throws IllegalArgumentException if given an executable or package type
jjh@972 174 * @jls 5.1.10 Capture Conversion
duke@1 175 */
duke@1 176 TypeMirror capture(TypeMirror t);
duke@1 177
duke@1 178 /**
duke@1 179 * Returns a primitive type.
duke@1 180 *
duke@1 181 * @param kind the kind of primitive type to return
duke@1 182 * @return a primitive type
duke@1 183 * @throws IllegalArgumentException if {@code kind} is not a primitive kind
duke@1 184 */
duke@1 185 PrimitiveType getPrimitiveType(TypeKind kind);
duke@1 186
duke@1 187 /**
duke@1 188 * Returns the null type. This is the type of {@code null}.
duke@1 189 *
duke@1 190 * @return the null type
duke@1 191 */
duke@1 192 NullType getNullType();
duke@1 193
duke@1 194 /**
duke@1 195 * Returns a pseudo-type used where no actual type is appropriate.
duke@1 196 * The kind of type to return may be either
duke@1 197 * {@link TypeKind#VOID VOID} or {@link TypeKind#NONE NONE}.
duke@1 198 * For packages, use
duke@1 199 * {@link Elements#getPackageElement(CharSequence)}{@code .asType()}
duke@1 200 * instead.
duke@1 201 *
duke@1 202 * @param kind the kind of type to return
duke@1 203 * @return a pseudo-type of kind {@code VOID} or {@code NONE}
duke@1 204 * @throws IllegalArgumentException if {@code kind} is not valid
duke@1 205 */
duke@1 206 NoType getNoType(TypeKind kind);
duke@1 207
duke@1 208 /**
duke@1 209 * Returns an array type with the specified component type.
duke@1 210 *
duke@1 211 * @param componentType the component type
duke@1 212 * @return an array type with the specified component type.
duke@1 213 * @throws IllegalArgumentException if the component type is not valid for
duke@1 214 * an array
duke@1 215 */
duke@1 216 ArrayType getArrayType(TypeMirror componentType);
duke@1 217
duke@1 218 /**
duke@1 219 * Returns a new wildcard type argument. Either of the wildcard's
duke@1 220 * bounds may be specified, or neither, but not both.
duke@1 221 *
duke@1 222 * @param extendsBound the extends (upper) bound, or {@code null} if none
duke@1 223 * @param superBound the super (lower) bound, or {@code null} if none
duke@1 224 * @return a new wildcard
duke@1 225 * @throws IllegalArgumentException if bounds are not valid
duke@1 226 */
duke@1 227 WildcardType getWildcardType(TypeMirror extendsBound,
duke@1 228 TypeMirror superBound);
duke@1 229
duke@1 230 /**
duke@1 231 * Returns the type corresponding to a type element and
duke@1 232 * actual type arguments.
duke@1 233 * Given the type element for {@code Set} and the type mirror
duke@1 234 * for {@code String},
duke@1 235 * for example, this method may be used to get the
duke@1 236 * parameterized type {@code Set<String>}.
duke@1 237 *
duke@1 238 * <p> The number of type arguments must either equal the
duke@1 239 * number of the type element's formal type parameters, or must be
duke@1 240 * zero. If zero, and if the type element is generic,
duke@1 241 * then the type element's raw type is returned.
duke@1 242 *
duke@1 243 * <p> If a parameterized type is being returned, its type element
duke@1 244 * must not be contained within a generic outer class.
duke@1 245 * The parameterized type {@code Outer<String>.Inner<Number>},
duke@1 246 * for example, may be constructed by first using this
duke@1 247 * method to get the type {@code Outer<String>}, and then invoking
duke@1 248 * {@link #getDeclaredType(DeclaredType, TypeElement, TypeMirror...)}.
duke@1 249 *
duke@1 250 * @param typeElem the type element
duke@1 251 * @param typeArgs the actual type arguments
duke@1 252 * @return the type corresponding to the type element and
duke@1 253 * actual type arguments
duke@1 254 * @throws IllegalArgumentException if too many or too few
duke@1 255 * type arguments are given, or if an inappropriate type
duke@1 256 * argument or type element is provided
duke@1 257 */
duke@1 258 DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs);
duke@1 259
duke@1 260 /**
duke@1 261 * Returns the type corresponding to a type element
duke@1 262 * and actual type arguments, given a
duke@1 263 * {@linkplain DeclaredType#getEnclosingType() containing type}
duke@1 264 * of which it is a member.
duke@1 265 * The parameterized type {@code Outer<String>.Inner<Number>},
duke@1 266 * for example, may be constructed by first using
duke@1 267 * {@link #getDeclaredType(TypeElement, TypeMirror...)}
duke@1 268 * to get the type {@code Outer<String>}, and then invoking
duke@1 269 * this method.
duke@1 270 *
duke@1 271 * <p> If the containing type is a parameterized type,
duke@1 272 * the number of type arguments must equal the
duke@1 273 * number of {@code typeElem}'s formal type parameters.
duke@1 274 * If it is not parameterized or if it is {@code null}, this method is
duke@1 275 * equivalent to {@code getDeclaredType(typeElem, typeArgs)}.
duke@1 276 *
duke@1 277 * @param containing the containing type, or {@code null} if none
duke@1 278 * @param typeElem the type element
duke@1 279 * @param typeArgs the actual type arguments
duke@1 280 * @return the type corresponding to the type element and
duke@1 281 * actual type arguments, contained within the given type
duke@1 282 * @throws IllegalArgumentException if too many or too few
duke@1 283 * type arguments are given, or if an inappropriate type
duke@1 284 * argument, type element, or containing type is provided
duke@1 285 */
duke@1 286 DeclaredType getDeclaredType(DeclaredType containing,
duke@1 287 TypeElement typeElem, TypeMirror... typeArgs);
duke@1 288
duke@1 289 /**
duke@1 290 * Returns the type of an element when that element is viewed as
duke@1 291 * a member of, or otherwise directly contained by, a given type.
duke@1 292 * For example,
duke@1 293 * when viewed as a member of the parameterized type {@code Set<String>},
duke@1 294 * the {@code Set.add} method is an {@code ExecutableType}
duke@1 295 * whose parameter is of type {@code String}.
duke@1 296 *
duke@1 297 * @param containing the containing type
duke@1 298 * @param element the element
duke@1 299 * @return the type of the element as viewed from the containing type
duke@1 300 * @throws IllegalArgumentException if the element is not a valid one
duke@1 301 * for the given type
duke@1 302 */
duke@1 303 TypeMirror asMemberOf(DeclaredType containing, Element element);
jjg@1521 304
jjg@1521 305 /**
jjg@1521 306 * Returns the annotations targeting the type.
jjg@1521 307 *
jjg@1521 308 * @param type the targeted type
jjg@1521 309 * @return the type annotations targeting the type
jjg@1521 310 */
jjg@1521 311 List<? extends AnnotationMirror> typeAnnotationsOf(TypeMirror type);
jjg@1521 312
jjg@1521 313 /**
jjg@1521 314 * Returns the type's annotation for the specified type if
jjg@1521 315 * such an annotation is present, else {@code null}. The
jjg@1521 316 * annotation has to be directly present on this
jjg@1521 317 * element.
jjg@1521 318 *
jjg@1521 319 * <p> The annotation returned by this method could contain an element
jjg@1521 320 * whose value is of type {@code Class}.
jjg@1521 321 * This value cannot be returned directly: information necessary to
jjg@1521 322 * locate and load a class (such as the class loader to use) is
jjg@1521 323 * not available, and the class might not be loadable at all.
jjg@1521 324 * Attempting to read a {@code Class} object by invoking the relevant
jjg@1521 325 * method on the returned annotation
jjg@1521 326 * will result in a {@link MirroredTypeException},
jjg@1521 327 * from which the corresponding {@link TypeMirror} may be extracted.
jjg@1521 328 * Similarly, attempting to read a {@code Class[]}-valued element
jjg@1521 329 * will result in a {@link MirroredTypesException}.
jjg@1521 330 *
jjg@1521 331 * <blockquote>
jjg@1521 332 * <i>Note:</i> This method is unlike others in this and related
jjg@1521 333 * interfaces. It operates on runtime reflective information &mdash;
jjg@1521 334 * representations of annotation types currently loaded into the
jjg@1521 335 * VM &mdash; rather than on the representations defined by and used
jjg@1521 336 * throughout these interfaces. Consequently, calling methods on
jjg@1521 337 * the returned annotation object can throw many of the exceptions
jjg@1521 338 * that can be thrown when calling methods on an annotation object
jjg@1521 339 * returned by core reflection. This method is intended for
jjg@1521 340 * callers that are written to operate on a known, fixed set of
jjg@1521 341 * annotation types.
jjg@1521 342 * </blockquote>
jjg@1521 343 *
jjg@1521 344 * @param <A> the annotation type
jjg@1521 345 * @param type the targeted type
jjg@1521 346 * @param annotationType the {@code Class} object corresponding to
jjg@1521 347 * the annotation type
jjg@1521 348 * @return the type's annotation for the specified annotation
jjg@1521 349 * type if present on the type, else {@code null}
jjg@1521 350 *
jjg@1521 351 * @see Element#getAnnotationMirrors()
jjg@1521 352 * @see EnumConstantNotPresentException
jjg@1521 353 * @see AnnotationTypeMismatchException
jjg@1521 354 * @see IncompleteAnnotationException
jjg@1521 355 * @see MirroredTypeException
jjg@1521 356 * @see MirroredTypesException
jjg@1521 357 */
jjg@1521 358 <A extends Annotation> A typeAnnotationOf(TypeMirror type, Class<A> annotationType);
jjg@1521 359
jjg@1521 360 /**
jjg@1521 361 * Returns the annotations targeting the method receiver type.
jjg@1521 362 *
jjg@1521 363 * @param type the targeted type
jjg@1521 364 * @return the receiver type of the executable type
jjg@1521 365 */
jjg@1521 366 TypeMirror receiverTypeOf(ExecutableType type);
jjg@1521 367
jjg@1521 368 /**
jjg@1521 369 * Returns the type's annotation for the specified executable type
jjg@1521 370 * receiver if such an annotation is present, else {@code null}. The
jjg@1521 371 * annotation has to be directly present on this
jjg@1521 372 * element.
jjg@1521 373 *
jjg@1521 374 * <p> The annotation returned by this method could contain an element
jjg@1521 375 * whose value is of type {@code Class}.
jjg@1521 376 * This value cannot be returned directly: information necessary to
jjg@1521 377 * locate and load a class (such as the class loader to use) is
jjg@1521 378 * not available, and the class might not be loadable at all.
jjg@1521 379 * Attempting to read a {@code Class} object by invoking the relevant
jjg@1521 380 * method on the returned annotation
jjg@1521 381 * will result in a {@link MirroredTypeException},
jjg@1521 382 * from which the corresponding {@link TypeMirror} may be extracted.
jjg@1521 383 * Similarly, attempting to read a {@code Class[]}-valued element
jjg@1521 384 * will result in a {@link MirroredTypesException}.
jjg@1521 385 *
jjg@1521 386 * <blockquote>
jjg@1521 387 * <i>Note:</i> This method is unlike others in this and related
jjg@1521 388 * interfaces. It operates on runtime reflective information &mdash;
jjg@1521 389 * representations of annotation types currently loaded into the
jjg@1521 390 * VM &mdash; rather than on the representations defined by and used
jjg@1521 391 * throughout these interfaces. Consequently, calling methods on
jjg@1521 392 * the returned annotation object can throw many of the exceptions
jjg@1521 393 * that can be thrown when calling methods on an annotation object
jjg@1521 394 * returned by core reflection. This method is intended for
jjg@1521 395 * callers that are written to operate on a known, fixed set of
jjg@1521 396 * annotation types.
jjg@1521 397 * </blockquote>
jjg@1521 398 *
jjg@1521 399 * @param <A> the annotation type
jjg@1521 400 * @param type the method type
jjg@1521 401 * @param annotationType the {@code Class} object corresponding to
jjg@1521 402 * the annotation type
jjg@1521 403 * @return the type's annotation for the specified annotation
jjg@1521 404 * type if present on the type, else {@code null}
jjg@1521 405 *
jjg@1521 406 * @see Element#getAnnotationMirrors()
jjg@1521 407 * @see EnumConstantNotPresentException
jjg@1521 408 * @see AnnotationTypeMismatchException
jjg@1521 409 * @see IncompleteAnnotationException
jjg@1521 410 * @see MirroredTypeException
jjg@1521 411 * @see MirroredTypesException
jjg@1521 412 */
jjg@1521 413 // TODO: no longer needed?
jjg@1521 414 // <A extends Annotation> A receiverTypeAnnotationOf(ExecutableType type, Class<A> annotationType);
jjg@1521 415
duke@1 416 }

mercurial