You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by eb...@apache.org on 2007/04/20 00:48:30 UTC
svn commit: r530583 - in /jakarta/commons/proper/configuration/trunk:
src/java/org/apache/commons/configuration/
src/test/org/apache/commons/configuration/ xdocs/
Author: ebourg
Date: Thu Apr 19 15:48:29 2007
New Revision: 530583
URL: http://svn.apache.org/viewvc?view=rev&rev=530583
Log:
DataConfiguration now supports the Java 5 enumeration types
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java
jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java?view=diff&rev=530583&r1=530582&r2=530583
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DataConfiguration.java Thu Apr 19 15:48:29 2007
@@ -46,6 +46,7 @@
* <li>{@link java.awt.Color}</li>
* <li>{@link java.net.InetAddress}</li>
* <li>{@link javax.mail.internet.InternetAddress} (requires Javamail in the classpath)</li>
+ * <li>{@link java.lang.Enum} (Java 5 enumeration types)</li>
* </ul>
*
* Lists and arrays are available for all types.
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java?view=diff&rev=530583&r1=530582&r2=530583
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java Thu Apr 19 15:48:29 2007
@@ -18,8 +18,10 @@
package org.apache.commons.configuration;
import java.awt.Color;
+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,6 +43,7 @@
import org.apache.commons.collections.iterators.SingletonIterator;
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.
@@ -90,70 +93,74 @@
{
if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls))
{
- return PropertyConverter.toBoolean(value);
+ return toBoolean(value);
}
else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive())
{
if (Integer.class.equals(cls) || Integer.TYPE.equals(cls))
{
- return PropertyConverter.toInteger(value);
+ return toInteger(value);
}
else if (Long.class.equals(cls) || Long.TYPE.equals(cls))
{
- return PropertyConverter.toLong(value);
+ return toLong(value);
}
else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls))
{
- return PropertyConverter.toByte(value);
+ return toByte(value);
}
else if (Short.class.equals(cls) || Short.TYPE.equals(cls))
{
- return PropertyConverter.toShort(value);
+ return toShort(value);
}
else if (Float.class.equals(cls) || Float.TYPE.equals(cls))
{
- return PropertyConverter.toFloat(value);
+ return toFloat(value);
}
else if (Double.class.equals(cls) || Double.TYPE.equals(cls))
{
- return PropertyConverter.toDouble(value);
+ return toDouble(value);
}
else if (BigInteger.class.equals(cls))
{
- return PropertyConverter.toBigInteger(value);
+ return toBigInteger(value);
}
else if (BigDecimal.class.equals(cls))
{
- return PropertyConverter.toBigDecimal(value);
+ return toBigDecimal(value);
}
}
else if (Date.class.equals(cls))
{
- return PropertyConverter.toDate(value, (String) params[0]);
+ return toDate(value, (String) params[0]);
}
else if (Calendar.class.equals(cls))
{
- return PropertyConverter.toCalendar(value, (String) params[0]);
+ return toCalendar(value, (String) params[0]);
}
else if (URL.class.equals(cls))
{
- return PropertyConverter.toURL(value);
+ return toURL(value);
}
else if (Locale.class.equals(cls))
{
- return PropertyConverter.toLocale(value);
+ return toLocale(value);
+ }
+ else if (isEnum(cls))
+ {
+ return toEnum(value, cls);
}
else if (Color.class.equals(cls))
{
- return PropertyConverter.toColor(value);
+ return toColor(value);
}
else if (cls.getName().equals("javax.mail.internet.InternetAddress"))
{
- return PropertyConverter.toInternetAddress(value);
+ return toInternetAddress(value);
}
else if (InetAddress.class.isAssignableFrom(cls))
{
- return PropertyConverter.toInetAddress(value);
+ return toInetAddress(value);
}
throw new ConversionException("The value '" + value + "' (" + value.getClass() + ") can't be converted to a " + cls.getName() + " object");
@@ -683,6 +690,76 @@
else
{
throw new ConversionException("The value " + value + " can't be converted to a InternetAddress");
+ }
+ }
+
+ /**
+ * Calls Class.isEnum() on Java 5, returns false on older JRE.
+ */
+ static boolean isEnum(Class cls)
+ {
+ if (!SystemUtils.isJavaVersionAtLeast(1.5f))
+ {
+ 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(), e);
+ }
+ }
+
+ /**
+ * Convert the specified value into a Java 5 enum.
+ *
+ * @param value the value to convert
+ * @param cls the type of the enumeration
+ * @return the converted value
+ * @throws ConversionException thrown if the value cannot be converted to an enumeration
+ *
+ * @since 1.5
+ */
+ static Object toEnum(Object value, Class cls) throws ConversionException
+ {
+ if (value.getClass().equals(cls))
+ {
+ return value;
+ }
+ else if (value instanceof String)
+ {
+ try
+ {
+ Method valueOfMethod = cls.getMethod("valueOf", new Class[] { String.class });
+ return valueOfMethod.invoke(null, new Object[] { value });
+ }
+ catch (Exception e)
+ {
+ throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
+ }
+ }
+ else if (value instanceof Number)
+ {
+ try
+ {
+ Method valuesMethod = cls.getMethod("values", new Class[] {});
+ Object valuesArray = valuesMethod.invoke(null, new Object[] {});
+
+ return Array.get(valuesArray, ((Number) value).intValue());
+ }
+ catch (Exception e)
+ {
+ throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
+ }
+ }
+ else
+ {
+ throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
}
}
Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java?view=diff&rev=530583&r1=530582&r2=530583
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java Thu Apr 19 15:48:29 2007
@@ -14,11 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.apache.commons.configuration;
+import java.lang.reflect.Method;
import java.math.BigDecimal;
-import java.util.List;
import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.lang.SystemUtils;
import junit.framework.TestCase;
@@ -100,8 +104,7 @@
config.addProperty("target", "lazy dog");
assertEquals("Wrong interpolation",
"The quick brown fox jumps over the lazy dog.",
- PropertyConverter.interpolate(
- "The ${animal} jumps over the ${target}.", config));
+ PropertyConverter.interpolate("The ${animal} jumps over the ${target}.", config));
}
/**
@@ -110,8 +113,7 @@
public void testInterpolateObject()
{
assertEquals("Object was not correctly interpolated", new Integer(42),
- PropertyConverter.interpolate(new Integer(42),
- new PropertiesConfiguration()));
+ PropertyConverter.interpolate(new Integer(42), new PropertiesConfiguration()));
}
/**
@@ -127,8 +129,7 @@
config.addProperty("target_attr", "lazy");
assertEquals("Wrong complex interpolation",
"The quick brown fox jumps over the lazy dog.",
- PropertyConverter.interpolate(
- "The ${animal} jumps over the ${target}.", config));
+ PropertyConverter.interpolate("The ${animal} jumps over the ${target}.", config));
}
/**
@@ -161,9 +162,8 @@
PropertiesConfiguration config = new PropertiesConfiguration();
config.addProperty("animal", "quick brown fox");
assertEquals("Wrong interpolation",
- "The quick brown fox jumps over ${target}.", PropertyConverter
- .interpolate("The ${animal} jumps over ${target}.",
- config));
+ "The quick brown fox jumps over ${target}.",
+ PropertyConverter.interpolate("The ${animal} jumps over ${target}.", config));
}
/**
@@ -173,11 +173,9 @@
public void testToNumberDirect()
{
Integer i = new Integer(42);
- assertSame("Wrong integer", i, PropertyConverter.toNumber(i,
- Integer.class));
+ assertSame("Wrong integer", i, PropertyConverter.toNumber(i, Integer.class));
BigDecimal d = new BigDecimal("3.1415");
- assertSame("Wrong BigDecimal", d, PropertyConverter.toNumber(d,
- Integer.class));
+ assertSame("Wrong BigDecimal", d, PropertyConverter.toNumber(d, Integer.class));
}
/**
@@ -186,10 +184,8 @@
*/
public void testToNumberFromString()
{
- assertEquals("Incorrect Integer value", new Integer(42),
- PropertyConverter.toNumber("42", Integer.class));
- assertEquals("Incorrect Short value", new Short((short) 10),
- PropertyConverter.toNumber(new StringBuffer("10"), Short.class));
+ assertEquals("Incorrect Integer value", new Integer(42), PropertyConverter.toNumber("42", Integer.class));
+ assertEquals("Incorrect Short value", new Short((short) 10), PropertyConverter.toNumber(new StringBuffer("10"), Short.class));
}
/**
@@ -250,6 +246,91 @@
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));
+ }
+
+ public void testToEnumFromString()
+ {
+ if (!SystemUtils.isJavaVersionAtLeast(1.5f))
+ {
+ return;
+ }
+
+ assertEquals(enumObject, PropertyConverter.toEnum("METHOD", enumClass));
+ }
+
+ public void testToEnumFromInvalidString()
+ {
+ if (!SystemUtils.isJavaVersionAtLeast(1.5f))
+ {
+ return;
+ }
+
+ try
+ {
+ assertEquals(enumObject, PropertyConverter.toEnum("FOO", enumClass));
+ fail("Could convert invalid String!");
+ }
+ catch (ConversionException e)
+ {
+ // expected
+ }
+ }
+
+ public void testToEnumFromNumber()
+ {
+ if (!SystemUtils.isJavaVersionAtLeast(1.5f))
+ {
+ return;
+ }
+
+ assertEquals(enumObject, PropertyConverter.toEnum(new Integer(2), enumClass));
+ }
+
+ public void testToEnumFromInvalidNumber()
+ {
+ if (!SystemUtils.isJavaVersionAtLeast(1.5f))
+ {
+ return;
+ }
+
+ try
+ {
+ assertEquals(enumObject, PropertyConverter.toEnum(new Integer(-1), enumClass));
+ fail("Could convert invalid number!");
+ }
+ catch (ConversionException e)
+ {
+ // expected
}
}
}
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=530583&r1=530582&r2=530583
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Thu Apr 19 15:48:29 2007
@@ -28,9 +28,9 @@
configurations (PropertyListConfiguration and XMLPropertyListConfiguration).
</action>
<action dev="ebourg" type="add">
- DataConfiguration now supports the java.net.InetAddress and
- javax.mail.internet.InternetAddress types. Properties are
- converted to these types using the new generic getters.
+ DataConfiguration now supports java.net.InetAddress,
+ javax.mail.internet.InternetAddress, and Java 5 enumeration types.
+ Properties are converted to these types using the new generic getters.
</action>
<action dev="ebourg" type="fix">
The object getters in DataConfiguration with no default value
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org