-
Notifications
You must be signed in to change notification settings - Fork 78
/
Type.java
207 lines (186 loc) · 6.87 KB
/
Type.java
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
package jakarta.enterprise.lang.model.types;
import jakarta.enterprise.lang.model.AnnotationTarget;
import jakarta.enterprise.lang.model.declarations.ClassInfo;
import jakarta.enterprise.lang.model.declarations.DeclarationInfo;
/**
* A type is an abstract entity that may be <i>used</i> in a program, but doesn't necessarily have to
* correspond to a <i>declaration</i> in a program. Types that are declared in a program source code have a method
* to obtain the corresponding {@linkplain DeclarationInfo declaration}.
* Types are:
*
* <ul>
* <li>the {@linkplain VoidType void} pseudo-type</li>
* <li>a {@linkplain PrimitiveType primitive} type, such as {@code int}</li>
* <li>a {@linkplain ClassType class} type, such as {@code String}</li>
* <li>an {@linkplain ArrayType array} type, such as {@code int[]} or {@code String[][]}</li>
* <li>a {@linkplain ParameterizedType parameterized} type, such as {@code List<String>}</li>
* <li>a {@linkplain TypeVariable type variable}, such as {@code T} when used in a class that declares a type parameter {@code T}</li>
* <li>a {@linkplain WildcardType wildcard} type, such as the type argument in {@code List<? extends Number>}</li>
* </ul>
*/
public interface Type extends AnnotationTarget {
@Override
default boolean isDeclaration() {
return false;
}
@Override
default boolean isType() {
return true;
}
@Override
default DeclarationInfo asDeclaration() {
throw new IllegalStateException("Not a declaration");
}
@Override
default Type asType() {
return this;
}
enum Kind {
/** E.g. when method returns {@code void}. */
VOID,
/** E.g. when method returns {@code int}. */
PRIMITIVE,
/** E.g. when method returns {@code String}. */
CLASS,
/** E.g. when method returns {@code int[]} or {@code String[][]}. */
ARRAY,
/** E.g. when method returns {@code List<String>}. */
PARAMETERIZED_TYPE,
/** E.g. when method returns {@code T} and {@code T} is a type parameter of the declaring class. */
TYPE_VARIABLE,
/**
* E.g. when method returns {@code List<? extends Number>}. On the first level, we have a {@code PARAMETERIZED_TYPE},
* but on the second level, the first (and only) type argument is a {@code WILDCARD_TYPE}.
*/
WILDCARD_TYPE,
}
/**
* Returns the {@linkplain Kind kind} of this type.
*
* @return the kind of this type
*/
Kind kind();
/**
* Returns whether this type is the {@linkplain VoidType void} pseudo-type.
*
* @return {@code true} if this is void, {@code false} otherwise
*/
default boolean isVoid() {
return kind() == Kind.VOID;
}
/**
* Returns whether this type is a {@linkplain PrimitiveType primitive} type.
*
* @return {@code true} if this is a primitive type, {@code false} otherwise
*/
default boolean isPrimitive() {
return kind() == Kind.PRIMITIVE;
}
/**
* Returns whether this type is a {@linkplain ClassType class} type.
*
* @return {@code true} if this is a class type, {@code false} otherwise
*/
default boolean isClass() {
return kind() == Kind.CLASS;
}
/**
* Returns whether this type is an {@linkplain ArrayType array} type.
*
* @return {@code true} if this is an array type, {@code false} otherwise
*/
default boolean isArray() {
return kind() == Kind.ARRAY;
}
/**
* Returns whether this type is a {@linkplain ParameterizedType parameterized} type.
*
* @return {@code true} if this is a parameterized type, {@code false} otherwise
*/
default boolean isParameterizedType() {
return kind() == Kind.PARAMETERIZED_TYPE;
}
/**
* Returns whether this type is a {@linkplain TypeVariable type variable}.
* Type variables are also used to represent type parameters in declarations
* of parameterized types.
*
* @return {@code true} if this is a primitive type, {@code false} otherwise
*/
default boolean isTypeVariable() {
return kind() == Kind.TYPE_VARIABLE;
}
/**
* Returns whether this type is a {@linkplain WildcardType wildcard} type.
*
* @return {@code true} if this is a wildcard type, {@code false} otherwise
*/
default boolean isWildcardType() {
return kind() == Kind.WILDCARD_TYPE;
}
/**
* Returns this type as the {@linkplain VoidType void} pseudo-type.
*
* @return this void type, never {@code null}
* @throws IllegalStateException if {@link #isVoid()} returns {@code false}
*/
default VoidType asVoid() {
throw new IllegalStateException("Not a void");
}
/**
* Returns this type as a {@linkplain PrimitiveType primitive} type.
*
* @return this primitive type, never {@code null}
* @throws IllegalStateException if {@link #isPrimitive()} returns {@code false}
*/
default PrimitiveType asPrimitive() {
throw new IllegalStateException("Not a primitive");
}
/**
* Returns this type as a {@linkplain ClassType class} type.
*
* @return this class type, never {@code null}
* @throws IllegalStateException if {@link #isClass()} returns {@code false}
*/
default ClassType asClass() {
throw new IllegalStateException("Not a class");
}
/**
* Returns this type as an {@linkplain ArrayType array} type.
*
* @return this array type, never {@code null}
* @throws IllegalStateException if {@link #isArray()} returns {@code false}
*/
default ArrayType asArray() {
throw new IllegalStateException("Not an array");
}
/**
* Returns this type as a {@linkplain ParameterizedType parameterized} type.
*
* @return this parameterized type, never {@code null}
* @throws IllegalStateException if {@link #isParameterizedType()} returns {@code false}
*/
default ParameterizedType asParameterizedType() {
throw new IllegalStateException("Not a parameterized type");
}
/**
* Returns this type as a {@linkplain TypeVariable type variable}.
* Type variables are also used to represent type parameters in declarations
* of parameterized types.
*
* @return this type variable, never {@code null}
* @throws IllegalStateException if {@link #isTypeVariable()} returns {@code false}
*/
default TypeVariable asTypeVariable() {
throw new IllegalStateException("Not a type variable");
}
/**
* Returns this type as a {@linkplain WildcardType wildcard} type.
*
* @return this wildcard type, never {@code null}
* @throws IllegalStateException if {@link #isWildcardType()} returns {@code false}
*/
default WildcardType asWildcardType() {
throw new IllegalStateException("Not a wildcard type");
}
}