im.aop.loggers.messageinterpolation.ArrayToStringStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of im-aop-loggers Show documentation
Show all versions of im-aop-loggers Show documentation
A handy and configurable sets of annotation-based loggers for Spring Boot that can
log every execution of a method when entering or exiting normally or abnormally, without you
writing a single line of code using aspect-oriented programming (AOP)
The newest version!
package im.aop.loggers.messageinterpolation;
import org.springframework.beans.factory.annotation.Autowired;
/**
* {@link ToStringStrategy} implementation for array, using {@link ObjectToStringStrategy} to return
* a String representation of the element. Encloses element within square brackets "[]".
*
* @author Andy Lian
*/
public class ArrayToStringStrategy implements ToStringStrategy {
@Autowired private ObjectToStringStrategy objectToStringStrategy;
@Override
public boolean supports(Object object) {
return object != null && object.getClass().isArray();
}
@Override
public String toString(Object object) {
return toString((Object[]) object);
}
private String toString(final Object[] array) {
final int maxIndex = array.length - 1;
if (maxIndex == -1) {
return "[]";
}
final StringBuilder builder = new StringBuilder("[");
for (int index = 0; ; index++) {
builder.append(toStringOrNullString(array[index]));
if (index == maxIndex) {
return builder.append("]").toString();
}
builder.append(", ");
}
}
private Object toStringOrNullString(Object element) {
return element == null ? "null" : objectToStringStrategy.toString(element);
}
}