Java toString() 使用反射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/443730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:43:06  来源:igfitidea点击:

Java toString() using reflection?

javaperformancereflectiontostring

提问by James McMahon

I was writing a toString() for a class in Java the other day by manually writing out each element of the class to a String and it occurred to me that using reflection it might be possible to create a generic toString() method that could work on ALL classes. I.E. it would figure out the field names and values and send them out to a String.

前几天我正在为 Java 中的一个类编写一个 toString(),通过手动将类的每个元素写入一个 String,我突然想到使用反射可能会创建一个可以工作的通用 toString() 方法在所有课程上。IE 它将找出字段名称和值并将它们发送到字符串。

Getting the field names is fairly simple, here is what a co-worker came up with:

获取字段名称相当简单,这是一位同事提出的:

public static List initFieldArray(String className) throws ClassNotFoundException {

    Class c = Class.forName(className);
    Field field[] = c.getFields();
    List<String> classFields = new ArrayList(field.length);

    for (int i = 0; i < field.length; i++) {
        String cf = field[i].toString();
        classFields.add(cf.substring(cf.lastIndexOf(".") + 1));
    }

    return classFields;
}

Using a factory I could reduce the performance overhead by storing the fields once, the first time the toString() is called. However finding the values could be a lot more expensive.

使用工厂,我可以通过在第一次调用 toString() 时存储一次字段来降低性能开销。然而,找到这些值可能要贵得多。

Due to the performance of reflection this may be more hypothetical then practical. But I am interested in the idea of reflection and how I can use it to improve my everyday programming.

由于反射的性能,这可能比实际更假设。但我对反射的想法以及如何使用它来改进我的日常编程感兴趣。

采纳答案by krosenvold

Apache commons-lang ReflectionToStringBuilderdoes this for you.

Apache commons-lang ReflectionToStringBuilder为你做这件事。

import org.apache.commons.lang3.builder.ReflectionToStringBuilder

// your code goes here

public String toString() {
   return ReflectionToStringBuilder.toString(this);
}

回答by Olivier

If you're using Eclipse, you may also have a look at JUtils toString generator, which does it statically (generating the method in your source code).

如果您使用的是 Eclipse,您还可以查看JUtils toString generator,它是静态的(在源代码中生成方法)。

回答by McDowell

Not reflection, but I had a look at generating the toString method (along with equals/hashCode) as a post-compilation step using bytecode manipulation. Results were mixed.

不是反射,但我查看了使用字节码操作生成 toString 方法(以及 equals/hashCode)作为编译后步骤。结果喜忧参半。

回答by Steve B.

W/reflection, as I hadn't been aware of the apache library:

W/reflection,因为我不知道 apache 库:

(be aware that if you do this you'll probably need to deal with subobjects and make sure they print properly - in particular, arrays won't show you anything useful)

(请注意,如果您这样做,您可能需要处理子对象并确保它们正确打印 - 特别是,数组不会向您显示任何有用的东西)

@Override
public String toString()
{
    StringBuilder b = new StringBuilder("[");
    for (Field f : getClass().getFields())
    {
        if (!isStaticField(f))
        {
            try
            {
                b.append(f.getName() + "=" + f.get(this) + " ");
            } catch (IllegalAccessException e)
            {
                // pass, don't print
            }
        }
    }
    b.append(']');
    return b.toString();
}


private boolean isStaticField(Field f)
{
    return Modifier.isStatic(f.getModifiers());
}

回答by James McMahon

Here is the Netbeans equivalent to Olivier's answer; smart-codegen plugin for Netbeans.

这是相当于 Olivier 答案的 Netbeans;Netbeans 的智能代码生成插件

回答by nazar_art

You can use already implemented libraries, as ReflectionToStringBuilderfrom Apache commons-lang. As was mentioned.

您可以使用已经实现的库,如Apache commons-lang 中的ReflectionToStringBuilder。正如所提到的。

Or write smt similar by yourself with reflection API.

或者自己用反射 API编写类似的 smt 。

Here is some example:

下面是一些例子

class UniversalAnalyzer {

   private ArrayList<Object> visited = new ArrayList<Object>();

   /**
    * Converts an object to a string representation that lists all fields.
    * @param obj an object
    * @return a string with the object's class name and all field names and
    * values
    */
   public String toString(Object obj) {
      if (obj == null) return "null";
      if (visited.contains(obj)) return "...";
      visited.add(obj);
      Class cl = obj.getClass();
      if (cl == String.class) return (String) obj;
      if (cl.isArray()) {
         String r = cl.getComponentType() + "[]{";
         for (int i = 0; i < Array.getLength(obj); i++) {
            if (i > 0) r += ",";
            Object val = Array.get(obj, i);
            if (cl.getComponentType().isPrimitive()) r += val;
            else r += toString(val);
         }
         return r + "}";
      }

      String r = cl.getName();
      // inspect the fields of this class and all superclasses
      do {
         r += "[";
         Field[] fields = cl.getDeclaredFields();
         AccessibleObject.setAccessible(fields, true);
         // get the names and values of all fields
         for (Field f : fields) {
            if (!Modifier.isStatic(f.getModifiers())) {
               if (!r.endsWith("[")) r += ",";
               r += f.getName() + "=";
               try {
                  Class t = f.getType();
                  Object val = f.get(obj);
                  if (t.isPrimitive()) r += val;
                  else r += toString(val);
               } catch (Exception e) {
                  e.printStackTrace();
               }
            }
         }
         r += "]";
         cl = cl.getSuperclass();
      } while (cl != null);

      return r;
   }    
}

回答by Daniel Schmidt

Another option, if you are ok with JSON, is Google's GSON library.

如果您对 JSON 没问题,另一种选择是 Google 的 GSON 库。

public String toString() {
    return new GsonBuilder().setPrettyPrinting().create().toJson(this);
}

It's going to do the reflection for you. This produces a nice, easy to read JSON file. Easy-to-read being relative, non tech folks might find the JSON intimidating.

它会为你做反思。这会生成一个漂亮的、易于阅读的 JSON 文件。相对易于阅读,非技术人员可能会发现 JSON 令人生畏。

You could make the GSONBuilder a member variable too, if you don't want to new it up every time.

如果您不想每次都更新它,您也可以将 GSONBuilder 设为成员变量。

If you have data that can't be printed (like a stream) or data you just don't want to print, you can just add @Expose tags to the attributes you want to print and then use the following line.

如果您有无法打印的数据(如流)或您只是不想打印的数据,则只需将 @Expose 标记添加到要打印的属性,然后使用以下行。

 new GsonBuilder()
.setPrettyPrinting()
.excludeFieldsWithoutExposeAnnotation()
.create()
.toJson(this);