openpojo icon indicating copy to clipboard operation
openpojo copied to clipboard

Boolean fields that start with a prefix and then "isXXX" will fail.

Open francisconoriega opened this issue 6 years ago • 3 comments

When you have a boolean field that starts with with a prefix, followed by "is", openpojo will fail to find/associate its getter.

e.g.

boolean mIsDefault; will try to find getters called getIsDefault and isIsDefault, but not isDefault

The issue is in PojoMethodFactory.generateGetMethodNames in this part

      prefix.add("is" + AttributeHelper.getAttributeName(field));
      String fieldName = field.getName();
      if (fieldName.length() > 2 && fieldName.startsWith("is") && Character.isUpperCase(fieldName.charAt(2)))
        prefix.add(fieldName);
    }

the first prefix.add will generate isIsDefault, the second prefix.add wont be executed because you are calling fieldName.startsWith instead of AttributeHelper.getAttributeName(field).startsWith.

You should also need add a toLower, making it

String attributeName = AttributeHelper.getAttributeName(field);
   if (attributeName.length() > 2 && attributeName.toLower().startsWith("is") && Character.isUpperCase(attributeName.charAt(2)))
        prefix.add(fieldName);
    }

francisconoriega avatar Aug 02 '17 04:08 francisconoriega

Works for me.

Yky avatar Aug 11 '17 13:08 Yky

@Yky could you give an example of the field name and getter name you are using?

It should not work if you have something like mIsDefault and then a getter called isDefault

and you can see in the generated method names code above, the method names this will produce are:

prefix.add("is" + AttributeHelper.getAttributeName(field)); => isIsDefault

String fieldName = field.getName(); if (fieldName.length() > 2 && fieldName.startsWith("is") && Character.isUpperCase(fieldName.charAt(2))) prefix.add(fieldName);=> mIsDefault

so you get isIsDefault && mIsDefault but not isDefault so you wont get the right method name

francisconoriega avatar Aug 14 '17 20:08 francisconoriega

Why do you have the following line in your sample code.

prefix.add("is" + AttributeHelper.getAttributeName(field));

My tests work without this. The only registered prefix I have is "m".

Yky avatar Aug 17 '17 10:08 Yky