⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formatting.java

📁 不管是测试驱动开发或者是其它的开发模式
💻 JAVA
字号:
/*  Copyright (c) 2000-2004 jMock.org
 */
package org.jmock.core;

import java.lang.reflect.Array;
import java.lang.reflect.Proxy;
import java.util.Collection;


public class Formatting
{
    public static String toReadableString( Object element ) {
        if (element == null) {
            return "null";
        } else if (Proxy.isProxyClass(element.getClass())) {
            return unpackProxy(element).toString();
        } else if (element.getClass().isArray()) {
            return join(element, new StringBuffer()).toString();
        } else {
            return element.toString();
        }
    }

    private static Object unpackProxy( Object element ) {
        Object invocationHandler = Proxy.getInvocationHandler(element);
        return (invocationHandler instanceof DynamicMock
                ? invocationHandler
                : element);
    }

    public static StringBuffer join( Object array, StringBuffer buf ) {
        return join(array, buf, "[", "]");
    }

    public static StringBuffer join( Collection collection, StringBuffer buf, String prefix, String postfix ) {
        return join(collection.toArray(), buf, prefix, postfix);
    }

    public static StringBuffer join( Collection collection, StringBuffer buf,
                                     String prefix, String separator, String postfix ) {
        return join(collection.toArray(), buf, prefix, separator, postfix);
    }

    public static StringBuffer join( Object array, StringBuffer buf, String prefix, String postfix ) {
        return join(array, buf, prefix, ", ", postfix);
    }

    public static StringBuffer join( Object array, StringBuffer buf,
                                     String prefix, String separator, String postfix ) {
        buf.append(prefix);
        for (int i = 0; i < Array.getLength(array); i++) {
            if (i > 0) buf.append(separator);

            Object element = Array.get(array, i);

            if (null == element) {
                buf.append("<null>");
            } else if (element.getClass().isArray()) {
                join(element, buf);
            } else {
                buf.append("<");
                buf.append(toReadableString(element));
                buf.append(">");
            }
        }
        buf.append(postfix);
        return buf;
    }

    public static String classShortName( Class c ) {
        String fullTypeName = c.getName();
        return fullTypeName.substring(Math.max(fullTypeName.lastIndexOf('.'), fullTypeName.lastIndexOf('$')) + 1);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -