GenericsUtil { @SuppressWarnings("unchecked") protected static Class<? extends Object> getRawClass( Type type) throws IllegalStateException { if (type instanceof Class) { return (Class<? extends Object>) type; } else if (type instanceof ParameterizedType) { return getRawClass(((ParameterizedType) type).getRawType()); } else if (type instanceof GenericArrayType) { Type componentType = ((GenericArrayType) type) .getGenericComponentType(); Class<? extends Object> componentClass = getRawClass(componentType); return Array.newInstance(componentClass, 0).getClass(); } throw new IllegalStateException("Type(" + type + ") is not instance of " + ParameterizedType.class.getName() + ", " + GenericArrayType.class.getName() + " nor " + Class.class .getName()); }  @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 testGetRawClass01() throws Exception { Type type = String.class; Class<?> result = GenericsUtil.getRawClass(type); assertEquals(String.class, result); }
@Test public void testGetRawClass02() throws Exception { Type type = getTypeArgument(GenericsUtil_ArrayListStub01.class, 0); Class<?> result = GenericsUtil.getRawClass(type); assertEquals(Map.class, result); }
@Test public void testGetRawClass03() throws Exception { Type type = getTypeArgument(GenericsUtil_ArrayListStub02.class, 0); Class<?> result = GenericsUtil.getRawClass(type); assertEquals(String[].class, result); }
@Test public void testGetRawClass04() throws Exception { Type type = new GenericsUtil_WildCardTypeStub01(); try { GenericsUtil.getRawClass(type); fail("例外がスローされませんでした。"); } catch (IllegalStateException e) { assertEquals("Type(" + type + ") is not instance of " + ParameterizedType.class.getName() + ", " + GenericArrayType.class.getName() + " nor " + Class.class .getName(), e.getMessage()); assertEquals(IllegalStateException.class.getName(), e.getClass() .getName()); } }
@Test public void testGetRawClass05() throws Exception { Type type = new GenericsUtil_GenericArrayTypeStub01(); Class<?> result = GenericsUtil.getRawClass(type); assertEquals(String[][].class, result); }