Adam Lee
Adam Lee

Reputation: 25738

How to list the bean's properties

I have a bean, is there a way to list bean's all properties without list one by one?

Some beans overide the ToString() method which is handy. Howvere the beans which does not override this method?

Upvotes: 7

Views: 12479

Answers (4)

Hakan Serce
Hakan Serce

Reputation: 11256

You can use BeanInfo via BeanIntrospection as follows:

Object o = new MyBean();
try {
    BeanInfo bi = Introspector.getBeanInfo(MyBean.class);
    PropertyDescriptor[] pds = bi.getPropertyDescriptors();
    for (int i=0; i<pds.length; i++) {
        // Get property name
        String propName = pds[i].getName();

        // Get the value of prop1
        Expression expr = new Expression(o, "getProp1", new Object[0]);
        expr.execute();
        String s = (String)expr.getValue();
    }
    // class, prop1, prop2, PROP3
} catch (java.beans.IntrospectionException e) {
}

Or you can go with reflection method using one of the following approaches:

  1. Get all no-parameter getXXX() methods through getDeclaredMethods and traverse them
  2. Get all fields using getDeclaredFields() and traverse them (Not compliant with Bean spec, if you care about it)

Upvotes: 11

Pshemo
Pshemo

Reputation: 124225

You can use reflection. Take declared fields from class, it they are private check if they have setters and getters (remember boolean getter is "isProperty")

Code can look like this:

List<String> properties = new ArrayList<String>();
Class<?> cl = MyBean.class;

// check all declared fields
for (Field field : cl.getDeclaredFields()) {

    // if field is private then look for setters/getters
    if (Modifier.isPrivate(field.getModifiers())) {

        // changing 1st letter to upper case
        String name = field.getName();
        String upperCaseName = name.substring(0, 1).toUpperCase()
                + name.substring(1);
        // and have getter and setter
        try {
            String simpleType = field.getType().getSimpleName();
            //for boolean property methods should be isProperty and setProperty(propertyType)
            if (simpleType.equals("Boolean") || simpleType.equals("boolean")) {
                if ((cl.getDeclaredMethod("is" + upperCaseName) != null)
                        && (cl.getDeclaredMethod("set" + upperCaseName,
                                field.getType()) != null)) {
                    properties.add(name);
                }
            } 
            //for not boolean property methods should be getProperty and setProperty(propertyType)
            else {
                if ((cl.getDeclaredMethod("get" + upperCaseName) != null)
                        && (cl.getDeclaredMethod("set" + upperCaseName,
                                field.getType()) != null)) {
                    properties.add(name);
                }
            }
        } catch (NoSuchMethodException | SecurityException e) {
            // if there is no method nothing bad will happen
        }
    }
}
for (String property:properties)
    System.out.println(property);

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109547

You might be interested in BeanInfo, a class that may accompany a bean class, without that the bean class need to be altered. It is used in many GUI builders to show properties of a bean, but has its non-GUI uses too.

Upvotes: 0

piotrek
piotrek

Reputation: 14530

see apache commons lang - ReflectionToStringBuilder

Upvotes: 4

Related Questions