Commons BeanUtils
  1. Commons BeanUtils
  2. BEANUTILS-106

Problem with BeanUtils.setProperty when the name of property of Bean have on second caracter a uppercase

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Blocker Blocker
    • Resolution: Won't Fix
    • Affects Version/s: Nightly Builds
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: Windows XP
      Platform: PC

      Description

      I have a bean with one property hEmail. I fact the Bean have these methods
      setHEmail(String hEmail) and getHEmail().

      There is a problem with these instruction :

      BeanUtils.setProperty(bean, "hEmail", new String("dupond"));
      or
      BeanUtils.copyProperty(bean, "hEmail", new String("dupond"));

      The property hEmail does not contain "dupond" after this instruction.

      If I check my property name with the command :

      String name = new
      PropertyUtilsBean().getPropertyDescriptors(mainBean)[0].getName();

      The result of name is HEmail (not hEmail)

      Info : if I change the property of bean hEmail by hemail there is also no problem.

      I fact I think there is a problem with the command
      PropertyUtilsBean().getPropertyDescriptors(mainBean), that don't return
      correctly the property of the bean when the name of property have on second
      character a uppercase.

      Version JVM : 1.4.2-b18 or 1.4.1_02-b06
      Version BeanUtils (Nightly Builds) v1.6.1 2003-10-01

      Best regards

        Activity

        Hide
        Arun Mammen Thomas added a comment -

        Just FYI,

        The getPropertyDescriptors method you describe simply delegates much of it's
        job to the java.beans.Introspector. (This bug, in particular, describes an
        issue which exists in the output from the Introspector.) Further, the
        approach of BeanUtils to this point in time has been to avoid rebuilding or
        redoing anything currently found in Introspector - this means the bad
        (Introspector bugs) comes with the good.

        My guess will be that this issue should and will be marked "WONTFIX", though
        I'll leave it to those actively working with BeanUtils to make the final
        decision. The easiest workaround, of course, is to choose a slightly more
        verbose name

        Show
        Arun Mammen Thomas added a comment - Just FYI, The getPropertyDescriptors method you describe simply delegates much of it's job to the java.beans.Introspector. (This bug, in particular, describes an issue which exists in the output from the Introspector.) Further, the approach of BeanUtils to this point in time has been to avoid rebuilding or redoing anything currently found in Introspector - this means the bad (Introspector bugs) comes with the good. My guess will be that this issue should and will be marked "WONTFIX", though I'll leave it to those actively working with BeanUtils to make the final decision. The easiest workaround, of course, is to choose a slightly more verbose name
        Hide
        Yasushi Inoue added a comment -

        The capitalization rule of PropertyDescriptor#getName() is as follows:

        If the leading two characters following set(get) prefix are capitalized,
        the leading character of a property name is also capitalized.

        Otherwise, the leading character of a property name is not capitalized.

        examples:

        set(get)AA --> AA (aA is expected)
        set(get)NAmount --> NAmount (nAmount is expected)
        set(get)Aa -> aa
        set(get)VcName -> vcName

        I don't know whether this rule is designated for acronyms or not. Method names
        conforming to Java's naming rule would not encounter this bug. But, if database
        people prefer to another naming rule that needs type prefix preceding nouns,
        workarounds are required.

        This is my workaround. See commented lines:

        import java.beans.BeanInfo;
        import java.beans.Introspector;
        import java.beans.PropertyDescriptor;
        public class TestCapitalization {

        private String aA;
        private String aa;
        private String nAmount;
        private String vcName;

        public String getAA()

        {return aA;}

        public String getAa()

        {return aa;}

        public String getNAmount()

        {return nAmount;}

        public String getVcName()

        {return vcName;}

        public void setAA(String aA)

        {this.aA = aA;}

        public void setAa(String aa)

        {this.aa = aa;}

        public void setNAmount(String nAmount)

        {this.nAmount = nAmount;}

        public void setVcName(String vcName)

        {this.vcName = vcName;}

        /*
        workaround: The leading character not to be capitalized.

        public String getaA()

        {getAA();}

        public void setaA(String aA)

        {setAA(aA);}

        */

        public static void main(String[] args) throws Exception {
        BeanInfo beanInfo = Introspector.getBeanInfo(TestCapitalization.class);
        PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
        for (int i=0; i < props.length; ++i)

        { System.out.println(props[i].getName()); }

        }

        }

        Show
        Yasushi Inoue added a comment - The capitalization rule of PropertyDescriptor#getName() is as follows: If the leading two characters following set(get) prefix are capitalized, the leading character of a property name is also capitalized. Otherwise, the leading character of a property name is not capitalized. examples: set(get)AA --> AA (aA is expected) set(get)NAmount --> NAmount (nAmount is expected) set(get)Aa -> aa set(get)VcName -> vcName I don't know whether this rule is designated for acronyms or not. Method names conforming to Java's naming rule would not encounter this bug. But, if database people prefer to another naming rule that needs type prefix preceding nouns, workarounds are required. This is my workaround. See commented lines: import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; public class TestCapitalization { private String aA; private String aa; private String nAmount; private String vcName; public String getAA() {return aA;} public String getAa() {return aa;} public String getNAmount() {return nAmount;} public String getVcName() {return vcName;} public void setAA(String aA) {this.aA = aA;} public void setAa(String aa) {this.aa = aa;} public void setNAmount(String nAmount) {this.nAmount = nAmount;} public void setVcName(String vcName) {this.vcName = vcName;} /* workaround: The leading character not to be capitalized. public String getaA() {getAA();} public void setaA(String aA) {setAA(aA);} */ public static void main(String[] args) throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(TestCapitalization.class); PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); for (int i=0; i < props.length; ++i) { System.out.println(props[i].getName()); } } }
        Hide
        Craig McClanahan added a comment -

        BeanUtils simply uses the standard introspection functionality of the underlying
        JDK. It does not make it's own determination of what is a property or not, nor
        can it influence the behavior of the underlying Introspector class.

        The workaround is for your class to follow the property name design patterns
        described in the JavaBeans Specification if you want BeanUtils, or any other
        software that deals with JavaBeans properties, to find them.

        Show
        Craig McClanahan added a comment - BeanUtils simply uses the standard introspection functionality of the underlying JDK. It does not make it's own determination of what is a property or not, nor can it influence the behavior of the underlying Introspector class. The workaround is for your class to follow the property name design patterns described in the JavaBeans Specification if you want BeanUtils, or any other software that deals with JavaBeans properties, to find them.
        Hide
        Pawel Kozlowski added a comment -

        I believe that this issue could (and should) be fixed. The standard JDK provides methods for dealing with property names as described in this issue. Please have a look at: http://java.sun.com/j2se/1.4.2/docs/api/java/beans/Introspector.html#decapitalize%28java.lang.String%29

        I believe that the problem is in the and more specifically in this part of the code:

        PropertyDescriptor[] descriptors = getPropertyDescriptors(bean);
        if (descriptors != null) {

        for (int i = 0; i < descriptors.length; i++) {
        if (name.equals(descriptors[i].getName()))

        { return (descriptors[i]); }

        }
        }

        That should be changed to use decapitalize method, so:

        name.equals(Introspector.html#decapitalize(descriptors[i].getName()))

        Could you please re-consider opening / fixing this issue?

        Show
        Pawel Kozlowski added a comment - I believe that this issue could (and should) be fixed. The standard JDK provides methods for dealing with property names as described in this issue. Please have a look at: http://java.sun.com/j2se/1.4.2/docs/api/java/beans/Introspector.html#decapitalize%28java.lang.String%29 I believe that the problem is in the and more specifically in this part of the code: PropertyDescriptor[] descriptors = getPropertyDescriptors(bean); if (descriptors != null) { for (int i = 0; i < descriptors.length; i++) { if (name.equals(descriptors [i] .getName())) { return (descriptors[i]); } } } That should be changed to use decapitalize method, so: name.equals(Introspector.html#decapitalize(descriptors [i] .getName())) Could you please re-consider opening / fixing this issue?
        Hide
        Niall Pemberton added a comment -

        @Pawel

        Craig pointed out that BeanUtils is following the JavaBean specification wrt to property names and I see nothing in your post that persuades me to change that decision. If we implemented your suggestion then anyone who had use the correct name in this circumstance would no longer work

        Show
        Niall Pemberton added a comment - @Pawel Craig pointed out that BeanUtils is following the JavaBean specification wrt to property names and I see nothing in your post that persuades me to change that decision. If we implemented your suggestion then anyone who had use the correct name in this circumstance would no longer work

          People

          • Assignee:
            Unassigned
            Reporter:
            Dutrieux Olivier
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development