@Test void shouldGetTypedValue() throws ReflectiveOperationException { Field field = GenericTypesClass.class.getDeclaredField("stringProvider"); Type constrParam = GenericTypesClass.class.getDeclaredConstructor(Set.class, List.class) .getGenericParameterTypes()[0]; Type methodParam = GenericTypesClass.class.getDeclaredMethod("withTypeList", List.class) .getGenericParameterTypes()[0]; Class<?> fieldType = ReflectionUtils.getGenericType(field.getGenericType()); Class<?> constrParamType = ReflectionUtils.getGenericType(constrParam); Class<?> methodParamType = ReflectionUtils.getGenericType(methodParam); assertThat(fieldType, equalTo(String.class)); assertThat(constrParamType, equalTo(Integer.class)); assertThat(methodParamType, equalTo(Type.class)); }
@Nullable public static Class<?> getGenericType(@Nullable Type genericType) { if (genericType != null && genericType instanceof ParameterizedType) { Type[] types = ((ParameterizedType) genericType).getActualTypeArguments(); if (types.length > 0 && types[0] instanceof Class<?>) { return (Class<?>) types[0]; } } return null; }
ReflectionUtils { @Nullable public static Class<?> getGenericType(@Nullable Type genericType) { if (genericType != null && genericType instanceof ParameterizedType) { Type[] types = ((ParameterizedType) genericType).getActualTypeArguments(); if (types.length > 0 && types[0] instanceof Class<?>) { return (Class<?>) types[0]; } } return null; } }
ReflectionUtils { @Nullable public static Class<?> getGenericType(@Nullable Type genericType) { if (genericType != null && genericType instanceof ParameterizedType) { Type[] types = ((ParameterizedType) genericType).getActualTypeArguments(); if (types.length > 0 && types[0] instanceof Class<?>) { return (Class<?>) types[0]; } } return null; } private  ReflectionUtils(); }
ReflectionUtils { @Nullable public static Class<?> getGenericType(@Nullable Type genericType) { if (genericType != null && genericType instanceof ParameterizedType) { Type[] types = ((ParameterizedType) genericType).getActualTypeArguments(); if (types.length > 0 && types[0] instanceof Class<?>) { return (Class<?>) types[0]; } } return null; } private  ReflectionUtils(); static Object getFieldValue(Field field, Object instance); static void setField(Field field, Object instance, Object value); static Object invokeMethod(Method method, Object instance, Object... parameters); static T newInstance(Constructor<T> constructor, Object... parameters); @Nullable static Class<?> getGenericType(@Nullable Type genericType); @Nullable static Class<?> getCollectionType(Class<?> mainType, @Nullable Type genericType); static Object toSuitableCollectionType(Class<?> rawType, Set<T> result); static Method[] safeGetDeclaredMethods(Class<?> clazz); static Field[] safeGetDeclaredFields(Class<?> clazz); }
ReflectionUtils { @Nullable public static Class<?> getGenericType(@Nullable Type genericType) { if (genericType != null && genericType instanceof ParameterizedType) { Type[] types = ((ParameterizedType) genericType).getActualTypeArguments(); if (types.length > 0 && types[0] instanceof Class<?>) { return (Class<?>) types[0]; } } return null; } private  ReflectionUtils(); static Object getFieldValue(Field field, Object instance); static void setField(Field field, Object instance, Object value); static Object invokeMethod(Method method, Object instance, Object... parameters); static T newInstance(Constructor<T> constructor, Object... parameters); @Nullable static Class<?> getGenericType(@Nullable Type genericType); @Nullable static Class<?> getCollectionType(Class<?> mainType, @Nullable Type genericType); static Object toSuitableCollectionType(Class<?> rawType, Set<T> result); static Method[] safeGetDeclaredMethods(Class<?> clazz); static Field[] safeGetDeclaredFields(Class<?> clazz);  }