GenericsUtil { protected static <T> List<ParameterizedType> getAncestorTypeList( Class<T> genericClass, Class<? extends T> descendantClass) throws IllegalStateException { List<ParameterizedType> ancestorTypeList = new ArrayList<ParameterizedType>(); Class<?> clazz = descendantClass; boolean isInterface = genericClass.isInterface(); while (clazz != null) { Type type = clazz.getGenericSuperclass(); if (checkParameterizedType(type, genericClass, ancestorTypeList)) { break; } if (!isInterface) { clazz = clazz.getSuperclass(); continue; } if (checkInterfaceAncestors(genericClass, ancestorTypeList, clazz)) { break; } clazz = clazz.getSuperclass(); } if (ancestorTypeList.isEmpty()) { throw new IllegalStateException("Argument 'genericClass'(" + genericClass.getName() + ") does not declare type parameter"); } ParameterizedType targetType = ancestorTypeList.get(ancestorTypeList .size() - 1); if (!targetType.getRawType().equals(genericClass)) { throw new IllegalStateException("Class(" + descendantClass.getName() + ") is not concrete class of Class(" + genericClass .getName() + ")"); } return ancestorTypeList; }  @SuppressWarnings("rawtypes") static Class[] resolveParameterizedClass(Class<T> genericClass,
            Class<? extends T> descendantClass); static Class<? extends Object> resolveParameterizedClass(
            Class<T> genericClass, Class<? extends T> descendantClass,
            int index);  }
@Test public void testGetAncestorTypeList01() throws Exception { try { GenericsUtil.getAncestorTypeList(Object.class, String.class); fail("例外がスローされませんでした。"); } catch (IllegalStateException e) { assertEquals("Argument 'genericClass'(" + Object.class.getName() + ") does not declare type parameter", e.getMessage()); assertEquals(IllegalStateException.class.getName(), e.getClass() .getName()); } }
@Test public void testGetAncestorTypeList02() throws Exception { List<ParameterizedType> result = GenericsUtil.getAncestorTypeList( FutureTask.class, GenericsUtil_FutureTaskStub02.class); assertNotNull(result); assertEquals(1, result.size()); assertEquals(GenericsUtil_FutureTaskStub02.class.getGenericSuperclass(), result.get(0)); }
@Test public void testGetAncestorTypeList03() throws Exception { List<ParameterizedType> result = GenericsUtil.getAncestorTypeList( FutureTask.class, GenericsUtil_FutureTaskStub03.class); assertNotNull(result); assertEquals(2, result.size()); assertEquals(getParameterizedClassList(FutureTask.class, GenericsUtil_FutureTaskStub03.class), result); }
@Test public void testGetAncestorTypeList04() throws Exception { List<ParameterizedType> result = GenericsUtil.getAncestorTypeList( Callable.class, GenericsUtil_CallableStub01.class); assertNotNull(result); assertEquals(1, result.size()); assertEquals(GenericsUtil_CallableStub01.class .getGenericInterfaces()[0], result.get(0)); }
@Test public void testGetAncestorTypeList05() throws Exception { List<ParameterizedType> result = GenericsUtil.getAncestorTypeList( Callable.class, GenericsUtil_CallableStub02.class); assertNotNull(result); assertEquals(1, result.size()); assertEquals(GenericsUtil_CallableStub02.class .getGenericInterfaces()[0], result.get(0)); }
@Test public void testGetAncestorTypeList06() throws Exception { List<ParameterizedType> result = GenericsUtil.getAncestorTypeList( Callable.class, GenericsUtil_CallableStub03.class); assertNotNull(result); assertEquals(1, result.size()); assertEquals(GenericsUtil_CallableStub03.class .getGenericInterfaces()[0], result.get(0)); }