commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r614667 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/ test/java/org/apache/commons/configuration2/
Date Wed, 23 Jan 2008 21:00:31 GMT
Author: oheger
Date: Wed Jan 23 13:00:30 2008
New Revision: 614667

URL: http://svn.apache.org/viewvc?rev=614667&view=rev
Log:
Fixed Java 1.5 related warnings; improved type safety

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertyConverter.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DataConfiguration.java?rev=614667&r1=614666&r2=614667&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
Wed Jan 23 13:00:30 2008
@@ -472,7 +472,7 @@
                 int i = 0;
                 while (it.hasNext())
                 {
-                    Array.set(array, i++, PropertyConverter.to(cls, interpolate(it.next()),
null));
+                    Array.set(array, i++, PropertyConverter.to(ClassUtils.primitiveToWrapper(cls),
interpolate(it.next()), null));
                 }
             }
             else
@@ -480,7 +480,7 @@
                 try
                 {
                     // attempt to convert a single value
-                    Object convertedValue = PropertyConverter.to(cls, interpolate(value),
null);
+                    Object convertedValue = PropertyConverter.to(ClassUtils.primitiveToWrapper(cls),
interpolate(value), null);
 
                     // create an array of one element
                     array = Array.newInstance(cls, 1);

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java?rev=614667&r1=614666&r2=614667&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java
Wed Jan 23 13:00:30 2008
@@ -21,7 +21,6 @@
 import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.net.InetAddress;
@@ -41,7 +40,6 @@
 
 import org.apache.commons.lang.BooleanUtils;
 import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.SystemUtils;
 
 /**
  * A utility class to convert the configuration properties into any type.
@@ -64,11 +62,8 @@
     /** Constant for the radix of hex numbers.*/
     private static final int HEX_RADIX = 16;
 
-    /** Constant for the Java version 1.5.*/
-    private static final float JAVA_VERSION_1_5 = 1.5f;
-
     /** Constant for the argument classes of the Number constructor that takes a String.
*/
-    private static final Class[] CONSTR_ARGS = {String.class};
+    private static final Class<?>[] CONSTR_ARGS = {String.class};
 
     /** The fully qualified name of {@link javax.mail.internet.InternetAddress} */
     private static final String INTERNET_ADDRESS_CLASSNAME = "javax.mail.internet.InternetAddress";
@@ -94,82 +89,93 @@
      *
      * @since 1.5
      */
-    static Object to(Class cls, Object value, Object[] params) throws ConversionException
+    @SuppressWarnings("unchecked")
+    static <T> T to(Class<T> cls, Object value, Object[] params)
+            throws ConversionException
     {
+        Object result = null;
+
         if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls))
         {
-            return toBoolean(value);
+            result = toBoolean(value);
         }
         else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive())
         {
             if (Integer.class.equals(cls) || Integer.TYPE.equals(cls))
             {
-                return toInteger(value);
+                result = toInteger(value);
             }
             else if (Long.class.equals(cls) || Long.TYPE.equals(cls))
             {
-                return toLong(value);
+                result = toLong(value);
             }
             else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls))
             {
-                return toByte(value);
+                result = toByte(value);
             }
             else if (Short.class.equals(cls) || Short.TYPE.equals(cls))
             {
-                return toShort(value);
+                result = toShort(value);
             }
             else if (Float.class.equals(cls) || Float.TYPE.equals(cls))
             {
-                return toFloat(value);
+                result = toFloat(value);
             }
             else if (Double.class.equals(cls) || Double.TYPE.equals(cls))
             {
-                return toDouble(value);
+                result = toDouble(value);
             }
             else if (BigInteger.class.equals(cls))
             {
-                return toBigInteger(value);
+                result = toBigInteger(value);
             }
             else if (BigDecimal.class.equals(cls))
             {
-                return toBigDecimal(value);
+                result = toBigDecimal(value);
             }
         }
         else if (Date.class.equals(cls))
         {
-            return toDate(value, (String) params[0]);
+            result = toDate(value, (String) params[0]);
         }
         else if (Calendar.class.equals(cls))
         {
-            return toCalendar(value, (String) params[0]);
+            result = toCalendar(value, (String) params[0]);
         }
         else if (URL.class.equals(cls))
         {
-            return toURL(value);
+            result = toURL(value);
         }
         else if (Locale.class.equals(cls))
         {
-            return toLocale(value);
+            result = toLocale(value);
         }
         else if (isEnum(cls))
         {
-            return toEnum(value, cls);
+            // This causes an unchecked warning because the concrete Enum class
+            // cannot be fully determined.
+            result = toEnum(value, cls.asSubclass(Enum.class));
         }
         else if (Color.class.equals(cls))
         {
-            return toColor(value);
+            result = toColor(value);
         }
         else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME))
         {
-            return toInternetAddress(value);
+            result = toInternetAddress(value);
         }
         else if (InetAddress.class.isAssignableFrom(cls))
         {
-            return toInetAddress(value);
+            result = toInetAddress(value);
+        }
+        else
+        {
+            throw new ConversionException("The value '" + value + "' ("
+                    + value.getClass() + ")" + " can't be converted to a "
+                    + cls.getName() + " object");
         }
 
