Author: julien(a)jboss.com
Date: 2008-03-20 10:32:45 -0400 (Thu, 20 Mar 2008)
New Revision: 10351
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/reflect/Reflection.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/reflect/ReflectionTestCase.java
Log:
added a safe cast util method
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/reflect/Reflection.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/reflect/Reflection.java 2008-03-20
14:29:00 UTC (rev 10350)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/reflect/Reflection.java 2008-03-20
14:32:45 UTC (rev 10351)
@@ -146,9 +146,14 @@
* @param value the value to cast
* @param type the type to downcast
* @return the casted value or null
+ * @throws IllegalArgumentException if the type argument is null
*/
- public static <T> T safeCast(Object value, Class<T> type)
+ public static <T> T safeCast(Object value, Class<T> type) throws
IllegalArgumentException
{
+ if (type == null)
+ {
+ throw new IllegalArgumentException("No null type accepted");
+ }
if (value == null)
{
return null;
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/reflect/ReflectionTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/reflect/ReflectionTestCase.java 2008-03-20
14:29:00 UTC (rev 10350)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/reflect/ReflectionTestCase.java 2008-03-20
14:32:45 UTC (rev 10351)
@@ -102,6 +102,26 @@
ArrayList list = new ArrayList();
assertSame(list, Reflection.safeCast(list, List.class));
assertSame(list, Reflection.safeCast(list, AbstractList.class));
+
+ //
+ try
+ {
+ Reflection.safeCast(null, null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+
+ //
+ try
+ {
+ Reflection.safeCast("foo", null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
}
}
Show replies by date