javaapache-commonsapache-commons-langapache-commons-lang3

Output all of the values for variables in an object using reflection


I would like to output all of the values for variables in an object.

Currently I am using ReflectionToStringBuilder but the problem is that it includes the [,] characters when outputting collections.

Here is an example.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Test 
{
    public int x = 10;

    public int y = 20;

    public String example = "This is some text, with a comma";

    public Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

    public static void main(String args[])
    {
        System.out.println(ReflectionToStringBuilder.toString(new Test(),
                ToStringStyle.NO_FIELD_NAMES_STYLE));
    }
}

Output

Test@efb78af[10,20,This is some text, with a comma,[1, 2, 3, 4, 5]]

I've tried defining my own ToStringStyle but it seems as though there aren't any options to remove the square brackets and commas.

import org.apache.commons.lang3.builder.ToStringStyle;

public class ValueOnlyToStringStyle extends ToStringStyle
{
    public static final ToStringStyle VALUE_ONLY = new ValueOnlyToStringStyle();

    private static final long serialVersionUID = 1L;

    private ValueOnlyToStringStyle() 
    {
        super();
        this.setContentStart("");
        this.setFieldSeparator("  ");
        this.setFieldSeparatorAtStart(true);
        this.setContentEnd("");
        this.setUseClassName(false);
        this.setUseFieldNames(false);
        this.setArrayContentDetail(true);
        this.setArrayStart(" ");
        this.setArrayEnd(" ");
        this.setArraySeparator(" ");
        this.setDefaultFullDetail(true);
        this.setNullText("");
        this.setSizeStartText("");
        this.setSizeStartText("");
        this.setFieldNameValueSeparator(" ");
        this.setUseShortClassName(false);
        this.setUseIdentityHashCode(false);
        this.setSummaryObjectStartText(" ");
        this.setSummaryObjectEndText(" ");

    }
}

Output

10  20  This is some text, with a comma  [1, 2, 3, 4, 5]

What I need is to only get the values with no added characters.

10  20  This is some text, with a comma 1 2 3 4 5

How could this be achieved?


Solution

  • I have tried the code on my machine, it should work on your example.

    class MyToStringStyle extends ToStringStyle {
        public MyToStringStyle() {
            setFieldSeparator(" ");
            setUseFieldNames(false);
            setUseIdentityHashCode(false);
            setUseClassName(false);
            setContentStart("");
            setContentEnd("");
        }
    
        @Override
        protected void appendDetail(StringBuffer buffer, String fieldName, Collection<?> coll) {
            if (coll.isEmpty()) return;
            Iterator iter = coll.iterator();
            buffer.append(iter.next());
            while (iter.hasNext()) {
                buffer.append(" ").append(iter.next());
            }
        }
    }