Source code
Description: source: package: org.apache.commons.lang3.builder, class: RecursiveToStringStyle

    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 *      https://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 * Works with {@link ToStringBuilder} to create a "deep" {@code toString}.
    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     * Constructs a new instance.
    064     */
    065    public RecursiveToStringStyle() {
    066    }
    067
    068    /**
    069     * Returns whether or not to recursively format the given {@link Class}.
    070     * By default, this method always returns {@code true}, but may be overwritten by
    071     * subclasses to filter specific classes.
    072     *
    073     * @param clazz
    074     *            The class to test.
    075     * @return Whether or not to recursively format the given {@link Class}.
    076     */
    077    protected boolean accept(final Class<?> clazz) {
    078        return true;
    079    }
    080
    081    @Override
    082    protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) {
    083        appendClassName(buffer, coll);
    084        appendIdentityHashCode(buffer, coll);
    085        appendDetail(buffer, fieldName, coll.toArray());
    086    }
    087
    088    @Override
    089    public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
    090        if (!ClassUtils.isPrimitiveWrapper(value.getClass()) &&
    091            !String.class.equals(value.getClass()) &&
    092            accept(value.getClass())) {
    093            buffer.append(ReflectionToStringBuilder.toString(value, this));
    094        } else {
    095            super.appendDetail(buffer, fieldName, value);
    096        }
    097    }
    098}