by Eabin 22 Mar 20:11
Improvement for fields starting with "is"

Hi Rob,

I just came across a counter-intuitive situation:

class X {
boolean isRead;

public boolean isRead() {
}

public void setRead(boolean isRead) {
this.isRead = isRead;
}
}

Ebean will look for getIsRead() and isIsRead(), but not find isRead(). i don't know what the bean-spec says about this, but IntelliJ auto-creates the getter as isRead().

23 Mar 00:21
by Rob

I believe the bean spec suggests the field should be "read" rather than "isRead" ... but practically this is a common enough occurrence that Ebean should handle it.

So yes, I'll log this as a bug.

Thanks, Rob.

23 Mar 09:22
by Rob

Logged as http://www.avaje.org/bugdetail-93.html

Fixed in HEAD.

24 Mar 10:53
by imario

Hmm ...

The fix you committed trims off the "is" always, but I have quite a lot of Shorts using "is" as name prefix.
These fields have the name stripped now too.

The methods also are named getIsX() and setIsX(), so, technically spoken, my fields and methods follow the Bean-Spec.

I'd vote to enhance the patch to check against which methods are existent and strip based on this information, or (I think even better) rollback this change, as the developer should fix their entities if they do not follow the spec.

However, at least I'd like to ask if you could check for boolean explicitely:

Index: ../ebean/src/com/avaje/ebean/enhance/agent/FieldMeta.java
===================================================================
--- ../ebean/src/com/avaje/ebean/enhance/agent/FieldMeta.java (revision 178)
+++ ../ebean/src/com/avaje/ebean/enhance/agent/FieldMeta.java Tue Mar 24 11:44:46 CET 2009
@@ -89,7 +89,7 @@
publicGetterName = name;

} else {
- String publicFieldName = getFieldName(name);
+ String publicFieldName = getFieldName(name, asmType);
// use java bean property name convention
String initCap = Character.toUpperCase(publicFieldName.charAt(0))+publicFieldName.substring(1);
publicSetterName = "set"+initCap;
@@ -110,8 +110,8 @@
/**
* Handle the case where a boolean variable starts with 'is'.
*/
- private String getFieldName(String name){
- if (name.startsWith("is") && name.length() > 2){
+ private String getFieldName(String name, Type asmType){
+ if (asmType.equals(Type.BOOLEAN_TYPE) && name.startsWith("is") && name.length() > 2){
char c = name.charAt(2);
if (Character.isUpperCase(c)){
if (classMeta.isLog(6)) {
Index: ../ebean/src/com/avaje/ebean/server/deploy/parse/CreateProperties.java
===================================================================
--- ../ebean/src/com/avaje/ebean/server/deploy/parse/CreateProperties.java (revision 178)
+++ ../ebean/src/com/avaje/ebean/server/deploy/parse/CreateProperties.java Tue Mar 24 11:44:46 CET 2009
@@ -188,7 +187,7 @@

private String getFieldName(Field field, Class beanType){
String name = field.getName();
- if (name.startsWith("is") && name.length() > 2){
+ if ((Boolean.class.equals(field.getType()) || boolean.class.equals(field.getType())) && name.startsWith("is") && name.length() > 2){
char c = name.charAt(2);
if (Character.isUpperCase(c)){
String msg = "trimming off 'is' from field name "+name+" in class "+beanType.getName();


Thanks!
Ciao,
Mario

24 Mar 12:27
by Rob

Very good point.

I'll incorporate the changes to include checking for the boolean or Boolean data type.

Thanks, Rob.

Create a New Topic

Title:
Body:
 
Introduction User Guide (pdf) Install/Configure Public JavaDoc Whitepapers
General Database Specific Byte Code Deployment Annotations Features
Top Bugs Top Enhancements
woResponse