commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r980557 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/reflect/TypeUtils.java test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
Date Thu, 29 Jul 2010 20:21:10 GMT
Author: mbenson
Date: Thu Jul 29 20:21:10 2010
New Revision: 980557

URL: http://svn.apache.org/viewvc?rev=980557&view=rev
Log:
add methods for inspecting array types

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java?rev=980557&r1=980556&r2=980557&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
Thu Jul 29 20:21:10 2010
@@ -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;
+    }
+
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java?rev=980557&r1=980556&r2=980557&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
Thu Jul 29 20:21:10 2010
@@ -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>.BBC
 
     public interface AInterface<T> {
     }
-}
\ No newline at end of file
+}



Mime
View raw message