Yohann Roussel | 804b758 | 2017-07-26 15:51:17 +0200 | [diff] [blame] | 1 | // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | package minifygeneric; |
| 5 | |
| 6 | import java.lang.reflect.Field; |
| 7 | import java.lang.reflect.ParameterizedType; |
| 8 | import java.lang.reflect.Type; |
| 9 | import java.lang.reflect.TypeVariable; |
| 10 | |
| 11 | public class Minifygeneric { |
| 12 | |
| 13 | public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException { |
| 14 | for (TypeVariable<Class<Generic>> var : Generic.class.getTypeParameters()) { |
| 15 | System.out.println(var.getName()); |
| 16 | Type bound = var.getBounds()[0]; |
| 17 | System.out.println(((Class<?>) bound).getName().equals(AA.class.getName())); |
| 18 | } |
| 19 | |
| 20 | Field f = Generic.class.getField("f"); |
| 21 | ParameterizedType fieldType = (java.lang.reflect.ParameterizedType)f.getGenericType(); |
| 22 | checkOneParameterType(fieldType, Generic.class, AA.class); |
| 23 | |
| 24 | ParameterizedType methodReturnType = |
| 25 | (ParameterizedType) Generic.class.getMethod("get").getGenericReturnType(); |
| 26 | checkOneParameterType(methodReturnType, Generic.class, AA.class); |
| 27 | } |
| 28 | |
| 29 | private static void checkOneParameterType(ParameterizedType toCheck, Class<?> rawType, |
| 30 | Class<?>... bounds) { |
| 31 | System.out.println(((Class<?>) toCheck.getRawType()).getName() |
| 32 | .equals(rawType.getName())); |
| 33 | Type[] parameters = toCheck.getActualTypeArguments(); |
| 34 | System.out.println(parameters.length); |
| 35 | TypeVariable<?> parameter = (TypeVariable<?>) parameters[0]; |
| 36 | System.out.println(parameter.getName()); |
| 37 | Type[] actualBounds = parameter.getBounds(); |
| 38 | for (int i = 0; i < bounds.length; i++) { |
| 39 | System.out.println(((Class<?>) actualBounds[i]).getName().equals(bounds[i].getName())); |
| 40 | } |
| 41 | } |
| 42 | } |