jp.primecloud.auto.util.StringUtils.java Source code

Java tutorial

Introduction

Here is the source code for jp.primecloud.auto.util.StringUtils.java

Source

/*
 * Copyright 2014 by SCSK Corporation.
 * 
 * This file is part of PrimeCloud Controller(TM).
 * 
 * PrimeCloud Controller(TM) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * PrimeCloud Controller(TM) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PrimeCloud Controller(TM). If not, see <http://www.gnu.org/licenses/>.
 */
package jp.primecloud.auto.util;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

/**
 * <p>
 * ???Class??
 * </p>
 *
 */
public class StringUtils {

    private StringUtils() {
    }

    public static String reflectToString(Object object) {
        if (object == null) {
            return null;
        }

        // String??
        if (object instanceof String) {
            return (String) object;
        }

        // ??
        if (object instanceof Number) {
            return object.toString();
        }

        // Boolean??
        if (object instanceof Boolean) {
            return object.toString();
        }

        // Character??
        if (object instanceof Character) {
            return object.toString();
        }

        // ???
        if (object instanceof Object[]) {
            return reflectToString(Arrays.asList((Object[]) object));
        }

        // ??
        if (object instanceof Collection<?>) {
            Iterator<?> iterator = ((Collection<?>) object).iterator();
            if (!iterator.hasNext()) {
                return "[]";
            }

            StringBuilder str = new StringBuilder();
            str.append("[");
            while (true) {
                Object object2 = iterator.next();
                str.append(reflectToString(object2));
                if (!iterator.hasNext()) {
                    break;
                }
                str.append(", ");
            }
            str.append("]");
            return str.toString();
        }

        // ??
        if (object instanceof Map<?, ?>) {
            Iterator<?> iterator = ((Map<?, ?>) object).entrySet().iterator();
            if (!iterator.hasNext()) {
                return "{}";
            }

            StringBuilder str = new StringBuilder();
            str.append("{");
            while (true) {
                Object entry = iterator.next();
                str.append(reflectToString(entry));
                if (!iterator.hasNext()) {
                    break;
                }
                str.append(", ");
            }
            str.append("}");
            return str.toString();
        }

        // Entry??
        if (object instanceof Entry<?, ?>) {
            Entry<?, ?> entry = (Entry<?, ?>) object;
            StringBuilder str = new StringBuilder();
            str.append(reflectToString(entry.getKey()));
            str.append("=");
            str.append(reflectToString(entry.getValue()));
            return str.toString();
        }

        // toString?????
        try {
            Method method = object.getClass().getMethod("toString");
            if (!Object.class.equals(method.getDeclaringClass())) {
                return object.toString();
            }
        } catch (NoSuchMethodException ignore) {
        }

        // ?????
        try {
            PropertyDescriptor[] descriptors = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors();
            StringBuilder str = new StringBuilder();
            str.append("[");
            for (PropertyDescriptor descriptor : descriptors) {
                if ("class".equals(descriptor.getName())) {
                    continue;
                }

                Method readMethod = descriptor.getReadMethod();
                if (readMethod == null) {
                    continue;
                }

                if (str.length() > 1) {
                    str.append(", ");
                }
                Object object2 = readMethod.invoke(object);
                str.append(descriptor.getName()).append("=").append(reflectToString(object2));
            }
            str.append("]");
            return str.toString();
        } catch (IntrospectionException ignore) {
        } catch (InvocationTargetException ignore) {
        } catch (IllegalAccessException ignore) {
        }

        // ????????commons-lang?
        return ReflectionToStringBuilder.toString(object);
    }

}