-        throw new ConversionException("The value '" + value + "' (" + value.getClass() +
")"
-                + " can't be converted to a " + cls.getName() + " object");
+        return cls.cast(result);
     }
 
     /**
@@ -378,7 +384,7 @@
      * @return the converted number
      * @throws ConversionException if the object cannot be converted
      */
-    static Number toNumber(Object value, Class targetClass) throws ConversionException
+    static Number toNumber(Object value, Class<? extends Number> targetClass) throws
ConversionException
     {
         if (value instanceof Number)
         {
@@ -403,7 +409,7 @@
 
             try
             {
-                Constructor constr = targetClass.getConstructor(CONSTR_ARGS);
+                Constructor<? extends Number> constr = targetClass.getConstructor(CONSTR_ARGS);
                 return (Number) constr.newInstance(new Object[]{str});
             }
             catch (InvocationTargetException itex)
@@ -467,14 +473,14 @@
         }
         else if (value instanceof String)
         {
-            List elements = split((String) value, '_');
+            List<String> elements = split((String) value, '_');
             int size = elements.size();
 
-            if (size >= 1 && (((String) elements.get(0)).length() == 2 || ((String)
elements.get(0)).length() == 0))
+            if (size >= 1 && ((elements.get(0)).length() == 2 || (elements.get(0)).length()
== 0))
             {
-                String language = (String) elements.get(0);
-                String country = (String) ((size >= 2) ? elements.get(1) : "");
-                String variant = (String) ((size >= 3) ? elements.get(2) : "");
+                String language = elements.get(0);
+                String country = (size >= 2) ? elements.get(1) : "";
+                String variant = (size >= 3) ? elements.get(2) : "";
 
                 return new Locale(language, country, variant);
             }
@@ -499,16 +505,16 @@
      * @param delimiter  the delimiter
      * @return a list with the single tokens
      */
-    public static List split(String s, char delimiter)
+    public static List<String> split(String s, char delimiter)
     {
         if (s == null)
         {
-            return new ArrayList();
+            return new ArrayList<String>();
         }
 
-        List list = new ArrayList();
+        List<String> list = new ArrayList<String>();
 
-        StringBuffer token = new StringBuffer();
+        StringBuilder token = new StringBuilder();
         int begin = 0;
         boolean inEscape = false;
 
@@ -534,7 +540,7 @@
                 {
                     // found a list delimiter -> add token and reset buffer
                     list.add(token.toString().trim());
-                    token = new StringBuffer();
+                    token = new StringBuilder();
                 }
                 else if (c == LIST_ESC_CHAR)
                 {
@@ -698,9 +704,11 @@
         }
         else if (value instanceof String)
         {
+            // Invoke per reflection because the dependency to Java mail
+            // should be optional.
             try
             {
-                Constructor ctor = Class.forName(INTERNET_ADDRESS_CLASSNAME).getConstructor(new
Class[] {String.class});
+                Constructor<?> ctor = Class.forName(INTERNET_ADDRESS_CLASSNAME).getConstructor(new
Class[] {String.class});
                 return ctor.newInstance(new Object[] {value});
             }
             catch (Exception e)
@@ -717,23 +725,9 @@
     /**
      * Calls Class.isEnum() on Java 5, returns false on older JRE.
      */
-    static boolean isEnum(Class cls)
+    static boolean isEnum(Class<?> cls)
     {
-        if (!SystemUtils.isJavaVersionAtLeast(JAVA_VERSION_1_5))
-        {
-            return false;
-        }
-
-        try
-        {
-            Method isEnumMethod = Class.class.getMethod("isEnum", new Class[] {});
-            return ((Boolean) isEnumMethod.invoke(cls, new Object[] {})).booleanValue();
-        }
-        catch (Exception e)
-        {
-            // impossible
-            throw new RuntimeException(e.getMessage());
-        }
+        return cls.isEnum();
     }
 
     /**
@@ -746,18 +740,19 @@
      *
      * @since 1.5
      */
-    static Object toEnum(Object value, Class cls) throws ConversionException
+    static <T extends Enum<T>> T toEnum(Object value, Class<T> cls) throws
ConversionException
     {
         if (value.getClass().equals(cls))
         {
-            return value;
+            // already an enum => return directly
+            return cls.cast(value);
         }
         else if (value instanceof String)
         {
+            // For strings try to find the matching enum literal
             try
             {
-                Method valueOfMethod = cls.getMethod("valueOf", new Class[] {String.class});
-                return valueOfMethod.invoke(null, new Object[] {value});
+                return Enum.valueOf(cls, String.valueOf(value));
             }
             catch (Exception e)
             {
@@ -766,12 +761,11 @@
         }
         else if (value instanceof Number)
         {
+            // A number is interpreted as the ordinal index of an enum literal
             try
             {
-                Method valuesMethod = cls.getMethod("values", new Class[] {});
-                Object valuesArray = valuesMethod.invoke(null, new Object[] {});
-
-                return Array.get(valuesArray, ((Number) value).intValue());
+                T[] valuesArray = cls.getEnumConstants();
+                return valuesArray[((Number) value).intValue()];
             }
             catch (Exception e)
             {
@@ -867,7 +861,7 @@
      * @param delimiter the delimiter for String values
      * @return an iterator for accessing the single values
      */
-    public static Iterator<Object> toIterator(Object value, char delimiter)
+    public static Iterator<?> toIterator(Object value, char delimiter)
     {
         return flatten(value, delimiter).iterator();
     }
@@ -895,7 +889,7 @@
      * @return a &quot;flat&quot; collection containing all primitive values of
      *         the passed in object
      */
-    public static Collection<Object> flatten(Object value, char delimiter)
+    public static Collection<?> flatten(Object value, char delimiter)
     {
         if (value instanceof String)
         {

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertyConverter.java?rev=614667&r1=614666&r2=614667&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertyConverter.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertyConverter.java
Wed Jan 23 13:00:30 2008
@@ -17,7 +17,7 @@
 
 package org.apache.commons.configuration2;
 
-import java.lang.reflect.Method;
+import java.lang.annotation.ElementType;
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -25,11 +25,6 @@
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.commons.configuration2.ConversionException;
-import org.apache.commons.configuration2.PropertiesConfiguration;
-import org.apache.commons.configuration2.PropertyConverter;
-import org.apache.commons.lang.SystemUtils;
-
 import junit.framework.TestCase;
 
 /**
@@ -43,10 +38,16 @@
     /** An array with test values for the flatten test.*/
     private static final Integer[] FLATTEN_VALUES = { 1, 2, 3, 4, 5, 6, 28 };
 
+    /** The enum class we use in tests for enum conversions.*/
+    private static Class<ElementType> ENUM_CLASS = ElementType.class;
+
+    /** An enumeration object used for testing conversions with enums.*/
+    private static ElementType ENUM_OBJECT = ElementType.METHOD;
+
     public void testSplit()
     {
         String s = "abc, xyz , 123";
-        List list = PropertyConverter.split(s, ',');
+        List<String> list = PropertyConverter.split(s, ',');
 
         assertEquals("size", 3, list.size());
         assertEquals("1st token for '" + s + "'", "abc", list.get(0));
@@ -57,7 +58,7 @@
     public void testSplitWithEscapedSeparator()
     {
         String s = "abc\\,xyz, 123";
-        List list = PropertyConverter.split(s, ',');
+        List<String> list = PropertyConverter.split(s, ',');
 
         assertEquals("size", 2, list.size());
         assertEquals("1st token for '" + s + "'", "abc,xyz", list.get(0));
@@ -67,7 +68,7 @@
     public void testSplitEmptyValues()
     {
         String s = ",,";
-        List list = PropertyConverter.split(s, ',');
+        List<String> list = PropertyConverter.split(s, ',');
 
         assertEquals("size", 3, list.size());
         assertEquals("1st token for '" + s + "'", "", list.get(0));
@@ -78,7 +79,7 @@
     public void testSplitWithEndingSlash()
     {
         String s = "abc, xyz\\";
-        List list = PropertyConverter.split(s, ',');
+        List<String> list = PropertyConverter.split(s, ',');
 
         assertEquals("size", 2, list.size());
         assertEquals("1st token for '" + s + "'", "abc", list.get(0));
@@ -87,7 +88,7 @@
 
     public void testSplitNull()
     {
-        List list = PropertyConverter.split(null, ',');
+        List<String> list = PropertyConverter.split(null, ',');
         assertNotNull(list);
         assertTrue(list.isEmpty());
     }
@@ -97,7 +98,7 @@
      */
     public void testSplitEscapeEscapeChar()
     {
-        List list = PropertyConverter.split("C:\\Temp\\\\,xyz", ',');
+        List<String> list = PropertyConverter.split("C:\\Temp\\\\,xyz", ',');
         assertEquals("Wrong list size", 2, list.size());
         assertEquals("Wrong element 1", "C:\\Temp\\", list.get(0));
         assertEquals("Wrong element 2", "xyz", list.get(1));
@@ -117,7 +118,7 @@
     {
         int[] array = new int[]{1, 2, 3};
 
-        Iterator it = PropertyConverter.toIterator(array, ',');
+        Iterator<?> it = PropertyConverter.toIterator(array, ',');
 
         assertEquals("1st element", new Integer(1), it.next());
         assertEquals("2nd element", new Integer(2), it.next());
@@ -201,11 +202,11 @@
      *
      * @param col the resulting collection
      */
-    private void checkFlattenResult(Collection<Object> col)
+    private void checkFlattenResult(Collection<?> col)
     {
         assertEquals("Wrong number of elements", FLATTEN_VALUES.length, col
                 .size());
-        Iterator<Object> it = col.iterator();
+        Iterator<?> it = col.iterator();
         for (Integer val : FLATTEN_VALUES)
         {
             assertEquals("Wrong value in result", val.toString(), String
@@ -351,72 +352,21 @@
         }
     }
 
-    /**
-     * Tests conversion to numbers when the passed in target class is invalid.
-     * This should cause an exception.
-     */
-    public void testToNumberWithInvalidClass()
-    {
-        try
-        {
-            PropertyConverter.toNumber("42", Object.class);
-            fail("Could convert to invalid target class!");
-        }
-        catch (ConversionException cex)
-        {
-            //ok
-        }
-    }
-
-    // enumeration type used for the tests, Java 5 only
-    private Class enumClass;
-    private Object enumObject;
-    {
-        if (SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            try
-            {
-                enumClass = Class.forName("java.lang.annotation.ElementType");
-
-                Method valueOfMethod = enumClass.getMethod("valueOf", new Class[] { String.class
});
-                enumObject = valueOfMethod.invoke(null, new Object[] { "METHOD" });
-            }
-            catch (Exception e)
-            {
-            }
-        }
-    }
-
     public void testToEnumFromEnum()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
-        assertEquals(enumObject, PropertyConverter.toEnum(enumObject, enumClass));
+        assertEquals(ENUM_OBJECT, PropertyConverter.toEnum(ENUM_OBJECT, ENUM_CLASS));
     }
 
     public void testToEnumFromString()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
-        assertEquals(enumObject, PropertyConverter.toEnum("METHOD", enumClass));
+        assertEquals(ENUM_OBJECT, PropertyConverter.toEnum("METHOD", ENUM_CLASS));
     }
 
     public void testToEnumFromInvalidString()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
         try
         {
-            assertEquals(enumObject, PropertyConverter.toEnum("FOO", enumClass));
+            PropertyConverter.toEnum("FOO", ENUM_CLASS);
             fail("Could convert invalid String!");
         }
         catch (ConversionException e)
@@ -427,24 +377,14 @@
 
     public void testToEnumFromNumber()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
-        assertEquals(enumObject, PropertyConverter.toEnum(new Integer(2), enumClass));
+        assertEquals(ENUM_OBJECT, PropertyConverter.toEnum(new Integer(2), ENUM_CLASS));
     }
 
     public void testToEnumFromInvalidNumber()
     {
-        if (!SystemUtils.isJavaVersionAtLeast(1.5f))
-        {
-            return;
-        }
-
         try
         {
-            assertEquals(enumObject, PropertyConverter.toEnum(new Integer(-1), enumClass));
+            PropertyConverter.toEnum(new Integer(-1), ENUM_CLASS);
             fail("Could convert invalid number!");
         }
         catch (ConversionException e)



Mime
View raw message