使用反射输出对象中变量的所有值

我想输出对象中变量的所有值。


目前我正在使用,ReflectionToStringBuilder但问题是它[,]在输出集合时包含字符。


这是一个例子。


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));

    }

}

输出


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

我试过定义我自己的,ToStringStyle但似乎没有任何选项可以删除方括号和逗号。


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(" ");


    }

}

输出


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

我需要的是只获取没有添加字符的值。


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

这怎么可能实现?


PIPIONE
浏览 222回答 2
2回答

墨色风雨

我已经在我的机器上尝试过代码,它应该适用于你的例子。class MyToStringStyle extends ToStringStyle {&nbsp; &nbsp; public MyToStringStyle() {&nbsp; &nbsp; &nbsp; &nbsp; setFieldSeparator(" ");&nbsp; &nbsp; &nbsp; &nbsp; setUseFieldNames(false);&nbsp; &nbsp; &nbsp; &nbsp; setUseIdentityHashCode(false);&nbsp; &nbsp; &nbsp; &nbsp; setUseClassName(false);&nbsp; &nbsp; &nbsp; &nbsp; setContentStart("");&nbsp; &nbsp; &nbsp; &nbsp; setContentEnd("");&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void appendDetail(StringBuffer buffer, String fieldName, Collection<?> coll) {&nbsp; &nbsp; &nbsp; &nbsp; if (coll.isEmpty()) return;&nbsp; &nbsp; &nbsp; &nbsp; Iterator iter = coll.iterator();&nbsp; &nbsp; &nbsp; &nbsp; buffer.append(iter.next());&nbsp; &nbsp; &nbsp; &nbsp; while (iter.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer.append(" ").append(iter.next());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕丝7291255

我决定编辑代码,因为我已经构建了一个自定义的 ReflectionToStringBuilder 方法。这是方法:public static String toString(Object object) {&nbsp; &nbsp; Class<?> clazz = object.getClass();&nbsp; &nbsp; Field[] fields = clazz.getDeclaredFields();&nbsp; &nbsp; StringBuilder stringBuilder = new StringBuilder();&nbsp; &nbsp; for (Field field : fields) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object value = field.get(object);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // check if the value is actually a list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (List.class.isAssignableFrom(value.getClass())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this for some reason gives the unchecked cast warning, but we actually are&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // checking it so don't worry!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Object> list = (List<Object>) value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Object element : list) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(element.toString()).append(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (value.getClass().isArray()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object[] array = (Object[]) value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Object element : array) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(element.toString()).append(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(value.toString()).append(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (IllegalArgumentException | IllegalAccessException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return stringBuilder.toString();}我已经对其进行了测试,并且效果很好。我的课:int x = 0;String string = "string0";List<String> stringList = Arrays.asList("string1");String[] stringArray = {"string3", "string4"};public static void main(String[] args) {&nbsp; &nbsp; Test test = new Test();&nbsp; &nbsp; System.out.println(toString(test));}输出:0 string0 string1 string3 string4&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java