Skip to content

BeanWrapper should allow reading of Boolean properties accessible via 'is' methods [SPR-9482] #14117

@spring-projects-issues

Description

@spring-projects-issues

Fried Hoeben opened SPR-9482 and commented

When accessing bean properties via a BeanWrapper Boolean properties whose getter method uses an 'is' prefix rather than a 'get' prefix are not seen as readable.
If the type of the property is boolean (i.e. primitive) it is readable.

Please also allow reading of Boolean properties using the 'is' prefix.

As a workaround we created our own getValue() implementation, but that is not really what we would like:

    private static Object getValue(BeanWrapper wrapper, String propertyName) {
        Object result = null;
        if (wrapper.isReadableProperty(propertyName)) {
            result = wrapper.getPropertyValue(propertyName);
        } else {
            PropertyDescriptor propertyDescriptor = wrapper.getPropertyDescriptor(propertyName);
            Class<?> propertyType = propertyDescriptor.getPropertyType();
            if (Boolean.class.equals(propertyType)) {
                String name = StringUtils.capitalize(propertyName);
                Object expected = wrapper.getWrappedInstance();
                Method m = ReflectionUtils.findMethod(expected.getClass(), "is" + name);
                if (m != null && m.getReturnType().equals(Boolean.class)) {
                    result = ReflectionUtils.invokeMethod(m, expected);
                } else {
                    throw new IllegalArgumentException(createErrorMsg(wrapper, propertyName));
                }
            } else {
                throw new IllegalArgumentException(createErrorMsg(wrapper, propertyName));
            }
        }
        return result;
    }

    private static String createErrorMsg(BeanWrapper wrapper, String propertyName) {
        return propertyName + " can not be read on: " + wrapper.getWrappedClass();
    }

Affects: 3.1.1

Attachments:

1 votes, 1 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning process

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions