Skip to content

A boolean property should be able to have two getters: isX() and getX() #906

@admxiii

Description

@admxiii

Groovy object properties follow the javabean spec for boolean, which allows for both a get* and an is* method to appear on the same object. Mybatis complains this doesn't follow the javabean spec which isn't entirely true. I know you can use Boolean to get around the get/is issue or define your own getter which stops groovy from building the pair, but the strange part is that this field is not even used by mybatis, its just a helper boolean internal to the object, but mybatis still validates the entire object whether the field is used or not in a resultmap.

could this rule be relaxed to allow the official javabean spec to not throw an error?

from oracle:

8.3.2 Boolean properties
In addition, for boolean properties, we allow a getter method to match the pattern:
public boolean is();
This “is” method may be provided instead of a “get” method,
or it may be provided in addition to a “get” method.
In either case, if the “is” method is present for a boolean property then we will
use the “is” method to read the property value.

heres the code in question that throws the error on a valid javabean.

private void resolveGetterConflicts(Map<String, List<Method>> conflictingGetters) {
    for (String propName : conflictingGetters.keySet()) {
      List<Method> getters = conflictingGetters.get(propName);
      Iterator<Method> iterator = getters.iterator();
      Method firstMethod = iterator.next();
      if (getters.size() == 1) {
        addGetMethod(propName, firstMethod);
      } else {
        Method getter = firstMethod;
        Class<?> getterType = firstMethod.getReturnType();
        while (iterator.hasNext()) {
          Method method = iterator.next();
          Class<?> methodType = method.getReturnType();
          if (methodType.equals(getterType)) {
            throw new ReflectionException("Illegal overloaded getter method with ambiguous type for property "
                + propName + " in class " + firstMethod.getDeclaringClass()
                + ".  This breaks the JavaBeans " + "specification and can cause unpredictable results.");
          } else if (methodType.isAssignableFrom(getterType)) {
            // OK getter type is descendant
          } else if (getterType.isAssignableFrom(methodType)) {
            getter = method;
            getterType = methodType;
          } else {
            throw new ReflectionException("Illegal overloaded getter method with ambiguous type for property "
                + propName + " in class " + firstMethod.getDeclaringClass()
                + ".  This breaks the JavaBeans " + "specification and can cause unpredictable results.");
          }
        }
        addGetMethod(propName, getter);
      }
    }
  }

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions