author | darcy |
Thu, 17 Jul 2014 09:47:25 -0700 | |
changeset 25690 | b1dac768ab79 |
parent 15386 | 92bc08d96f0c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
15386
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
2 |
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package javax.lang.model.util; |
|
27 |
||
14258 | 28 |
import javax.annotation.processing.SupportedSourceVersion; |
29 |
import javax.lang.model.SourceVersion; |
|
10 | 30 |
import javax.lang.model.type.*; |
31 |
import static javax.lang.model.SourceVersion.*; |
|
32 |
||
33 |
/** |
|
34 |
* A visitor of types based on their {@linkplain TypeKind kind} with |
|
35 |
* default behavior appropriate for the {@link SourceVersion#RELEASE_6 |
|
36 |
* RELEASE_6} source version. For {@linkplain |
|
37 |
* TypeMirror types} <tt><i>XYZ</i></tt> that may have more than one |
|
38 |
* kind, the <tt>visit<i>XYZ</i></tt> methods in this class delegate |
|
39 |
* to the <tt>visit<i>XYZKind</i></tt> method corresponding to the |
|
40 |
* first argument's kind. The <tt>visit<i>XYZKind</i></tt> methods |
|
41 |
* call {@link #defaultAction defaultAction}, passing their arguments |
|
42 |
* to {@code defaultAction}'s corresponding parameters. |
|
43 |
* |
|
44 |
* <p> Methods in this class may be overridden subject to their |
|
45 |
* general contract. Note that annotating methods in concrete |
|
46 |
* subclasses with {@link java.lang.Override @Override} will help |
|
47 |
* ensure that methods are overridden as intended. |
|
48 |
* |
|
49 |
* <p> <b>WARNING:</b> The {@code TypeVisitor} interface implemented |
|
50 |
* by this class may have methods added to it in the future to |
|
51 |
* accommodate new, currently unknown, language structures added to |
|
52 |
* future versions of the Java™ programming language. |
|
53 |
* Therefore, methods whose names begin with {@code "visit"} may be |
|
54 |
* added to this class in the future; to avoid incompatibilities, |
|
55 |
* classes which extend this class should not declare any instance |
|
56 |
* methods with names beginning with {@code "visit"}. |
|
57 |
* |
|
58 |
* <p>When such a new visit method is added, the default |
|
59 |
* implementation in this class will be to call the {@link |
|
60 |
* #visitUnknown visitUnknown} method. A new type kind visitor class |
|
61 |
* will also be introduced to correspond to the new language level; |
|
62 |
* this visitor will have different default behavior for the visit |
|
63 |
* method in question. When the new visitor is introduced, all or |
|
64 |
* portions of this visitor may be deprecated. |
|
65 |
* |
|
15386
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
66 |
* <p>Note that adding a default implementation of a new visit method |
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
67 |
* in a visitor class will occur instead of adding a <em>default |
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
68 |
* method</em> directly in the visitor interface since a Java SE 8 |
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
69 |
* language feature cannot be used to this version of the API since |
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
70 |
* this version is required to be runnable on Java SE 7 |
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
71 |
* implementations. Future versions of the API that are only required |
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
72 |
* to run on Java SE 8 and later may take advantage of default methods |
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
73 |
* in this situation. |
92bc08d96f0c
8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents:
14258
diff
changeset
|
74 |
* |
10 | 75 |
* @param <R> the return type of this visitor's methods. Use {@link |
76 |
* Void} for visitors that do not need to return results. |
|
77 |
* @param <P> the type of the additional parameter to this visitor's |
|
78 |
* methods. Use {@code Void} for visitors that do not need an |
|
79 |
* additional parameter. |
|
80 |
* |
|
81 |
* @author Joseph D. Darcy |
|
82 |
* @author Scott Seligman |
|
83 |
* @author Peter von der Ahé |
|
5841
7a8448425bb7
6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents:
5520
diff
changeset
|
84 |
* |
7a8448425bb7
6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents:
5520
diff
changeset
|
85 |
* @see TypeKindVisitor7 |
10192
378321489bea
7025809: Provided new utility visitors supporting SourceVersion.RELEASE_8
darcy
parents:
7681
diff
changeset
|
86 |
* @see TypeKindVisitor8 |
10 | 87 |
* @since 1.6 |
25690
b1dac768ab79
8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents:
15386
diff
changeset
|
88 |
* @deprecated Release 6 is obsolete; update to a visitor for a newer |
b1dac768ab79
8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents:
15386
diff
changeset
|
89 |
* release level. |
10 | 90 |
*/ |
25690
b1dac768ab79
8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents:
15386
diff
changeset
|
91 |
@Deprecated |
10 | 92 |
@SupportedSourceVersion(RELEASE_6) |
93 |
public class TypeKindVisitor6<R, P> extends SimpleTypeVisitor6<R, P> { |
|
94 |
/** |
|
95 |
* Constructor for concrete subclasses to call; uses {@code null} |
|
96 |
* for the default value. |
|
97 |
*/ |
|
98 |
protected TypeKindVisitor6() { |
|
99 |
super(null); |
|
100 |
} |
|
101 |
||
102 |
||
103 |
/** |
|
104 |
* Constructor for concrete subclasses to call; uses the argument |
|
105 |
* for the default value. |
|
106 |
* |
|
107 |
* @param defaultValue the value to assign to {@link #DEFAULT_VALUE} |
|
108 |
*/ |
|
109 |
protected TypeKindVisitor6(R defaultValue) { |
|
110 |
super(defaultValue); |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Visits a primitive type, dispatching to the visit method for |
|
115 |
* the specific {@linkplain TypeKind kind} of primitive type: |
|
116 |
* {@code BOOLEAN}, {@code BYTE}, etc. |
|
117 |
* |
|
118 |
* @param t {@inheritDoc} |
|
119 |
* @param p {@inheritDoc} |
|
120 |
* @return the result of the kind-specific visit method |
|
121 |
*/ |
|
122 |
@Override |
|
123 |
public R visitPrimitive(PrimitiveType t, P p) { |
|
124 |
TypeKind k = t.getKind(); |
|
125 |
switch (k) { |
|
126 |
case BOOLEAN: |
|
127 |
return visitPrimitiveAsBoolean(t, p); |
|
128 |
||
129 |
case BYTE: |
|
130 |
return visitPrimitiveAsByte(t, p); |
|
131 |
||
132 |
case SHORT: |
|
133 |
return visitPrimitiveAsShort(t, p); |
|
134 |
||
135 |
case INT: |
|
136 |
return visitPrimitiveAsInt(t, p); |
|
137 |
||
138 |
case LONG: |
|
139 |
return visitPrimitiveAsLong(t, p); |
|
140 |
||
141 |
case CHAR: |
|
142 |
return visitPrimitiveAsChar(t, p); |
|
143 |
||
144 |
case FLOAT: |
|
145 |
return visitPrimitiveAsFloat(t, p); |
|
146 |
||
147 |
case DOUBLE: |
|
148 |
return visitPrimitiveAsDouble(t, p); |
|
149 |
||
150 |
default: |
|
151 |
throw new AssertionError("Bad kind " + k + " for PrimitiveType" + t); |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Visits a {@code BOOLEAN} primitive type by calling |
|
157 |
* {@code defaultAction}. |
|
158 |
* |
|
159 |
* @param t the type to visit |
|
160 |
* @param p a visitor-specified parameter |
|
161 |
* @return the result of {@code defaultAction} |
|
162 |
*/ |
|
163 |
public R visitPrimitiveAsBoolean(PrimitiveType t, P p) { |
|
164 |
return defaultAction(t, p); |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* Visits a {@code BYTE} primitive type by calling |
|
169 |
* {@code defaultAction}. |
|
170 |
* |
|
171 |
* @param t the type to visit |
|
172 |
* @param p a visitor-specified parameter |
|
173 |
* @return the result of {@code defaultAction} |
|
174 |
*/ |
|
175 |
public R visitPrimitiveAsByte(PrimitiveType t, P p) { |
|
176 |
return defaultAction(t, p); |
|
177 |
} |
|
178 |
||
179 |
/** |
|
180 |
* Visits a {@code SHORT} primitive type by calling |
|
181 |
* {@code defaultAction}. |
|
182 |
* |
|
183 |
* @param t the type to visit |
|
184 |
* @param p a visitor-specified parameter |
|
185 |
* @return the result of {@code defaultAction} |
|
186 |
*/ |
|
187 |
public R visitPrimitiveAsShort(PrimitiveType t, P p) { |
|
188 |
return defaultAction(t, p); |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Visits an {@code INT} primitive type by calling |
|
193 |
* {@code defaultAction}. |
|
194 |
* |
|
195 |
* @param t the type to visit |
|
196 |
* @param p a visitor-specified parameter |
|
197 |
* @return the result of {@code defaultAction} |
|
198 |
*/ |
|
199 |
public R visitPrimitiveAsInt(PrimitiveType t, P p) { |
|
200 |
return defaultAction(t, p); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Visits a {@code LONG} primitive type by calling |
|
205 |
* {@code defaultAction}. |
|
206 |
* |
|
207 |
* @param t the type to visit |
|
208 |
* @param p a visitor-specified parameter |
|
209 |
* @return the result of {@code defaultAction} |
|
210 |
*/ |
|
211 |
public R visitPrimitiveAsLong(PrimitiveType t, P p) { |
|
212 |
return defaultAction(t, p); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Visits a {@code CHAR} primitive type by calling |
|
217 |
* {@code defaultAction}. |
|
218 |
* |
|
219 |
* @param t the type to visit |
|
220 |
* @param p a visitor-specified parameter |
|
221 |
* @return the result of {@code defaultAction} |
|
222 |
*/ |
|
223 |
public R visitPrimitiveAsChar(PrimitiveType t, P p) { |
|
224 |
return defaultAction(t, p); |
|
225 |
} |
|
226 |
||
227 |
/** |
|
228 |
* Visits a {@code FLOAT} primitive type by calling |
|
229 |
* {@code defaultAction}. |
|
230 |
* |
|
231 |
* @param t the type to visit |
|
232 |
* @param p a visitor-specified parameter |
|
233 |
* @return the result of {@code defaultAction} |
|
234 |
*/ |
|
235 |
public R visitPrimitiveAsFloat(PrimitiveType t, P p) { |
|
236 |
return defaultAction(t, p); |
|
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* Visits a {@code DOUBLE} primitive type by calling |
|
241 |
* {@code defaultAction}. |
|
242 |
* |
|
243 |
* @param t the type to visit |
|
244 |
* @param p a visitor-specified parameter |
|
245 |
* @return the result of {@code defaultAction} |
|
246 |
*/ |
|
247 |
public R visitPrimitiveAsDouble(PrimitiveType t, P p) { |
|
248 |
return defaultAction(t, p); |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Visits a {@link NoType} instance, dispatching to the visit method for |
|
253 |
* the specific {@linkplain TypeKind kind} of pseudo-type: |
|
254 |
* {@code VOID}, {@code PACKAGE}, or {@code NONE}. |
|
255 |
* |
|
256 |
* @param t {@inheritDoc} |
|
257 |
* @param p {@inheritDoc} |
|
258 |
* @return the result of the kind-specific visit method |
|
259 |
*/ |
|
260 |
@Override |
|
261 |
public R visitNoType(NoType t, P p) { |
|
262 |
TypeKind k = t.getKind(); |
|
263 |
switch (k) { |
|
264 |
case VOID: |
|
265 |
return visitNoTypeAsVoid(t, p); |
|
266 |
||
267 |
case PACKAGE: |
|
268 |
return visitNoTypeAsPackage(t, p); |
|
269 |
||
270 |
case NONE: |
|
271 |
return visitNoTypeAsNone(t, p); |
|
272 |
||
273 |
default: |
|
274 |
throw new AssertionError("Bad kind " + k + " for NoType" + t); |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
/** |
|
279 |
* Visits a {@link TypeKind#VOID VOID} pseudo-type by calling |
|
280 |
* {@code defaultAction}. |
|
281 |
* |
|
282 |
* @param t the type to visit |
|
283 |
* @param p a visitor-specified parameter |
|
284 |
* @return the result of {@code defaultAction} |
|
285 |
*/ |
|
286 |
public R visitNoTypeAsVoid(NoType t, P p) { |
|
287 |
return defaultAction(t, p); |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Visits a {@link TypeKind#PACKAGE PACKAGE} pseudo-type by calling |
|
292 |
* {@code defaultAction}. |
|
293 |
* |
|
294 |
* @param t the type to visit |
|
295 |
* @param p a visitor-specified parameter |
|
296 |
* @return the result of {@code defaultAction} |
|
297 |
*/ |
|
298 |
public R visitNoTypeAsPackage(NoType t, P p) { |
|
299 |
return defaultAction(t, p); |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Visits a {@link TypeKind#NONE NONE} pseudo-type by calling |
|
304 |
* {@code defaultAction}. |
|
305 |
* |
|
306 |
* @param t the type to visit |
|
307 |
* @param p a visitor-specified parameter |
|
308 |
* @return the result of {@code defaultAction} |
|
309 |
*/ |
|
310 |
public R visitNoTypeAsNone(NoType t, P p) { |
|
311 |
return defaultAction(t, p); |
|
312 |
} |
|
313 |
} |