Example usage for org.springframework.core.convert Property getWriteMethod

List of usage examples for org.springframework.core.convert Property getWriteMethod

Introduction

In this page you can find the example usage for org.springframework.core.convert Property getWriteMethod.

Prototype

@Nullable
public Method getWriteMethod() 

Source Link

Document

The property setter method: e.g.

Usage

From source file:de.escalon.hypermedia.action.ActionInputParameter.java

public Object[] getPossibleValues(Property property, ActionDescriptor actionDescriptor) {
    // TODO remove code duplication of getPossibleValues
    try {/* ww w.  j a v a 2  s . c o  m*/
        Class<?> parameterType = property.getType();
        Object[] possibleValues;
        Class<?> nested;
        if (Enum[].class.isAssignableFrom(parameterType)) {
            possibleValues = parameterType.getComponentType().getEnumConstants();
        } else if (Enum.class.isAssignableFrom(parameterType)) {
            possibleValues = parameterType.getEnumConstants();
        } else if (Collection.class.isAssignableFrom(parameterType)
                && Enum.class.isAssignableFrom(nested = TypeDescriptor.nested(property, 1).getType())) {
            possibleValues = nested.getEnumConstants();
        } else {
            Annotation[][] parameterAnnotations = property.getWriteMethod().getParameterAnnotations();
            // setter has exactly one param
            Select select = getSelectAnnotationFromFirstParam(parameterAnnotations[0]);
            if (select != null) {
                Class<? extends Options> optionsClass = select.options();
                Options options = optionsClass.newInstance();
                // collect call values to pass to options.get
                List<Object> from = new ArrayList<Object>();
                for (String paramName : select.args()) {
                    ActionInputParameter parameterValue = actionDescriptor.getActionInputParameter(paramName);
                    if (parameterValue != null) {
                        from.add(parameterValue.getCallValue());
                    }
                }

                Object[] args = from.toArray();
                possibleValues = options.get(select.value(), args);
            } else {
                possibleValues = new Object[0];
            }
        }
        return possibleValues;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}