### Question:
ClassUtil { public static Class<?> getGeneric(final Constructor<?> constructor, final int i) { Assertion.check() .isNotNull(constructor); return getGeneric( constructor.getGenericParameterTypes()[i], () -> new UnsupportedOperationException("La détection du générique n'a pas pu être effectuée sur le constructeur " + constructor)); } private  ClassUtil(); static Object newInstance(final String javaClassName); static J newInstance(final String javaClassName, final Class<J> type); static J newInstance(final Class<J> clazz); static J newInstance(final Constructor<J> constructor, final Object[] args); static Constructor<J> findConstructor(final Class<J> clazz, final Class<?>[] parameterTypes); static Class<?> classForName(final String javaClassName); static Class<? extends J> classForName(final String javaClassName, final Class<J> type); static Object invoke(final Object instance, final Method method, final Object... args); static void set(final Object instance, final Field field, final Object value); static Object get(final Object instance, final Field field); static Method findMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterTypes); static Collection<Field> getAllFields(final Class<?> clazz, final Class<? extends Annotation> annotation); static Collection<Method> getAllMethods(final Class<?> clazz, final Class<? extends Annotation> annotation); static Collection<Field> getAllFields(final Class<?> clazz); static Collection<Method> getAllMethods(final Class<?> clazz); static Set<Class<?>> getAllInterfaces(final Class<?> clazz); static Class<?> getGeneric(final Constructor<?> constructor, final int i); static Class<?> getGeneric(final Method method, final int i); static Class<?> getGeneric(final Field field); static Class<?> getGeneric(
			final Type type,
			final Supplier<RuntimeException> exceptionSupplier); static String getPropertyName(final Method method);  }### Answer:
@Test public void testGeneric() throws SecurityException, NoSuchFieldException { Field field; field = MyGenerics.class.getField("myOption"); assertEquals(Long.class, ClassUtil.getGeneric(field)); field = MyGenerics.class.getField("myList"); assertEquals(Long.class, ClassUtil.getGeneric(field)); field = MyGenerics.class.getField("myList3"); assertEquals(Map.class, ClassUtil.getGeneric(field)); }
@Test public void testGenericReturn() throws NoSuchMethodException, SecurityException { assertEquals(String.class, ClassUtil.getGeneric( this.getClass().getMethod("getWords").getGenericReturnType(), () -> new IllegalStateException())); }
@Test public void testGenericWithError() throws SecurityException { assertThrows(Exception.class, () -> { final Field field = MyGenerics.class.getField("myList2"); final Class<?> generic = ClassUtil.getGeneric(field); nop(generic); }); }
@Test public void testMethodParameterGeneric() throws NoSuchMethodException, SecurityException { final Method method = MyGenerics.class.getMethod("setOption", Optional.class); assertEquals(Long.class, ClassUtil.getGeneric(method, 0)); }
@Test public void testConstructorParameterGeneric() throws NoSuchMethodException, SecurityException { final Constructor<MyGenerics> constructor = MyGenerics.class.getConstructor(Optional.class, List.class, List.class, List.class); assertEquals(Long.class, ClassUtil.getGeneric(constructor, 0)); assertEquals(Long.class, ClassUtil.getGeneric(constructor, 1)); assertEquals(Map.class, ClassUtil.getGeneric(constructor, 3)); }
@Test public void testConstructorParameterGenericWithError() throws NoSuchMethodException, SecurityException { final Constructor<MyGenerics> constructor = MyGenerics.class.getConstructor(Optional.class, List.class, List.class, List.class); assertThrows(Exception.class, () -> { final Class<?> generic = ClassUtil.getGeneric(constructor, 2); nop(generic); }); }