Commits

mrdon  committed d509468

Added support for enums in the type conversion, fixed setting of flag for
allowing access to static methods
XW-564

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1599e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits fc3cee5

Comments (0)

Files changed (5)

File src/java/com/opensymphony/xwork2/conversion/impl/DefaultTypeConverter.java

                     result = bigDecValue(value);
                 if (toType == String.class)
                     result = stringValue(value);
+                if (Enum.class.isAssignableFrom(toType))
+                    result = enumValue((Class<Enum>)toType, value);
             }
         } else {
             if (toType.isPrimitive()) {
             return ((Number) value).doubleValue() != 0;
         return true; // non-null
     }
+    
+    public Enum<?> enumValue(Class toClass, Object o) {
+        Enum<?> result = null;
+        if (o == null) {
+            result = null;
+        } else if (o instanceof String[]) {
+            result = Enum.valueOf(toClass, ((String[]) o)[0]);
+        } else if (o instanceof String) {
+            result = Enum.valueOf(toClass, (String) o);
+        }
+        return result;
+    }
 
     /**
      * Evaluates the given object as a long integer.

File src/java/com/opensymphony/xwork2/conversion/impl/XWorkConverter.java

     }
 
     /**
-     * not used
-     */
-    private Object acceptableErrorValue(Class toClass) {
-        if (!toClass.isPrimitive()) {
-            return null;
-        }
-        
-        if (toClass == int.class) {
-            return new Integer(0);
-        } else if (toClass == double.class) {
-            return new Double(0);
-        } else if (toClass == long.class) {
-            return new Long(0);
-        } else if (toClass == boolean.class) {
-            return Boolean.FALSE;
-        } else if (toClass == short.class) {
-            return new Short((short) 0);
-        } else if (toClass == float.class) {
-            return new Float(0);
-        } else if (toClass == byte.class) {
-            return new Byte((byte) 0);
-        } else if (toClass == char.class) {
-            return new Character((char) 0);
-        }
-        
-        return null;
-    }
-
-    /**
      * Looks for converter mappings for the specified class and adds it to an existing map.  Only new converters are
      * added.  If a converter is defined on a key that already exists, the converter is ignored.
      *

File src/java/com/opensymphony/xwork2/util/OgnlValueStack.java

     
     @Inject("devMode")
     public static void setDevMode(String mode) {
-        devMode = "true".equals(mode);
+        devMode = "true".equalsIgnoreCase(mode);
     }
 
     @Inject(value="allowStaticMethodAccess", required=false)
-    public static void setAllowStaticMethodAccess(boolean allowStaticMethodAccess) {
-        OgnlValueStack.allowStaticMethodAccess = allowStaticMethodAccess;
+    public static void setAllowStaticMethodAccess(String allowStaticMethodAccess) {
+        OgnlValueStack.allowStaticMethodAccess = "true".equalsIgnoreCase(allowStaticMethodAccess);
     }
 
     /* (non-Javadoc)

File src/test/com/opensymphony/xwork2/conversion/impl/XWorkConverterTest.java

 import com.opensymphony.xwork2.util.Bar;
 import com.opensymphony.xwork2.util.Cat;
 import com.opensymphony.xwork2.util.Foo;
+import com.opensymphony.xwork2.util.FurColor;
 import com.opensymphony.xwork2.util.InstantiatingNullHandler;
 import com.opensymphony.xwork2.util.OgnlValueStack;
 import com.opensymphony.xwork2.util.ValueStack;
         assertEquals(new Double(1234.12), converter.convertValue(context, null, null, null, "1.234,12", Double.class));
 
     }
+    
+    public void testStringToEnum() {
+        assertEquals(FurColor.BLACK, converter.convertValue(context, null, null, null, "BLACK", FurColor.class));
+        assertEquals(OgnlRuntime.NoConversionPossible, converter.convertValue(context, null, null, null, "black", FurColor.class));
+        assertEquals(OgnlRuntime.NoConversionPossible, converter.convertValue(context, null, null, null, "red", FurColor.class));
+    }
 
     // Testing for null result on non-primitive Number types supplied as empty String or 
     public void testNotPrimitiveDefaultsToNull() {

File src/test/com/opensymphony/xwork2/util/OgnlValueStackTest.java

     }
     
     public void testStaticMethodDisallow() {
-        OgnlValueStack.setAllowStaticMethodAccess(false);
+        OgnlValueStack.setAllowStaticMethodAccess("false");
         try {
             OgnlValueStack vs = new OgnlValueStack();
     
             vs.push(dog);
             assertNull(vs.findValue("@com.opensymphony.xwork2.util.Dog@getDeity()", String.class));
         } finally {
-            OgnlValueStack.setAllowStaticMethodAccess(true);
+            OgnlValueStack.setAllowStaticMethodAccess("true");
         }
     }