简体   繁体   English

Java toString()使用反射?

[英]Java toString() using reflection?

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. 前几天我通过手动将类的每个元素写成String来为Java中的类编写toString(),并且我发现使用反射可能会创建一个可以工作的通用toString()方法所有课程。 IE it would figure out the field names and values and send them out to a String. IE它将找出字段名称和值并将它们发送到String。

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. 使用工厂我可以通过存储一次字段来减少性能开销,第一次调用toString()。 However finding the values could be a lot more expensive. 然而,找到这些价值可能要贵得多。

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. 但我对反思的想法以及如何使用它来改进我的日常编程感兴趣。

Apache commons-lang ReflectionToStringBuilder does 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);
}

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. 这会产生一个漂亮,易于阅读的JSON文件。 Easy-to-read being relative, non tech folks might find the JSON intimidating. 易于阅读的相对非技术人员可能会发现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);

W/reflection, as I hadn't been aware of the apache library: W /反射,因为我没有意识到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());
}

如果你正在使用Eclipse,你也可以查看JUtils toString生成器 ,它静态地执行它(在源代码中生成方法)。

You can use already implemented libraries, as ReflectionToStringBuilder from Apache commons-lang. 您可以使用已实现的库,如Apache commons-lang中的ReflectionToStringBuilder As was mentioned. 如上所述。

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

Not reflection, but I had a look at generating the toString method (along with equals/hashCode) as a post-compilation step using bytecode manipulation. 不是反射,但我看一下使用字节码操作生成toString方法(以及equals / hashCode)作为后编译步骤。 Results were mixed. 结果好坏参半。

Here is the Netbeans equivalent to Olivier's answer; 这是Netbeans相当于Olivier的答案; smart-codegen plugin for Netbeans . Netbeans的smart-codegen插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM