001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.style; 018 019import org.springframework.lang.Nullable; 020import org.springframework.util.Assert; 021import org.springframework.util.ClassUtils; 022import org.springframework.util.ObjectUtils; 023 024/** 025 * Spring's default {@code toString()} styler. 026 * 027 * <p>This class is used by {@link ToStringCreator} to style {@code toString()} 028 * output in a consistent manner according to Spring conventions. 029 * 030 * @author Keith Donald 031 * @author Juergen Hoeller 032 * @since 1.2.2 033 */ 034public class DefaultToStringStyler implements ToStringStyler { 035 036 private final ValueStyler valueStyler; 037 038 039 /** 040 * Create a new DefaultToStringStyler. 041 * @param valueStyler the ValueStyler to use 042 */ 043 public DefaultToStringStyler(ValueStyler valueStyler) { 044 Assert.notNull(valueStyler, "ValueStyler must not be null"); 045 this.valueStyler = valueStyler; 046 } 047 048 /** 049 * Return the ValueStyler used by this ToStringStyler. 050 */ 051 protected final ValueStyler getValueStyler() { 052 return this.valueStyler; 053 } 054 055 056 @Override 057 public void styleStart(StringBuilder buffer, Object obj) { 058 if (!obj.getClass().isArray()) { 059 buffer.append('[').append(ClassUtils.getShortName(obj.getClass())); 060 styleIdentityHashCode(buffer, obj); 061 } 062 else { 063 buffer.append('['); 064 styleIdentityHashCode(buffer, obj); 065 buffer.append(' '); 066 styleValue(buffer, obj); 067 } 068 } 069 070 private void styleIdentityHashCode(StringBuilder buffer, Object obj) { 071 buffer.append('@'); 072 buffer.append(ObjectUtils.getIdentityHexString(obj)); 073 } 074 075 @Override 076 public void styleEnd(StringBuilder buffer, Object o) { 077 buffer.append(']'); 078 } 079 080 @Override 081 public void styleField(StringBuilder buffer, String fieldName, @Nullable Object value) { 082 styleFieldStart(buffer, fieldName); 083 styleValue(buffer, value); 084 styleFieldEnd(buffer, fieldName); 085 } 086 087 protected void styleFieldStart(StringBuilder buffer, String fieldName) { 088 buffer.append(' ').append(fieldName).append(" = "); 089 } 090 091 protected void styleFieldEnd(StringBuilder buffer, String fieldName) { 092 } 093 094 @Override 095 public void styleValue(StringBuilder buffer, @Nullable Object value) { 096 buffer.append(this.valueStyler.style(value)); 097 } 098 099 @Override 100 public void styleFieldSeparator(StringBuilder buffer) { 101 buffer.append(','); 102 } 103 104}