openpojo
openpojo copied to clipboard
Boolean fields that start with a prefix and then "isXXX" will fail.
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);
}
Works for me.
@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
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".