summaryrefslogtreecommitdiff
path: root/javax/lang/model/util/Types.java
blob: 86647d1b6dbb4923e00dd70b9dfd66972841aaed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* Types.java -- Utility methods for operating on types.
   Copyright (C) 2012  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package javax.lang.model.util;

import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.NoType;
import javax.lang.model.type.NullType;
import javax.lang.model.type.PrimitiveType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.WildcardType;

/**
 * Utility methods for operating on types.
 *
 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
 * @since 1.6
 */
public interface Types
{

  /**
   * Returns the element corresponding to this type
   * or {@code null} if no such element exists.
   * The type may be a {@link javax.lang.model.type.DeclaredType}
   * or a {@link javax.lang.model.type.TypeVariable}.
   * 
   * @param type the type to return the corresponding element to.
   * @return the element corresponding to this type.
   */
  Element asElement(TypeMirror type);

  /**
   * Returns a view of an element when seen as a member of,
   * or otherwise contained by, the specified type.  For
   * example, the method {@code Set.add(E)} becomes
   * {@code Set.add(String)} when viewed as a member of
   * the parameterised type {@code Set<String>}.
   *
   * @param container the type containing the element.
   * @param element the element being contained.
   * @return the type of the element viewed from the container.
   * @throws IllegalArgumentException if the element is not applicable
   *                                  for the specified container.
   */
  TypeMirror asMemberOf(DeclaredType container, Element element);

  /**
   * Applies capture conversion to the type, as specified
   * in section 5.1.10 of the Java Language Specification.
   *
   * @param type the type to capture convert.
   * @return the result of applying capture conversion.
   * @throws IllegalArgumentException if given an executable or package type.
   */
  TypeMirror capture(TypeMirror type);

  /**
   * Returns {@code true} if {@code type1} contains {@code type2}.
   *
   * @param type1 the first type.
   * @param type2 the second type.
   * @return true if type1 contains type2.
   * @throws IllegalArgumentException if given an executable or package type.
   */
  boolean contains(TypeMirror type1, TypeMirror type2);

  /**
   * Returns the direct supertypes of the specified type.  The superclass
   * appears first, if any, followed by the interfaces, if any.
   *
   * @param type the type to examine.
   * @return the direct supertypes or an empty list if there are none.
   * @throws IllegalArgumentException if given an executable or package type.
   */
  List<? extends TypeMirror> directSupertypes(TypeMirror type);

  /**
   * Returns the erasure of the type, as specified by section
   * 4.6 of the Java Language Specification.
   *
   * @param type the type to erase.
   * @return the type erasure.
   * @throws IllegalArgumentException if given a package type.
   */
  TypeMirror erasure(TypeMirror type);

  /**
   * <p>Returns the type corresponding to a type element and actual
   * type arguments, given the specified containing type of which
   * it is a member.  This method is needed to retrieve types
   * contained by parameterised types, for example
   * {@code Outer<String>.Inner<Integer>}.  To obtain this type,
   * this method should be supplied with the {@link DeclaredType}
   * for {@code Outer<String>} (probably obtained by a call
   * to {@link #getDeclaredType(TypeElement, TypeMirror...)}),
   * the element representing {@code Inner} and the
   * {@link TypeMirror} for {@code String}</p>.
   * <p>If the container is parameterised, the number of type
   * arguments must equal the number of formal type parameters
   * used in the specified element.  Calls where the container
   * is not parameterised, or is {@code null}, are equivalent
   * to calls to {@link #getDeclaredType(TypeElement, TypeMirror...)}.
   *
   * @param container the type containing the returned type,
   *                  or {@code null} if there is none.
   * @param element the type element to return the realised type for.
   * @param args the actual type arguments to match to the element's
   *             formal parameters.
   * @return the type corresponding to the specified element using
   *         the specified type arguments.
   * @throws IllegalArgumentException if the number of type arguments
   *                                  doesn't match the number of type
   *                                  parameters, or an inappropriate
   *                                  container, type element or argument
   *                                  is given.
   */
  DeclaredType getDeclaredType(DeclaredType container, TypeElement element,
			       TypeMirror... args);

  /**
   * Returns the type corresponding to a type element and actual
   * type arguments.  So, for the type element for {@code Set<T>},
   * and the type mirror for {@code String}, this method would
   * return the declared type for {@code Set<String>}.  The number
   * of type arguments must either match the number of formal
   * type parameters in the element, or be zero in order to obtain
   * the raw type ({@code Set} in the above).  The type element
   * must not be contained within a generic outer class; for that,
   * use {@link  #getDeclaredType(DeclaredType,TypeElement,TypeMirror...)}.
   *
   * @param element the type element to return the realised type for.
   * @param args the actual type arguments to match to the element's
   *             formal parameters.
   * @return the type corresponding to the specified element using
   *         the specified type arguments.
   * @throws IllegalArgumentException if the number of type arguments
   *                                  doesn't match the number of type
   *                                  parameters, or an inappropriate
   *                                  type element or argument is given.
   */
  DeclaredType getDeclaredType(TypeElement element, TypeMirror... args);

