commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Corey Sanders <corey.thecolo...@gmail.com>
Subject [beanutils] Retrieving enum property from class
Date Mon, 15 Oct 2012 19:28:20 GMT
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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message