Skip to content
Snippets Groups Projects
Commit c7266e0e authored by ribeaudc's avatar ribeaudc
Browse files

remove: - 'ClassUtils.tryGetInterfaceTypeArgument'.

change: - Generics should only be used at compiling time. Avoid using reflection on it.

SVN: 7247
parent 63a94540
No related branches found
No related tags found
No related merge requests found
......@@ -20,8 +20,6 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
......@@ -353,38 +351,4 @@ public final class ClassUtils
}
return field;
}
/**
* For given <var>clazz</var> tries to retrieve the generic interface of given class, then
* returns the type argument found at given <var>index</var>.
*
* @return <code>null</code> if not found.
*/
public final static Class<?> tryGetInterfaceTypeArgument(final Class<?> clazz,
final Class<?> genericInterfaceClass, final int index)
{
assert clazz != null : "Unspecified class";
assert genericInterfaceClass != null && genericInterfaceClass.isInterface() : "Is not defined or not an interface";
assert index > -1 : "Only positive index (> -1)";
final Type[] genericInterfaces = clazz.getGenericInterfaces();
for (final Type genericInterface : genericInterfaces)
{
// Only typed interface is an instance of 'ParameterizedType'.
// Other is just a 'Class'.
if (genericInterface instanceof ParameterizedType)
{
final ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
if (genericInterfaceClass.isAssignableFrom((Class<?>) parameterizedType
.getRawType()))
{
final Type typeArgument = parameterizedType.getActualTypeArguments()[index];
if (typeArgument instanceof Class)
{
return (Class<?>) typeArgument;
}
}
}
}
return null;
}
}
......@@ -205,31 +205,6 @@ public final class ClassUtilsTest
assertSame(object, myExtendedClass.finalObject);
}
@Test
public final void testGetInterfaceTypeArgument()
{
boolean fail = true;
try
{
ClassUtils.tryGetInterfaceTypeArgument(null, null, -1);
} catch (final AssertionError e)
{
fail = false;
}
assertFalse(fail);
Class<?> typeArgument =
ClassUtils.tryGetInterfaceTypeArgument(ExtendingA.class, IExtendingIA.class, 0);
assertNotNull(typeArgument);
typeArgument =
ClassUtils.tryGetInterfaceTypeArgument(ExtendingExtendingA.class, IB.class, 0);
assertNull(typeArgument);
// We do not support array type argument
typeArgument =
ClassUtils
.tryGetInterfaceTypeArgument(ExtendingArrayA.class, IExtendingIA.class, 0);
assertNull(typeArgument);
}
//
// Helper Classes
//
......@@ -309,10 +284,6 @@ public final class ClassUtilsTest
{
}
private static class ExtendingArrayA extends A implements IExtendingIA<String[]>
{
}
private static class ExtendingExtendingA extends ExtendingA implements IB, IA<String>
{
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment