• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

bean style property/attribute mapping

Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a need to convert raw strings to what the javabean equivalent
would be if the string was the name of an instance variable.

For example:

The conversion should be as follows:

_this_property -> thisProperty
this_property -> thisProperty
This_property -> thisProperty
THIS_PROPERTY -> thisProperty
THISPROPERTY -> thisproperty
this_other_property -> thisOtherProperty
this_property_x -> thisPropertyX
this_property_ -> thisProperty

As I understand it, this is the way java bean spec would map these
entries. Is there a way I can leverage bean utils or some standard
package to do the conversion? what would you recommend?
Posts: 4179
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't heard of, and don't see in the JavaBeans Spec anything concerning standards or conversions for property names. The only real conventions used are names of methods for setting and accessing properties. That standard is:
Accessor: PropertyType get<Property Name>()
Setter: void set<Property Name>(PropertyType)

<Property Name> would be the name of the property with the first character in the name converted to upper case. There are no conventions on how the property should be named outside of normal Java conventions (typically the first character is lower case, and otherwise in camel case to make the words readable). These aren't requirements though.

So the JavaBeans spec would just be to define the methods:

Converting from a String that represents a property name to one that represents an accessor method is not hard, just toUpperCase the first character in the property name, then prepend get (thisProperty -> getThisProperty). Same goes with the setter method.

But to convert from some random String and turn it into a name that follows recommended Java conventions is not easy at all (THISPROPERTY -> thisProperty). It probably requires a dictionary search to split the String into real words. I don't know of a library that does this, but you can play with some spell checkers to see if they would make the task easier.
You may have just won ten million dollars! Or, maybe a tiny ad.
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic