diff --git a/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java b/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java index 9fe1db9d6..ec782bd69 100644 --- a/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java +++ b/src/main/org/apache/tools/ant/types/EnumeratedAttribute.java @@ -54,6 +54,32 @@ public abstract class EnumeratedAttribute { } /** + * Factory method for instantiating EAs via API in a more + * developer friendly way. + * @param clazz Class, extending EA, which to instantiate + * @param value The value to set on that EA + * @return Configured EA + * @throws BuildException If the class could not be found or the value + * is not valid for the given EA-class. + * @see Bug-14831 + */ + public static EnumeratedAttribute getInstance( + Class/**/ clazz, + String value) throws BuildException { + if (!EnumeratedAttribute.class.isAssignableFrom(clazz)) { + throw new BuildException("You have to provide a subclass from EnumeratedAttribut as clazz-parameter."); + } + EnumeratedAttribute ea = null; + try { + ea = (EnumeratedAttribute)clazz.newInstance(); + } catch (Exception e) { + throw new BuildException(e); + } + ea.setValue(value); + return ea; + } + + /** * Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}. * @param value the String value of the attribute * @throws BuildException if the value is not valid for the attribute @@ -111,7 +137,6 @@ public abstract class EnumeratedAttribute { return index; } - /** * Convert the value to its string form. * @@ -121,4 +146,4 @@ public abstract class EnumeratedAttribute { return getValue(); } -} +} \ No newline at end of file diff --git a/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java b/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java index 246686a4b..ec1ff6c08 100644 --- a/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/EnumeratedAttributeTest.java @@ -24,9 +24,7 @@ import org.apache.tools.ant.BuildException; /** * JUnit 3 testcases for org.apache.tools.ant.EnumeratedAttribute. - * */ - public class EnumeratedAttributeTest extends TestCase { private static String[] expected = {"a", "b", "c"}; @@ -49,7 +47,22 @@ public class EnumeratedAttributeTest extends TestCase { !(new TestNull()).containsValue("d")); } - public void testExceptions() { + public void testFactory() { + EnumeratedAttributeTest_Factory ea1 = (EnumeratedAttributeTest_Factory)EnumeratedAttribute.getInstance( + EnumeratedAttributeTest_Factory.class, + "one"); + assertEquals("Factory didnt set the right value.", ea1.getValue(), "one"); + try { + EnumeratedAttributeTest_Factory ea2 = (EnumeratedAttributeTest_Factory)EnumeratedAttribute.getInstance( + EnumeratedAttributeTest_Factory.class, + "illegal"); + fail("Factory should fail when trying to set an illegal value."); + } catch (BuildException be) { + // was expected + } + } + + public void testExceptions() { EnumeratedAttribute t1 = new TestNormal(); for (int i=0; i