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</code>.</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]]</code></p> 050 * 051 * @since 3.2 052 * @version $Id: RecursiveToStringStyle.java 1572875 2014-02-28 08:34:55Z britter $ 053 */ 054public class RecursiveToStringStyle extends ToStringStyle { 055 056 /** 057 * Required for serialization support. 058 * 059 * @see java.io.Serializable 060 */ 061 private static final long serialVersionUID = 1L; 062 063 /** 064 * <p>Constructor.</p> 065 */ 066 public RecursiveToStringStyle() { 067 super(); 068 } 069 070 @Override 071 public void appendDetail(StringBuffer buffer, String fieldName, Object value) { 072 if (!ClassUtils.isPrimitiveWrapper(value.getClass()) && 073 !String.class.equals(value.getClass()) && 074 accept(value.getClass())) { 075 buffer.append(ReflectionToStringBuilder.toString(value, this)); 076 } else { 077 super.appendDetail(buffer, fieldName, value); 078 } 079 } 080 081 @Override 082 protected void appendDetail(StringBuffer buffer, String fieldName, Collection<?> coll) { 083 appendClassName(buffer, coll); 084 appendIdentityHashCode(buffer, coll); 085 appendDetail(buffer, fieldName, coll.toArray()); 086 } 087 088 /** 089 * Returns whether or not to recursively format the given <code>Class</code>. 090 * By default, this method always returns {@code true}, but may be overwritten by 091 * sub-classes to filter specific classes. 092 * 093 * @param clazz 094 * The class to test. 095 * @return Whether or not to recursively format the given <code>Class</code>. 096 */ 097 protected boolean accept(final Class<?> clazz) { 098 return true; 099 } 100}