Handling EnumSets in DefaultActionBeanPropertyBinder
----------------------------------------------------

                 Key: STS-830
                 URL: http://www.stripesframework.org/jira/browse/STS-830
             Project: Stripes
          Issue Type: Improvement
            Reporter: David Bitkowski


Hi guys,

I had some problems with the DefaultActionBeanPropertyBinder binding correctly 
with an EnumSet bean.  I traced it and found it was a small modification to 
DefaultActionBeanPropertyBinder.  Here is the additional code that will allow 
for handling EnumSets.  It is just a small modification to bindNonNullValue:

    @SuppressWarnings({ "unchecked", "rawtypes" })
        protected void bindNonNullValue(ActionBean bean,
            PropertyExpressionEvaluation propertyEvaluation, List<Object> 
valueOrValues,
            Class targetType, Class scalarType) throws Exception {
        Class valueType = valueOrValues.iterator().next().getClass();

        // If the target type is an array, set it as one, otherwise set as 
scalar
        if (targetType.isArray() && !valueType.isArray()) {
            Object typedArray = Array.newInstance(scalarType, 
valueOrValues.size());
            for (int i = 0; i < valueOrValues.size(); ++i) {
                Array.set(typedArray, i, valueOrValues.get(i));
            }

            propertyEvaluation.setValue(typedArray);
        }
        else if (Collection.class.isAssignableFrom(targetType)
                && !Collection.class.isAssignableFrom(valueType)) {
            Collection collection = null;
            if (targetType.isInterface()) {
                collection = (Collection) 
ReflectUtil.getInterfaceInstance(targetType);
            }
            else if(EnumSet.class.isAssignableFrom(targetType) && 
Enum.class.isAssignableFrom(scalarType)) {
                collection = EnumSet.noneOf(scalarType.asSubclass(Enum.class));
            }
            else {
                collection = (Collection) targetType.newInstance();
            }

            collection.addAll(valueOrValues);
            propertyEvaluation.setValue(collection);
        }
        else {
            propertyEvaluation.setValue(valueOrValues.get(0));
        }
    }

Cheers, and lovin' Stripes!

-- 
This message is automatically generated by JIRA.
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger. 
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Data protection magic?
Nope - It's vRanger. Get your free trial download today. 
http://p.sf.net/sfu/quest-sfdev2dev
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to