Java Object to String toString(Object object)

Here you can find the source of toString(Object object)

Description

Returns a String representation of the given object.

License

Apache License

Parameter

Parameter Description
object the object to build the String of

Return

a representation of the given object.

Declaration

@SuppressWarnings("unchecked")
public static String toString(Object object) 

Method Source Code

//package com.java2s;
/*/*from   www .j  a va  2s . c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

import java.util.Iterator;
import java.util.Map;

public class Main {
    /**
     * Returns a {@link String} representation of the given object.
     * 
     * @param object
     *            the object to build the String of
     * @return a {@link String} representation of the given object.
     */
    @SuppressWarnings("unchecked")
    public static String toString(Object object) {
        if (object == null) {
            return "null";
        }
        Class c = object.getClass();

        StringBuffer sb = new StringBuffer();
        // convert maps to { key1: value1, key2: value2, ... }
        if (Map.class.isAssignableFrom(c)) {
            Map map = (Map) object;
            sb.append("{ ");
            for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
                Object key = iter.next();
                sb.append(toString(key));
                sb.append(": ");
                sb.append(toString(map.get(key)));
                if (iter.hasNext()) {
                    sb.append(",");
                }
                sb.append(" ");
            }
            sb.append("}");
        }
        // convert collections and arrays to [ value1, value2, value3, ... ]
        else if (Collection.class.isAssignableFrom(c) || Object[].class.isAssignableFrom(c)) {
            Object[] array = (Object[]) (c.isArray() ? object : ((Collection) object).toArray());
            sb.append("[ ");
            for (int i = 0; i < array.length; i++) {
                sb.append(toString(array[i]));
                if (i < array.length - 1) {
                    sb.append(",");
                }
                sb.append(" ");
            }
            sb.append("]");
        } else {
            sb.append(object.toString());
        }
        return sb.toString();
    }
}

Related

  1. objToString(Object obj)
  2. objToString(Object[] obj)
  3. toString(Object o)
  4. toString(Object obj)
  5. toString(Object obj)
  6. toString(Object object)
  7. toString(Object object)
  8. toString(Object object)
  9. toString(Object object, Class objectClass)