@Test void shouldReturnNullForNotPresentGenerics() throws ReflectiveOperationException { Field field1 = GenericTypesClass.class.getDeclaredField("untypedIterable"); Field field2 = GenericTypesClass.class.getDeclaredField("booleanField"); Type untypedParam = GenericTypesClass.class.getDeclaredMethod("withUntypedSet", Set.class) .getGenericParameterTypes()[0]; Class<?> fieldType1 = ReflectionUtils.getGenericType(field1.getGenericType()); Class<?> fieldType2 = ReflectionUtils.getGenericType(field2.getGenericType()); Class<?> paramType = ReflectionUtils.getGenericType(untypedParam); assertThat(fieldType1, nullValue()); assertThat(fieldType2, nullValue()); assertThat(paramType, nullValue()); }
@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);  }