    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.builder;
    018
    019import java.util.Collection;
    020
    021import org.apache.commons.lang3.ClassUtils;
    022
    023/**
    024 * <p>Works with {@link ToStringBuilder} to create a "deep" {@code toString}.</p>
    025 *
    026 * <p>To use this class write code as follows:</p>
    027 *
    028 * <pre>
    029 * public class Job {
    030 *   String title;
    031 *   ...
    032 * }
    033 *
    034 * public class Person {
    035 *   String name;
    036 *   int age;
    037 *   boolean smoker;
    038 *   Job job;
    039 *
    040 *   ...
    041 *
    042 *   public String toString() {
    043 *     return new ReflectionToStringBuilder(this, new RecursiveToStringStyle()).toString();
    044 *   }
    045 * }
    046 * </pre>
    047 *
    048 * <p>This will produce a toString of the format:
    049 * {@code Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]]}</p>
    050 *
    051 * @since 3.2
    052 */
    053public class RecursiveToStringStyle extends ToStringStyle {
    054
    055    /**
    056     * Required for serialization support.
    057     *
    058     * @see java.io.Serializable
    059     */
    060    private static final long serialVersionUID = 1L;
    061
    062    /**
    063     * <p>Constructor.</p>
    064     */
    065    public RecursiveToStringStyle() {
    066    }
    067
    068    @Override
    069    public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
    070        if (!ClassUtils.isPrimitiveWrapper(value.getClass()) &&
    071            !String.class.equals(value.getClass()) &&
    072            accept(value.getClass())) {
    073            buffer.append(ReflectionToStringBuilder.toString(value, this));
    074        } else {
    075            super.appendDetail(buffer, fieldName, value);
    076        }
    077    }
    078
    079    @Override
    080    protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) {
    081        appendClassName(buffer, coll);
    082        appendIdentityHashCode(buffer, coll);
    083        appendDetail(buffer, fieldName, coll.toArray());
    084    }
    085
    086    /**
    087     * Returns whether or not to recursively format the given {@code Class}.
    088     * By default, this method always returns {@code true}, but may be overwritten by
    089     * sub-classes to filter specific classes.
    090     *
    091     * @param clazz
    092     *            The class to test.
    093     * @return Whether or not to recursively format the given {@code Class}.
    094     */
    095    protected boolean accept(final Class<?> clazz) {
    096        return true;
    097    }
    098}