GenericsUtil { protected static <T> boolean checkParameterizedType(Type type, Class<T> genericClass, List<ParameterizedType> ancestorTypeList) { if (!(type instanceof ParameterizedType)) { return false; } ParameterizedType parameterlizedType = (ParameterizedType) type; if (!genericClass.isAssignableFrom((Class<?>) parameterlizedType .getRawType())) { return false; } ancestorTypeList.add(parameterlizedType); if (parameterlizedType.getRawType().equals(genericClass)) { return true; } return false; }  @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 testCheckParameterizedType01() throws Exception { List<ParameterizedType> list = new ArrayList<ParameterizedType>(); boolean result = GenericsUtil.checkParameterizedType(ArrayList.class, List.class, list); assertFalse(result); assertTrue(list.isEmpty()); }
@Test public void testCheckParameterizedType02() throws Exception { List<ParameterizedType> list = new ArrayList<ParameterizedType>(); boolean result = GenericsUtil.checkParameterizedType( GenericsUtil_ArrayListStub01.class, Map.class, list); assertFalse(result); assertTrue(list.isEmpty()); }
@Test public void testCheckParameterizedType03() throws Exception { List<ParameterizedType> list = new ArrayList<ParameterizedType>(); boolean result = GenericsUtil.checkParameterizedType( GenericsUtil_ArrayListStub01.class.getGenericSuperclass(), List.class, list); assertFalse(result); assertEquals(1, list.size()); assertEquals(GenericsUtil_ArrayListStub01.class.getGenericSuperclass(), list.get(0)); }
@Test public void testCheckParameterizedType04() throws Exception { Class<?> listClass = GenericsUtil_ArrayListStub01.class; while (listClass != null && !listClass.equals(AbstractList.class)) { listClass = listClass.getSuperclass(); } if (listClass == null) { fail(AbstractList.class.getName() + "が取得できません。"); } Type type = listClass.getGenericInterfaces()[0]; List<ParameterizedType> list = new ArrayList<ParameterizedType>(); boolean result = GenericsUtil.checkParameterizedType(type, List.class, list); assertTrue(result); assertEquals(1, list.size()); assertEquals(type, list.get(0)); }