Java Reflection Java Java API
Class:
java.lang.reflect.Method
Method:
public Type[] getGenericParameterTypes()
Returns an array of
Type
objects that represent the formal parameter types, in declaration order, of the method represented by this
object.
Examples
package com.logicbig.example.method;
import java.lang.reflect.*; import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Objects;
public class GetGenericParameterTypesExample { private <N extends Number, T extends Thread> void process( N t, Map<? super BigDecimal, ? extends CharSequence> map, List<String> list, T[] ts) { }
public static void main(String... args) throws NoSuchMethodException { Method m = GetGenericParameterTypesExample.class.getDeclaredMethod("process", Number.class, Map.class, List.class, Thread[].class); Type[] types = m.getGenericParameterTypes(); System.out.println("Method#getGenericParameterTypes(): \n" + Arrays.toString(types)); printTypes(types, 1); }
private static void printTypes(Type[] types, int level) { for (Type type : types) { printType(type, level); } }
private static void printType(Type type, int level) { if (type == null) { return; } print(level, "type: " + Objects.toString(type)); if (type instanceof TypeVariable) { print(level, "type is an instance of TypeVariable"); printTypeVariable((TypeVariable) type, level + 1); } else if (type instanceof Class) { print(level, "type is an instance of Class"); print(level, "Class Name: " + ((Class) type).getName()); } else if (type instanceof ParameterizedType) { print(level, "type is an instance of ParameterizedType"); printParameterizeType((ParameterizedType) type, level + 1); } else if (type instanceof WildcardType) { print(level, "type is an instance of WildcardType"); printWildcardType((WildcardType) type, level + 1); } else if (type instanceof GenericArrayType) { print(level, "type is an instance of GenericArrayType"); printGenericArrayType((GenericArrayType) type, level + 1); } else { throw new RuntimeException("Unknown type: " + type); } }
private static void printGenericArrayType(GenericArrayType type, int level) { Type componentType = type.getGenericComponentType(); print(level, "GenericArrayType#getGenericComponentType(): " + componentType); printType(componentType, level + 1); }
private static void printWildcardType(WildcardType type, int level) { Type[] lowerBounds = type.getLowerBounds(); print(level, "WildcardType#getLowerBounds(): " + Arrays.toString(lowerBounds)); for (Type lowerBound : lowerBounds) { print(level, "WildcardType#lowerBound: " + lowerBound); printType(lowerBound, level + 1); } Type[] upperBounds = type.getUpperBounds(); print(level, "WildcardType#getUpperBounds(): " + Arrays.toString(upperBounds)); for (Type upperBound : upperBounds) { print(level, "WildcardType#upperBound: " + upperBound); printType(upperBound, level + 1); } }
private static void printParameterizeType(ParameterizedType type, int level) { Type ownerType = type.getOwnerType(); print(level, "ParameterizedType#getOwnerType(): " + ownerType); printType(ownerType, level + 1); Type rawType = type.getRawType(); print(level, "ParameterizedType#getRawType(): " + rawType); printType(rawType, level + 1); Type[] types = type.getActualTypeArguments(); print(level, "ParameterizedType#getActualTypeArguments(): " + Arrays.toString(types)); for (Type type1 : types) { print(level, "ParameterizedType#actualTypeArgument: " + type1); printType(type1, level + 1); } }
private static void printTypeVariable(TypeVariable type, int level) { print(level, "TypeVariable#getName(): " + type.getName()); Type[] bounds = type.getBounds(); print(level, "TypeVariable#getBounds(): " + Arrays.toString(bounds)); for (Type bound : bounds) { print(level, "TypeVariable#bound: " + bound); printType(bound, level + 1); } }
private static void print(int level, String string) { System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string); } }
OutputMethod#getGenericParameterTypes(): [N, java.util.Map<? super java.math.BigDecimal, ? extends java.lang.CharSequence>, java.util.List<java.lang.String>, T[]] ¦- type: N ¦- type is an instance of TypeVariable ¦- TypeVariable#getName(): N ¦- TypeVariable#getBounds(): [class java.lang.Number] ¦- TypeVariable#bound: class java.lang.Number ¦- type: class java.lang.Number ¦- type is an instance of Class ¦- Class Name: java.lang.Number ¦- type: java.util.Map<? super java.math.BigDecimal, ? extends java.lang.CharSequence> ¦- type is an instance of ParameterizedType ¦- ParameterizedType#getOwnerType(): null ¦- ParameterizedType#getRawType(): interface java.util.Map ¦- type: interface java.util.Map ¦- type is an instance of Class ¦- Class Name: java.util.Map ¦- ParameterizedType#getActualTypeArguments(): [? super java.math.BigDecimal, ? extends java.lang.CharSequence] ¦- ParameterizedType#actualTypeArgument: ? super java.math.BigDecimal ¦- type: ? super java.math.BigDecimal ¦- type is an instance of WildcardType ¦- WildcardType#getLowerBounds(): [class java.math.BigDecimal] ¦- WildcardType#lowerBound: class java.math.BigDecimal ¦- type: class java.math.BigDecimal ¦- type is an instance of Class ¦- Class Name: java.math.BigDecimal ¦- WildcardType#getUpperBounds(): [class java.lang.Object] ¦- WildcardType#upperBound: class java.lang.Object ¦- type: class java.lang.Object ¦- type is an instance of Class ¦- Class Name: java.lang.Object ¦- ParameterizedType#actualTypeArgument: ? extends java.lang.CharSequence ¦- type: ? extends java.lang.CharSequence ¦- type is an instance of WildcardType ¦- WildcardType#getLowerBounds(): [] ¦- WildcardType#getUpperBounds(): [interface java.lang.CharSequence] ¦- WildcardType#upperBound: interface java.lang.CharSequence ¦- type: interface java.lang.CharSequence ¦- type is an instance of Class ¦- Class Name: java.lang.CharSequence ¦- type: java.util.List<java.lang.String> ¦- type is an instance of ParameterizedType ¦- ParameterizedType#getOwnerType(): null ¦- ParameterizedType#getRawType(): interface java.util.List ¦- type: interface java.util.List ¦- type is an instance of Class ¦- Class Name: java.util.List ¦- ParameterizedType#getActualTypeArguments(): [class java.lang.String] ¦- ParameterizedType#actualTypeArgument: class java.lang.String ¦- type: class java.lang.String ¦- type is an instance of Class ¦- Class Name: java.lang.String ¦- type: T[] ¦- type is an instance of GenericArrayType ¦- GenericArrayType#getGenericComponentType(): T ¦- type: T ¦- type is an instance of TypeVariable ¦- TypeVariable#getName(): T ¦- TypeVariable#getBounds(): [class java.lang.Thread] ¦- TypeVariable#bound: class java.lang.Thread ¦- type: class java.lang.Thread ¦- type is an instance of Class ¦- Class Name: java.lang.Thread
|
|