summaryrefslogtreecommitdiff
path: root/javax/lang/model/util/Types.java
blob: b05008c77365e25cd77ddf62e756f4059373be0e (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
/* 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.DeclaredType;
import javax.lang.model.type.TypeMirror;

/**
 * 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);

}