  /**
   * Returns {@code true} if {@code type1} may be assigned to {@code type2},
   * according to section 5.2 of the Java Language Specification.
   *
   * @param type1 the first type.
   * @param type2 the second type.
   * @return true if type1 can be assigned to type2.
   * @throws IllegalArgumentException if given an executable or package type.
   */
  boolean isAssignable(TypeMirror type1, TypeMirror type2);

  /**
   * Returns {@code true} if the two types are the same.  Note that
   * wildcard types are never the same as any other type, including
   * themselves.  This rule prevents wildcard types being used as
   * method arguments.
   *
   * @param type1 the first type.
   * @param type2 the second type.
   * @return true iff the two types are the same.
   */
  boolean isSameType(TypeMirror type1, TypeMirror type2);

  /**
   * Returns {@code true} if {@code type1} is a subtype of {@code type2},
   * according to section 4.10 of the Java Language Specification.  A
   * type is always considered a subtype of itself.
   *
   * @param type1 the first type.
   * @param type2 the second type.
   * @return true if type1 is a subtype of type2.
   * @throws IllegalArgumentException if given an executable or package type.
   */
  boolean isSubtype(TypeMirror type1, TypeMirror type2);

  /**
   * <p>Returns the class which is used to wrap the given primitive
   * type, according to the following mapping given in section
   * 5.1.7 of the Java Language Specification:</p>
   * <ul>
   * <li>{@code boolean} ==&gt; {@code Boolean}</li>
   * <li>{@code byte} ==&gt; {@code Byte}</li>
   * <li>{@code char} ==&gt; {@code Character}</li>
   * <li>{@code double} ==&gt; {@code Double}</li>
   * <li>{@code float} ==&gt; {@code Float}</li>
   * <li>{@code int} ==&gt; {@code Integer}</li>
   * <li>{@code long} ==&gt; {@code Long}</li>
   * <li>{@code short} ==&gt; {@code Short}</li>
   * </ul>
   *
   * @param primitive the primitive type whose wrapper class should
   *                  be returned.
   * @return the wrapper class used for the given primitive type.
   */
  TypeElement boxedClass(PrimitiveType primitive);

  /**
   * Returns an array type with the specified component type.
   *
   * @param componentType the component type to be used in the array.
   * @return an array type using the specified component type.
   * @throws IllegalArgumentException if the component type given
   *                                  can not be used in an array.
   */
  ArrayType getArrayType(TypeMirror componentType);

  /**
   * Returns a pseudo-type of the specified kind for use where a real
   * type is not applicable.  Only the kinds {@link TypeKind#VOID}
   * and {@link TypeKind#NONE} should be passed to this method.
   * For packages, use
   * {@code Elements#getPackageElement(CharSequence).asType()}
   * instead.
   *
   * @param kind the kind of {@link NoType} to return.
   * @return the corresponding instance.
   * @throws IllegalArgumentException if an invalid kind is given.
   */
  NoType getNoType(TypeKind kind);

  /**
   * Returns the null type i.e. the type of {@code null}.
   *
   * @return the null type.
   */
  NullType getNullType();

  /**
   * Returns a primitive type of the given kind.
   *
   * @param kind the kind of primitive type to return.
   * @return the corresponding instance.
   * @throws IllegalArgumentException if the kind given is not
   *                                  a primitive type.
   */
  PrimitiveType getPrimitiveType(TypeKind kind);

  /**
   * Returns a wildcard type with the specified bounds.
   * Each bound is optional and {@code null} may be passed
   * instead.  It is invalid for both bounds to be non-null.
   *
   * @param extendsBound the upper bound, which usually follows the
   *                     {@code extends clause}, or {@code null}.
   * @param superBound the lower bound, which usually follows the
   *                   {@code super clause}, or {@code null}.
   * @return the corresponding wildcard type.
   * @throws IllegalArgumentException if the bounds are invalid.
   */
  WildcardType getWildcardType(TypeMirror extendsBound,
			       TypeMirror superBound);

  /**
   * Returns {@code true} if the signature of {@code method1} is
   * a subsignature of the signature of {@code method2}, according
   * to section 8.4.2 of the Java Language Specification.
   *
   * @param method1 the first method.
   * @param method2 the second method.
   * @return true if method1's signature is a subsignature of method2's.
   */
  boolean isSubsignature(ExecutableType method1, ExecutableType method2);

  /**
   * <p>Returns the primitive type which is used to unwrap the value
   * contained in the specified wrapper class, according to the following
   * mapping given in section 5.1.8 of the Java Language Specification:</p>
   * <ul>
   * <li>{@code Boolean} ==&gt; {@code boolean}</li>
   * <li>{@code Byte} ==&gt; {@code byte}</li>
   * <li>{@code Character} ==&gt; {@code char}</li>
   * <li>{@code Double} ==&gt; {@code double}</li>
   * <li>{@code Float} ==&gt; {@code float}</li>
   * <li>{@code Integer} ==&gt; {@code int}</li>
   * <li>{@code Long} ==&gt; {@code long}</li>
   * <li>{@code Short} ==&gt; {@code short}</li>
   * </ul>
   *
   * @param wrapper the wrapper class for which the corresponding primitive
   *                type should be returned.
   * @return the corresponding primitive type.
   * @throws IllegalArgumentException if the given class is not a wrapper class.
   */
  PrimitiveType unboxedType(TypeMirror wrapper);

}