javadoc specifically says that value will be converted to a string value. quoting BeanUtils' javadoc "Return the value of the specified property of the specified bean, no matter which property reference format is used, as a String. " if you want the object you can use PropertyUtils instead. here is what the javadoc says: "Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions"
Your code will look like this: Object value = *PropertyUtils*.getProperty(tester, "type"); On Mon, Oct 15, 2012 at 4:28 PM, Corey Sanders <[email protected]>wrote: > I am trying to retrieve an enum property from a class using > BeanUtils.getProperty(object, propertyName) and I'm not seeing the behavior > I expect. Instead of returning the property as an instance of the enum, > BeanUtils is returning it as a String. > > I searched around and I see a lot of examples of people trying to do the > reverse, set an enum property using the string value, but no one with my > problem. What is the expected behavior here and is there a way to get the > value as an enum instead of a String? > > package test; > > import org.apache.commons.beanutils.BeanUtils; > > public class Tester > { > public static enum Type { > ON, > OFF > } > > private Type type = Type.ON; > > public Type getType() { > return this.type; > } > > public void setType(Type type) { > this.type = type; > } > > public static void main(String [] args) > throws Exception > { > Tester tester = new Tester(); > Object value = BeanUtils.getProperty(tester, "type"); > System.out.println("The type property is an instanceof " + > value.getClass().getName() + " with value " + value.toString()); > } > } > > $ java -classpath > classes:commons-beanutils-1.8.3.jar:commons-logging-1.1.1.jar test.Tester > The type property is an instanceof java.lang.String with value ON > > > -- Corey >
