Here you can find the source of nullSafeToString(Object obj)
Parameter | Description |
---|---|
obj | the object to convert to a String; may be null |
public static String nullSafeToString(Object obj)
//package com.java2s; /*/* w w w . ja v a 2 s . c om*/ * Copyright 2015-2017 the original author or authors. * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v2.0 which * accompanies this distribution and is available at * * http://www.eclipse.org/legal/epl-v20.html */ import java.util.Arrays; public class Main { /** * Convert the supplied {@code Object} to a {@code String} using the * following algorithm. * * <ul> * <li>If the supplied object is {@code null}, this method returns {@code "null"}.</li> * <li>If the supplied object is a primitive array, the appropriate * {@code Arrays#toString(...)} variant will be used to convert it to a String.</li> * <li>If the supplied object is an object array, {@code Arrays#deepToString(Object[])} * will be used to convert it to a String.</li> * <li>Otherwise, the result of invoking {@code toString()} on the object * will be returned.</li> * </ul> * * @param obj the object to convert to a String; may be {@code null} * @return a String representation of the supplied object * @see Arrays#deepToString(Object[]) * @see ClassUtils#nullSafeToString(Class...) */ public static String nullSafeToString(Object obj) { if (obj == null) { return "null"; } else if (obj.getClass().isArray()) { if (obj.getClass().getComponentType().isPrimitive()) { if (obj instanceof boolean[]) { return Arrays.toString((boolean[]) obj); } if (obj instanceof char[]) { return Arrays.toString((char[]) obj); } if (obj instanceof short[]) { return Arrays.toString((short[]) obj); } if (obj instanceof byte[]) { return Arrays.toString((byte[]) obj); } if (obj instanceof int[]) { return Arrays.toString((int[]) obj); } if (obj instanceof long[]) { return Arrays.toString((long[]) obj); } if (obj instanceof float[]) { return Arrays.toString((float[]) obj); } if (obj instanceof double[]) { return Arrays.toString((double[]) obj); } } return Arrays.deepToString((Object[]) obj); } // else return obj.toString(); } }