View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.builder;
18  
19  import java.util.Collection;
20  import java.util.concurrent.atomic.AtomicBoolean;
21  
22  import org.apache.commons.lang3.ClassUtils;
23  import org.apache.commons.lang3.mutable.MutableBoolean;
24  
25  /**
26   * Works with {@link ToStringBuilder} to create a "deep" {@code toString}.
27   *
28   * <p>To use this class write code as follows:</p>
29   *
30   * <pre>
31   * public class Job {
32   *   String title;
33   *   ...
34   * }
35   *
36   * public class Person {
37   *   String name;
38   *   int age;
39   *   boolean smoker;
40   *   Job job;
41   *
42   *   ...
43   *
44   *   public String toString() {
45   *     return new ReflectionToStringBuilder(this, new RecursiveToStringStyle()).toString();
46   *   }
47   * }
48   * </pre>
49   *
50   * <p>This will produce a toString of the format:
51   * {@code Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]]}</p>
52   *
53   * @since 3.2
54   */
55  public class RecursiveToStringStyle extends ToStringStyle {
56  
57      /**
58       * Required for serialization support.
59       *
60       * @see java.io.Serializable
61       */
62      private static final long serialVersionUID = 1L;
63  
64      /**
65       * Constructs a new instance.
66       */
67      public RecursiveToStringStyle() {
68      }
69  
70      /**
71       * Tests whether or not to recursively format the given {@link Class}.
72       * <p>
73       * By default, this method always filters out the following:
74       * </p>
75       * <ul>
76       * <li><a href="https://docs.oracle.com/javase/specs/jls/se25/html/jls-5.html#jls-5.1.7">Boxed primitives</a>, see {@link ClassUtils#isPrimitiveWrapper(Class)}
77       * <li>{@link String}</li>
78       * <li>{@link Number} subclasses</li>
79       * <li>{@link AtomicBoolean}</li>
80       * <li>{@link MutableBoolean}</li>
81       * </ul>
82       *
83       * @param clazz The class to test.
84       * @return Whether or not to recursively format instances of the given {@link Class}.
85       */
86      protected boolean accept(final Class<?> clazz) {
87          // @formatter:off
88          return !ClassUtils.isPrimitiveWrapper(clazz) &&
89                 !String.class.equals(clazz) &&
90                 !Number.class.isAssignableFrom(clazz) &&
91                 !AtomicBoolean.class.equals(clazz) &&
92                 !MutableBoolean.class.equals(clazz);
93          // @formatter:on
94      }
95  
96      @Override
97      protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) {
98          appendClassName(buffer, coll);
99          appendIdentityHashCode(buffer, coll);
100         appendDetail(buffer, fieldName, coll.toArray());
101     }
102 
103     @Override
104     public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
105         if (value != null && accept(value.getClass())) {
106             buffer.append(ReflectionToStringBuilder.toString(value, this));
107         } else {
108             super.appendDetail(buffer, fieldName, value);
109         }
110     }
111 }