add methods for inspecting array types
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@980557 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5bfa8550e8
commit
b74e64b211
|
@ -1057,4 +1057,29 @@ public class TypeUtils {
|
|||
throw new IllegalArgumentException("unknown type: " + type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Learn whether the specified type denotes an array type.
|
||||
* @param type
|
||||
* @return <code>true</code> if <code>type</code> is an array class or a {@link GenericArrayType}.
|
||||
*/
|
||||
public static boolean isArrayType(Type type) {
|
||||
return type instanceof GenericArrayType || type instanceof Class<?> && ((Class<?>) type).isArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array component type of <code>type</code>.
|
||||
* @param type
|
||||
* @return component type or null if type is not an array type
|
||||
*/
|
||||
public static Type getArrayComponentType(Type type) {
|
||||
if (type instanceof Class<?>) {
|
||||
Class<?> clazz = (Class<?>) type;
|
||||
return clazz.isArray() ? clazz.getComponentType() : null;
|
||||
}
|
||||
if (type instanceof GenericArrayType) {
|
||||
return ((GenericArrayType) type).getGenericComponentType();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.apache.commons.lang3.reflect.testbed.Foo;
|
||||
|
@ -557,6 +557,100 @@ public class TypeUtilsTest<B> {
|
|||
.getDeclaredField("barParents").getGenericType(), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsArrayTypeClasses() {
|
||||
Assert.assertTrue(TypeUtils.isArrayType(boolean[].class));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(byte[].class));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(short[].class));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(int[].class));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(char[].class));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(long[].class));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(float[].class));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(double[].class));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(Object[].class));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(String[].class));
|
||||
|
||||
Assert.assertFalse(TypeUtils.isArrayType(boolean.class));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(byte.class));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(short.class));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(int.class));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(char.class));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(long.class));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(float.class));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(double.class));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(Object.class));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsArrayGenericTypes() throws Exception {
|
||||
Method method = getClass().getMethod("dummyMethod", List.class, List.class, List.class,
|
||||
List.class, List.class, List.class, List.class, List[].class, List[].class,
|
||||
List[].class, List[].class, List[].class, List[].class, List[].class);
|
||||
|
||||
Type[] types = method.getGenericParameterTypes();
|
||||
|
||||
Assert.assertFalse(TypeUtils.isArrayType(types[0]));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(types[1]));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(types[2]));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(types[3]));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(types[4]));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(types[5]));
|
||||
Assert.assertFalse(TypeUtils.isArrayType(types[6]));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(types[7]));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(types[8]));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(types[9]));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(types[10]));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(types[11]));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(types[12]));
|
||||
Assert.assertTrue(TypeUtils.isArrayType(types[13]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrimitiveArrayComponentType() throws Exception {
|
||||
Assert.assertEquals(boolean.class, TypeUtils.getArrayComponentType(boolean[].class));
|
||||
Assert.assertEquals(byte.class, TypeUtils.getArrayComponentType(byte[].class));
|
||||
Assert.assertEquals(short.class, TypeUtils.getArrayComponentType(short[].class));
|
||||
Assert.assertEquals(int.class, TypeUtils.getArrayComponentType(int[].class));
|
||||
Assert.assertEquals(char.class, TypeUtils.getArrayComponentType(char[].class));
|
||||
Assert.assertEquals(long.class, TypeUtils.getArrayComponentType(long[].class));
|
||||
Assert.assertEquals(float.class, TypeUtils.getArrayComponentType(float[].class));
|
||||
Assert.assertEquals(double.class, TypeUtils.getArrayComponentType(double[].class));
|
||||
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(boolean.class));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(byte.class));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(short.class));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(int.class));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(char.class));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(long.class));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(float.class));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(double.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArrayComponentType() throws Exception {
|
||||
Method method = getClass().getMethod("dummyMethod", List.class, List.class, List.class,
|
||||
List.class, List.class, List.class, List.class, List[].class, List[].class,
|
||||
List[].class, List[].class, List[].class, List[].class, List[].class);
|
||||
|
||||
Type[] types = method.getGenericParameterTypes();
|
||||
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(types[0]));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(types[1]));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(types[2]));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(types[3]));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(types[4]));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(types[5]));
|
||||
Assert.assertNull(TypeUtils.getArrayComponentType(types[6]));
|
||||
Assert.assertEquals(types[0], TypeUtils.getArrayComponentType(types[7]));
|
||||
Assert.assertEquals(types[1], TypeUtils.getArrayComponentType(types[8]));
|
||||
Assert.assertEquals(types[2], TypeUtils.getArrayComponentType(types[9]));
|
||||
Assert.assertEquals(types[3], TypeUtils.getArrayComponentType(types[10]));
|
||||
Assert.assertEquals(types[4], TypeUtils.getArrayComponentType(types[11]));
|
||||
Assert.assertEquals(types[5], TypeUtils.getArrayComponentType(types[12]));
|
||||
Assert.assertEquals(types[6], TypeUtils.getArrayComponentType(types[13]));
|
||||
}
|
||||
|
||||
public Iterable<? extends Map<Integer, ? extends Collection<?>>> iterable;
|
||||
|
||||
public static <G extends Comparable<G>> G stub() {
|
||||
|
@ -623,4 +717,4 @@ class AClass extends AAClass<String>.BBClass<Number> {
|
|||
|
||||
public interface AInterface<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue