commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1535155 - in /commons/proper/beanutils/branches/java5/src: main/java/org/apache/commons/beanutils/converters/ClassConverter.java test/java/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
Date Wed, 23 Oct 2013 20:17:50 GMT
Author: oheger
Date: Wed Oct 23 20:17:50 2013
New Revision: 1535155

URL: http://svn.apache.org/r1535155
Log:
Generified ClassConverter.

The converter now also checks whether it supports the passed in target type.

Modified:
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/ClassConverter.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/ClassConverterTestCase.java

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/ClassConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/ClassConverter.java?rev=1535155&r1=1535154&r2=1535155&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/ClassConverter.java
(original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/ClassConverter.java
Wed Oct 23 20:17:50 2013
@@ -59,7 +59,7 @@ public final class ClassConverter extend
      * @since 1.8.0
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<?> getDefaultType() {
         return Class.class;
     }
 
@@ -72,7 +72,7 @@ public final class ClassConverter extend
      */
     @Override
     protected String convertToString(Object value) {
-        return (value instanceof Class) ? ((Class)value).getName() : value.toString();
+        return (value instanceof Class) ? ((Class<?>)value).getName() : value.toString();
     }
 
     /**
@@ -85,21 +85,25 @@ public final class ClassConverter extend
      * @since 1.8.0
      */
     @Override
-    protected Object convertToType(Class type, Object value) throws Throwable {
-        ClassLoader classLoader =
-            Thread.currentThread().getContextClassLoader();
-        if (classLoader != null) {
-            try {
-                return (classLoader.loadClass(value.toString()));
-            } catch (ClassNotFoundException ex) {
-                // Don't fail, carry on and try this class's class loader
-                // (see issue# BEANUTILS-263)
+    protected <T> T convertToType(Class<T> type, Object value) throws Throwable
{
+        if (Class.class.equals(type)) {
+            ClassLoader classLoader = Thread.currentThread()
+                    .getContextClassLoader();
+            if (classLoader != null) {
+                try {
+                    return type.cast(classLoader.loadClass(value.toString()));
+                } catch (ClassNotFoundException ex) {
+                    // Don't fail, carry on and try this class's class loader
+                    // (see issue# BEANUTILS-263)
+                }
             }
+
+            // Try this class's class loader
+            classLoader = ClassConverter.class.getClassLoader();
+            return type.cast(classLoader.loadClass(value.toString()));
         }
 
-        // Try this class's class loader
-        classLoader = ClassConverter.class.getClassLoader();
-        return (classLoader.loadClass(value.toString()));
+        throw conversionException(type, value);
     }
 
 }

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/ClassConverterTestCase.java?rev=1535155&r1=1535154&r2=1535155&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
(original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
Wed Oct 23 20:17:50 2013
@@ -150,4 +150,16 @@ public class ClassConverterTestCase exte
         }
     }
 
+    /**
+     * Tries a conversion to an unsupported target type.
+     */
+    public void testUnsupportedTargetType() {
+        Converter converter = new ClassConverter();
+        try {
+            converter.convert(Integer.class, getClass().getName());
+            fail("Invalid target class not detected!");
+        } catch (ConversionException cex) {
+            // expected result
+        }
+    }
 }



Mime
View raw message