RecursiveToStringStyle.java

  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.  *      http://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. import java.util.Collection;

  19. import org.apache.commons.lang3.ClassUtils;

  20. /**
  21.  * Works with {@link ToStringBuilder} to create a "deep" {@code toString}.
  22.  *
  23.  * <p>To use this class write code as follows:</p>
  24.  *
  25.  * <pre>
  26.  * public class Job {
  27.  *   String title;
  28.  *   ...
  29.  * }
  30.  *
  31.  * public class Person {
  32.  *   String name;
  33.  *   int age;
  34.  *   boolean smoker;
  35.  *   Job job;
  36.  *
  37.  *   ...
  38.  *
  39.  *   public String toString() {
  40.  *     return new ReflectionToStringBuilder(this, new RecursiveToStringStyle()).toString();
  41.  *   }
  42.  * }
  43.  * </pre>
  44.  *
  45.  * <p>This will produce a toString of the format:
  46.  * {@code Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]]}</p>
  47.  *
  48.  * @since 3.2
  49.  */
  50. public class RecursiveToStringStyle extends ToStringStyle {

  51.     /**
  52.      * Required for serialization support.
  53.      *
  54.      * @see java.io.Serializable
  55.      */
  56.     private static final long serialVersionUID = 1L;

  57.     /**
  58.      * Constructs a new instance.
  59.      */
  60.     public RecursiveToStringStyle() {
  61.     }

  62.     /**
  63.      * Returns whether or not to recursively format the given {@link Class}.
  64.      * By default, this method always returns {@code true}, but may be overwritten by
  65.      * subclasses to filter specific classes.
  66.      *
  67.      * @param clazz
  68.      *            The class to test.
  69.      * @return Whether or not to recursively format the given {@link Class}.
  70.      */
  71.     protected boolean accept(final Class<?> clazz) {
  72.         return true;
  73.     }

  74.     @Override
  75.     protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) {
  76.         appendClassName(buffer, coll);
  77.         appendIdentityHashCode(buffer, coll);
  78.         appendDetail(buffer, fieldName, coll.toArray());
  79.     }

  80.     @Override
  81.     public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
  82.         if (!ClassUtils.isPrimitiveWrapper(value.getClass()) &&
  83.             !String.class.equals(value.getClass()) &&
  84.             accept(value.getClass())) {
  85.             buffer.append(ReflectionToStringBuilder.toString(value, this));
  86.         } else {
  87.             super.appendDetail(buffer, fieldName, value);
  88.         }
  89.     }
  90. }