translate general objects into string. - Java java.lang

Java examples for java.lang:String Join

Description

translate general objects into string.

Demo Code

//package com.java2s;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Object o = "java2s.com";
        System.out.println(objToString(o));
    }// w w  w. j a  v a 2 s. co m

    /**
     * translate general objects into string.
     * 
     * @param o
     * @return The string representation of this Object.
     */
    public static final String objToString(Object o) {
        StringBuilder sb = new StringBuilder();
        printFields(o, sb, 2);
        return sb.toString();
    }

    private static final void printFields(Object o, StringBuilder sb,
            int indentation) {
        if (o == null) {
            sb.append("null");
            return;
        }
        if (indentation > 12) {
            sb.append(o);
            return;
        }
        if (o instanceof Object[]) {
            Object[] os = (Object[]) o;
            sb.append("(").append(os.length).append(")[\n");
            int i = 0;
            for (Object o2 : os) {
                generateIndentation(sb, indentation);
                sb.append(i++);
                sb.append(" => ");
                printFields(o2, sb, indentation + 2);
                sb.append("\n");
            }
            i = sb.length() - 1;
            if (os.length == 0)
                sb.setCharAt(i, ']');
            else {
                generateIndentation(sb, indentation - 2);
                sb.append(']');
            }
            return;
        }
        if (o instanceof Collection<?>) {
            Collection<? extends Object> os = (Collection<?>) o;
            sb.append("(").append(os.size()).append(")[\n");
            int i = 0;
            for (Object o2 : os) {
                generateIndentation(sb, indentation);
                sb.append(i++);
                sb.append(" => ");
                printFields(o2, sb, indentation + 2);
                sb.append("\n");
            }
            i = sb.length() - 1;
            if (os.size() == 0)
                sb.setCharAt(i, ']');
            else {
                generateIndentation(sb, indentation - 2);
                sb.append(']');
            }
            return;
        }
        if (o.getClass().getName().startsWith("java")
                && !o.getClass().getName().equals("java.lang.Object")) {
            sb.append(o);
            return;
        }
        sb.append("{\n");
        for (Field f : o.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            if (f.isSynthetic() || (f.getModifiers() & Modifier.STATIC) > 0)
                continue;
            try {
                generateIndentation(sb, indentation);
                sb.append(f.getName()).append("=");
                printFields(f.get(o), sb, indentation + 2);
                sb.append("\n");
            } catch (Exception e) {
            }
        }
        int i = sb.length() - 2;
        if (sb.charAt(i) == '{')
            sb.setCharAt(i + 1, '}');
        else {
            generateIndentation(sb, indentation - 2);
            sb.append("}");
        }
    }

    private static final void generateIndentation(StringBuilder sb,
            int indentation) {
        while ((indentation = indentation - 2) >= 0) {
            sb.append("  ");
        }
    }
}

Related Tutorials