GenericUtil { public static Class<?> getGenericClass(Class<?> clazz) { Type[] genericInterfaceTypes = clazz.getGenericInterfaces(); for (Type genericInterfaceType : genericInterfaceTypes) { if (genericInterfaceType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType)genericInterfaceType; return (Class<?>)parameterizedType.getActualTypeArguments()[0]; } } Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { return getGenericClass(superClass); } return Object.class; }  static Class<?> getGenericClass(Class<?> clazz); static Class<?> getGenericClass(Object object); static String getGenericClassName(Object object);  }
@Test public void testGetGenericClass() { Assert.assertEquals( String.class, GenericUtil.getGenericClass(new StringParameterizedType())); Assert.assertEquals( Object.class, GenericUtil.getGenericClass(new NoParameterizedTypeImpl())); Assert.assertEquals( Object.class, GenericUtil.getGenericClass(ExtendsNoParameterizedTypeImpl.class)); Assert.assertEquals( Object.class, GenericUtil.getGenericClass(NoParameterizedTypeImpl.class)); Assert.assertEquals( String.class, GenericUtil.getGenericClass(StringParameterizedType.class)); }