diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java index 182d41c12156e079dc76dbcf9cd03d037b21333c..16107789578bcef1784d9c7ef2730cd712a1c4ec 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java @@ -16,10 +16,8 @@ package ch.systemsx.cisd.common.utilities; -import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.GenericArrayType; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; @@ -357,13 +355,13 @@ public final class ClassUtils } /** - * Returns the type argument found at given <var>index</var> of given generic interface - * <var>genericInterfaceClass</var>. + * 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 <I> Class<?> tryGetInterfaceTypeArgument(final Class<?> clazz, - final Class<I> genericInterfaceClass, final int index) + 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"; @@ -371,23 +369,19 @@ public final class ClassUtils final Type[] genericInterfaces = clazz.getGenericInterfaces(); for (final Type genericInterface : genericInterfaces) { - // Only typed interface are instance of ParameterizedType. Other is just a Class. + // 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())) { - Type typeArgument = parameterizedType.getActualTypeArguments()[index]; - if (typeArgument instanceof GenericArrayType) + final Type typeArgument = parameterizedType.getActualTypeArguments()[index]; + if (typeArgument instanceof Class) { - final GenericArrayType genericArrayType = (GenericArrayType) typeArgument; - // TODO 2008-07-12, Christian Ribeaud: Is there a better way to do this? - return Array.newInstance( - ((Class<?>) genericArrayType.getGenericComponentType()), 0) - .getClass(); + return (Class<?>) typeArgument; } - return (Class<?>) typeArgument; } } } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java index 22acebabaa8ba6f0b97c159f389eeb5fb9769e17..1f102295df137d246a72b9a48c40588b0226ce6a 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java @@ -223,6 +223,11 @@ public final class ClassUtilsTest 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); } // @@ -304,6 +309,10 @@ public final class ClassUtilsTest { } + private static class ExtendingArrayA extends A implements IExtendingIA<String[]> + { + } + private static class ExtendingExtendingA extends ExtendingA implements IB, IA<String> { }