15

I have method which gets a POJO as it's parameter. Now I want to programmatically get all the attributes of the POJO (because my code may not know what are all the attributes in it at run time) and need to get the values for the attributes also. Finally I'll form a string representation of the POJO.

I could use ToStringBuilder, but I want build my output string in certain format specific to my requirement.

Is it possible to do so in Beanutils !? If yes, any pointers to the method name? If no, should I write my own reflection code?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Veera
  • 32,532
  • 36
  • 98
  • 137
  • You can use ToStringStyle, it is having predifined styles DEFAULT_STYLE: The default toString style. MULTI_LINE_STYLE:The multi line toString style. NO_FIELD_NAMES_STYLE:The no field names toString style. SHORT_PREFIX_STYLE:The short prefix toString style. SIMPLE_STYLE:The simple toString style. See if any of these can solve your purpose – Rakesh Juyal Jun 24 '09 at 13:38

3 Answers3

18

I know this is a year old question, but I think it can be useful for others.

I have found a partial solution using this LOC

Field [] attributes =  MyBeanClass.class.getDeclaredFields();

Here is a working example:

import java.lang.reflect.Field;

import org.apache.commons.beanutils.PropertyUtils;

public class ObjectWithSomeProperties {

    private String firstProperty;

    private String secondProperty;


    public String getFirstProperty() {
        return firstProperty;
    }

    public void setFirstProperty(String firstProperty) {
        this.firstProperty = firstProperty;
    }

    public String getSecondProperty() {
        return secondProperty;
    }

    public void setSecondProperty(String secondProperty) {
        this.secondProperty = secondProperty;
    }

    public static void main(String[] args) {

        ObjectWithSomeProperties object = new ObjectWithSomeProperties();

        // Load all fields in the class (private included)
        Field [] attributes =  object.getClass().getDeclaredFields();

        for (Field field : attributes) {
            // Dynamically read Attribute Name
            System.out.println("ATTRIBUTE NAME: " + field.getName());

            try {
                // Dynamically set Attribute Value
                PropertyUtils.setSimpleProperty(object, field.getName(), "A VALUE");
                System.out.println("ATTRIBUTE VALUE: " + PropertyUtils.getSimpleProperty(object, field.getName()));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
fthiella
  • 48,073
  • 15
  • 90
  • 106
Jorge Palacio
  • 345
  • 2
  • 5
12

Have you tried ReflectionToStringBuilder? It looks like is should do what you describe.

John Meagher
  • 22,808
  • 14
  • 54
  • 57
  • yes. it gives me the string representation of my POJO. but, i want the string format to be in different style. For example, instead of having com.demo.MyPojo@a33425[name=foo,desc=bar], I want the string to be "[name=foo;desc=bar;]" – Veera Jun 24 '09 at 13:40
  • Perhaps this version: http://commons.apache.org/lang/api-release/org/apache/commons/lang/builder/ReflectionToStringBuilder.html#toString(java.lang.Object,%20org.apache.commons.lang.builder.ToStringStyle) – John Meagher Jun 24 '09 at 15:47
  • Hmm, the link above didn't copy correctly. There is a version that takes the object and a ToStringStyle that should allow you to customize the output. – John Meagher Jun 24 '09 at 15:48
3

get all properties/variables ( just the name ) using reflection. Now use getProperty method to get the value of that variable

Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214