1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
325 public Class ref(Class c) {
326 return c;
327 }
328
329 public Class use(Class c) {
330 return c;
331 }
332
333 public Class asDecl(Type t) {
334 return erasure(t);
335 }
336
337 public Class asDecl(Class c) {
338 return c;
339 }
340 /**
341 * Implements the logic for {@link #erasure(Type)}.
342 */
343 private static final TypeVisitor<Class, Void> eraser = new TypeVisitor<Class, Void>() {
344
345 public Class onClass(Class c, Void _) {
346 return c;
347 }
348
349 public Class onParameterizdType(ParameterizedType p, Void _) {
350 // TODO: why getRawType returns Type? not Class?
351 return visit(p.getRawType(), null);
352 }
353
354 public Class onGenericArray(GenericArrayType g, Void _) {
355 return Array.newInstance(
356 visit(g.getGenericComponentType(), null),
357 0).getClass();
358 }
359
360 public Class onVariable(TypeVariable v, Void _) {
361 return visit(v.getBounds()[0], null);
362 }
363
364 public Class onWildcard(WildcardType w, Void _) {
365 return visit(w.getUpperBounds()[0], null);
366 }
367 };
368
369 /**
370 * Returns the runtime representation of the given type.
371 *
372 * This corresponds to the notion of the erasure in JSR-14.
373 *
374 * <p>
375 * Because of the difference in the way Annotation Processing and the Java reflection
376 * treats primitive type and array type, we can't define this method
377 * on {@link Navigator}.
378 *
379 * <p>
380 * It made me realize how difficult it is to define the common navigation
381 * layer for two different underlying reflection library. The other way
382 * is to throw away the entire parameterization and go to the wrapper approach.
383 */
384 public <T> Class<T> erasure(Type t) {
|
1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
325 public Class ref(Class c) {
326 return c;
327 }
328
329 public Class use(Class c) {
330 return c;
331 }
332
333 public Class asDecl(Type t) {
334 return erasure(t);
335 }
336
337 public Class asDecl(Class c) {
338 return c;
339 }
340 /**
341 * Implements the logic for {@link #erasure(Type)}.
342 */
343 private static final TypeVisitor<Class, Void> eraser = new TypeVisitor<Class, Void>() {
344
345 public Class onClass(Class c, Void v) {
346 return c;
347 }
348
349 public Class onParameterizdType(ParameterizedType p, Void v) {
350 // TODO: why getRawType returns Type? not Class?
351 return visit(p.getRawType(), null);
352 }
353
354 public Class onGenericArray(GenericArrayType g, Void v) {
355 return Array.newInstance(
356 visit(g.getGenericComponentType(), null),
357 0).getClass();
358 }
359
360 public Class onVariable(TypeVariable tv, Void v) {
361 return visit(tv.getBounds()[0], null);
362 }
363
364 public Class onWildcard(WildcardType w, Void v) {
365 return visit(w.getUpperBounds()[0], null);
366 }
367 };
368
369 /**
370 * Returns the runtime representation of the given type.
371 *
372 * This corresponds to the notion of the erasure in JSR-14.
373 *
374 * <p>
375 * Because of the difference in the way Annotation Processing and the Java reflection
376 * treats primitive type and array type, we can't define this method
377 * on {@link Navigator}.
378 *
379 * <p>
380 * It made me realize how difficult it is to define the common navigation
381 * layer for two different underlying reflection library. The other way
382 * is to throw away the entire parameterization and go to the wrapper approach.
383 */
384 public <T> Class<T> erasure(Type t) {
|