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

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package javax.lang.model.util;
aoqi@0 27
aoqi@0 28 import java.lang.annotation.Annotation;
aoqi@0 29 import java.lang.annotation.AnnotationTypeMismatchException;
aoqi@0 30 import java.lang.annotation.IncompleteAnnotationException;
aoqi@0 31 import java.util.List;
aoqi@0 32 import javax.lang.model.element.*;
aoqi@0 33 import javax.lang.model.type.*;
aoqi@0 34
aoqi@0 35 /**
aoqi@0 36 * Utility methods for operating on types.
aoqi@0 37 *
aoqi@0 38 * <p><b>Compatibility Note:</b> Methods may be added to this interface
aoqi@0 39 * in future releases of the platform.
aoqi@0 40 *
aoqi@0 41 * @author Joseph D. Darcy
aoqi@0 42 * @author Scott Seligman
aoqi@0 43 * @author Peter von der Ah&eacute;
aoqi@0 44 * @see javax.annotation.processing.ProcessingEnvironment#getTypeUtils
aoqi@0 45 * @since 1.6
aoqi@0 46 */
aoqi@0 47 public interface Types {
aoqi@0 48
aoqi@0 49 /**
aoqi@0 50 * Returns the element corresponding to a type.
aoqi@0 51 * The type may be a {@code DeclaredType} or {@code TypeVariable}.
aoqi@0 52 * Returns {@code null} if the type is not one with a
aoqi@0 53 * corresponding element.
aoqi@0 54 *
aoqi@0 55 * @param t the type to map to an element
aoqi@0 56 * @return the element corresponding to the given type
aoqi@0 57 */
aoqi@0 58 Element asElement(TypeMirror t);
aoqi@0 59
aoqi@0 60 /**
aoqi@0 61 * Tests whether two {@code TypeMirror} objects represent the same type.
aoqi@0 62 *
aoqi@0 63 * <p>Caveat: if either of the arguments to this method represents a
aoqi@0 64 * wildcard, this method will return false. As a consequence, a wildcard
aoqi@0 65 * is not the same type as itself. This might be surprising at first,
aoqi@0 66 * but makes sense once you consider that an example like this must be
aoqi@0 67 * rejected by the compiler:
aoqi@0 68 * <pre>
aoqi@0 69 * {@code List<?> list = new ArrayList<Object>();}
aoqi@0 70 * {@code list.add(list.get(0));}
aoqi@0 71 * </pre>
aoqi@0 72 *
aoqi@0 73 * <p>Since annotations are only meta-data associated with a type,
aoqi@0 74 * the set of annotations on either argument is <em>not</em> taken
aoqi@0 75 * into account when computing whether or not two {@code
aoqi@0 76 * TypeMirror} objects are the same type. In particular, two
aoqi@0 77 * {@code TypeMirror} objects can have different annotations and
aoqi@0 78 * still be considered the same.
aoqi@0 79 *
aoqi@0 80 * @param t1 the first type
aoqi@0 81 * @param t2 the second type
aoqi@0 82 * @return {@code true} if and only if the two types are the same
aoqi@0 83 */
aoqi@0 84 boolean isSameType(TypeMirror t1, TypeMirror t2);
aoqi@0 85
aoqi@0 86 /**
aoqi@0 87 * Tests whether one type is a subtype of another.
aoqi@0 88 * Any type is considered to be a subtype of itself.
aoqi@0 89 *
aoqi@0 90 * @param t1 the first type
aoqi@0 91 * @param t2 the second type
aoqi@0 92 * @return {@code true} if and only if the first type is a subtype
aoqi@0 93 * of the second
aoqi@0 94 * @throws IllegalArgumentException if given an executable or package type
aoqi@0 95 * @jls 4.10 Subtyping
aoqi@0 96 */
aoqi@0 97 boolean isSubtype(TypeMirror t1, TypeMirror t2);
aoqi@0 98
aoqi@0 99 /**
aoqi@0 100 * Tests whether one type is assignable to another.
aoqi@0 101 *
aoqi@0 102 * @param t1 the first type
aoqi@0 103 * @param t2 the second type
aoqi@0 104 * @return {@code true} if and only if the first type is assignable
aoqi@0 105 * to the second
aoqi@0 106 * @throws IllegalArgumentException if given an executable or package type
aoqi@0 107 * @jls 5.2 Assignment Conversion
aoqi@0 108 */
aoqi@0 109 boolean isAssignable(TypeMirror t1, TypeMirror t2);
aoqi@0 110
aoqi@0 111 /**
aoqi@0 112 * Tests whether one type argument <i>contains</i> another.
aoqi@0 113 *
aoqi@0 114 * @param t1 the first type
aoqi@0 115 * @param t2 the second type
aoqi@0 116 * @return {@code true} if and only if the first type contains the second
aoqi@0 117 * @throws IllegalArgumentException if given an executable or package type
aoqi@0 118 * @jls 4.5.1.1 Type Argument Containment and Equivalence
aoqi@0 119 */
aoqi@0 120 boolean contains(TypeMirror t1, TypeMirror t2);
aoqi@0 121
aoqi@0 122 /**
aoqi@0 123 * Tests whether the signature of one method is a <i>subsignature</i>
aoqi@0 124 * of another.
aoqi@0 125 *
aoqi@0 126 * @param m1 the first method
aoqi@0 127 * @param m2 the second method
aoqi@0 128 * @return {@code true} if and only if the first signature is a
aoqi@0 129 * subsignature of the second
aoqi@0 130 * @jls 8.4.2 Method Signature
aoqi@0 131 */
aoqi@0 132 boolean isSubsignature(ExecutableType m1, ExecutableType m2);
aoqi@0 133
aoqi@0 134 /**
aoqi@0 135 * Returns the direct supertypes of a type. The interface types, if any,
aoqi@0 136 * will appear last in the list.
aoqi@0 137 *
aoqi@0 138 * @param t the type being examined
aoqi@0 139 * @return the direct supertypes, or an empty list if none
aoqi@0 140 * @throws IllegalArgumentException if given an executable or package type
aoqi@0 141 */
aoqi@0 142 List<? extends TypeMirror> directSupertypes(TypeMirror t);
aoqi@0 143
aoqi@0 144 /**
aoqi@0 145 * Returns the erasure of a type.
aoqi@0 146 *
aoqi@0 147 * @param t the type to be erased
aoqi@0 148 * @return the erasure of the given type
aoqi@0 149 * @throws IllegalArgumentException if given a package type
aoqi@0 150 * @jls 4.6 Type Erasure
aoqi@0 151 */
aoqi@0 152 TypeMirror erasure(TypeMirror t);
aoqi@0 153
aoqi@0 154 /**
aoqi@0 155 * Returns the class of a boxed value of a given primitive type.
aoqi@0 156 * That is, <i>boxing conversion</i> is applied.
aoqi@0 157 *
aoqi@0 158 * @param p the primitive type to be converted
aoqi@0 159 * @return the class of a boxed value of type {@code p}
aoqi@0 160 * @jls 5.1.7 Boxing Conversion
aoqi@0 161 */
aoqi@0 162 TypeElement boxedClass(PrimitiveType p);
aoqi@0 163
aoqi@0 164 /**
aoqi@0 165 * Returns the type (a primitive type) of unboxed values of a given type.
aoqi@0 166 * That is, <i>unboxing conversion</i> is applied.
aoqi@0 167 *
aoqi@0 168 * @param t the type to be unboxed
aoqi@0 169 * @return the type of an unboxed value of type {@code t}
aoqi@0 170 * @throws IllegalArgumentException if the given type has no
aoqi@0 171 * unboxing conversion
aoqi@0 172 * @jls 5.1.8 Unboxing Conversion
aoqi@0 173 */
aoqi@0 174 PrimitiveType unboxedType(TypeMirror t);
aoqi@0 175
aoqi@0 176 /**
aoqi@0 177 * Applies capture conversion to a type.
aoqi@0 178 *
aoqi@0 179 * @param t the type to be converted
aoqi@0 180 * @return the result of applying capture conversion
aoqi@0 181 * @throws IllegalArgumentException if given an executable or package type
aoqi@0 182 * @jls 5.1.10 Capture Conversion
aoqi@0 183 */
aoqi@0 184 TypeMirror capture(TypeMirror t);
aoqi@0 185
aoqi@0 186 /**
aoqi@0 187 * Returns a primitive type.
aoqi@0 188 *
aoqi@0 189 * @param kind the kind of primitive type to return
aoqi@0 190 * @return a primitive type
aoqi@0 191 * @throws IllegalArgumentException if {@code kind} is not a primitive kind
aoqi@0 192 */
aoqi@0 193 PrimitiveType getPrimitiveType(TypeKind kind);
aoqi@0 194
aoqi@0 195 /**
aoqi@0 196 * Returns the null type. This is the type of {@code null}.
aoqi@0 197 *
aoqi@0 198 * @return the null type
aoqi@0 199 */
aoqi@0 200 NullType getNullType();
aoqi@0 201
aoqi@0 202 /**
aoqi@0 203 * Returns a pseudo-type used where no actual type is appropriate.
aoqi@0 204 * The kind of type to return may be either
aoqi@0 205 * {@link TypeKind#VOID VOID} or {@link TypeKind#NONE NONE}.
aoqi@0 206 * For packages, use
aoqi@0 207 * {@link Elements#getPackageElement(CharSequence)}{@code .asType()}
aoqi@0 208 * instead.
aoqi@0 209 *
aoqi@0 210 * @param kind the kind of type to return
aoqi@0 211 * @return a pseudo-type of kind {@code VOID} or {@code NONE}
aoqi@0 212 * @throws IllegalArgumentException if {@code kind} is not valid
aoqi@0 213 */
aoqi@0 214 NoType getNoType(TypeKind kind);
aoqi@0 215
aoqi@0 216 /**
aoqi@0 217 * Returns an array type with the specified component type.
aoqi@0 218 *
aoqi@0 219 * @param componentType the component type
aoqi@0 220 * @return an array type with the specified component type.
aoqi@0 221 * @throws IllegalArgumentException if the component type is not valid for
aoqi@0 222 * an array
aoqi@0 223 */
aoqi@0 224 ArrayType getArrayType(TypeMirror componentType);
aoqi@0 225
aoqi@0 226 /**
aoqi@0 227 * Returns a new wildcard type argument. Either of the wildcard's
aoqi@0 228 * bounds may be specified, or neither, but not both.
aoqi@0 229 *
aoqi@0 230 * @param extendsBound the extends (upper) bound, or {@code null} if none
aoqi@0 231 * @param superBound the super (lower) bound, or {@code null} if none
aoqi@0 232 * @return a new wildcard
aoqi@0 233 * @throws IllegalArgumentException if bounds are not valid
aoqi@0 234 */
aoqi@0 235 WildcardType getWildcardType(TypeMirror extendsBound,
aoqi@0 236 TypeMirror superBound);
aoqi@0 237
aoqi@0 238 /**
aoqi@0 239 * Returns the type corresponding to a type element and
aoqi@0 240 * actual type arguments.
aoqi@0 241 * Given the type element for {@code Set} and the type mirror
aoqi@0 242 * for {@code String},
aoqi@0 243 * for example, this method may be used to get the
aoqi@0 244 * parameterized type {@code Set<String>}.
aoqi@0 245 *
aoqi@0 246 * <p> The number of type arguments must either equal the
aoqi@0 247 * number of the type element's formal type parameters, or must be
aoqi@0 248 * zero. If zero, and if the type element is generic,
aoqi@0 249 * then the type element's raw type is returned.
aoqi@0 250 *
aoqi@0 251 * <p> If a parameterized type is being returned, its type element
aoqi@0 252 * must not be contained within a generic outer class.
aoqi@0 253 * The parameterized type {@code Outer<String>.Inner<Number>},
aoqi@0 254 * for example, may be constructed by first using this
aoqi@0 255 * method to get the type {@code Outer<String>}, and then invoking
aoqi@0 256 * {@link #getDeclaredType(DeclaredType, TypeElement, TypeMirror...)}.
aoqi@0 257 *
aoqi@0 258 * @param typeElem the type element
aoqi@0 259 * @param typeArgs the actual type arguments
aoqi@0 260 * @return the type corresponding to the type element and
aoqi@0 261 * actual type arguments
aoqi@0 262 * @throws IllegalArgumentException if too many or too few
aoqi@0 263 * type arguments are given, or if an inappropriate type
aoqi@0 264 * argument or type element is provided
aoqi@0 265 */
aoqi@0 266 DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs);
aoqi@0 267
aoqi@0 268 /**
aoqi@0 269 * Returns the type corresponding to a type element
aoqi@0 270 * and actual type arguments, given a
aoqi@0 271 * {@linkplain DeclaredType#getEnclosingType() containing type}
aoqi@0 272 * of which it is a member.
aoqi@0 273 * The parameterized type {@code Outer<String>.Inner<Number>},
aoqi@0 274 * for example, may be constructed by first using
aoqi@0 275 * {@link #getDeclaredType(TypeElement, TypeMirror...)}
aoqi@0 276 * to get the type {@code Outer<String>}, and then invoking
aoqi@0 277 * this method.
aoqi@0 278 *
aoqi@0 279 * <p> If the containing type is a parameterized type,
aoqi@0 280 * the number of type arguments must equal the
aoqi@0 281 * number of {@code typeElem}'s formal type parameters.
aoqi@0 282 * If it is not parameterized or if it is {@code null}, this method is
aoqi@0 283 * equivalent to {@code getDeclaredType(typeElem, typeArgs)}.
aoqi@0 284 *
aoqi@0 285 * @param containing the containing type, or {@code null} if none
aoqi@0 286 * @param typeElem the type element
aoqi@0 287 * @param typeArgs the actual type arguments
aoqi@0 288 * @return the type corresponding to the type element and
aoqi@0 289 * actual type arguments, contained within the given type
aoqi@0 290 * @throws IllegalArgumentException if too many or too few
aoqi@0 291 * type arguments are given, or if an inappropriate type
aoqi@0 292 * argument, type element, or containing type is provided
aoqi@0 293 */
aoqi@0 294 DeclaredType getDeclaredType(DeclaredType containing,
aoqi@0 295 TypeElement typeElem, TypeMirror... typeArgs);
aoqi@0 296
aoqi@0 297 /**
aoqi@0 298 * Returns the type of an element when that element is viewed as
aoqi@0 299 * a member of, or otherwise directly contained by, a given type.
aoqi@0 300 * For example,
aoqi@0 301 * when viewed as a member of the parameterized type {@code Set<String>},
aoqi@0 302 * the {@code Set.add} method is an {@code ExecutableType}
aoqi@0 303 * whose parameter is of type {@code String}.
aoqi@0 304 *
aoqi@0 305 * @param containing the containing type
aoqi@0 306 * @param element the element
aoqi@0 307 * @return the type of the element as viewed from the containing type
aoqi@0 308 * @throws IllegalArgumentException if the element is not a valid one
aoqi@0 309 * for the given type
aoqi@0 310 */
aoqi@0 311 TypeMirror asMemberOf(DeclaredType containing, Element element);
aoqi@0 312 }

mercurial