1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package libcore.util;
18 
19 import java.lang.reflect.Field;
20 import java.lang.reflect.Modifier;
21 import java.util.Arrays;
22 
23 public final class Objects {
Objects()24     private Objects() {}
25 
26     /**
27      * Returns a string reporting the value of each declared field, via reflection.
28      * Static and transient fields are automatically skipped. Produces output like
29      * "SimpleClassName[integer=1234,string="hello",character='c',intArray=[1,2,3]]".
30      */
toString(Object o)31     public static String toString(Object o) {
32         Class<?> c = o.getClass();
33         StringBuilder sb = new StringBuilder();
34         sb.append(c.getSimpleName()).append('[');
35         int i = 0;
36         for (Field f : c.getDeclaredFields()) {
37             if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0) {
38                 continue;
39             }
40             f.setAccessible(true);
41             try {
42                 Object value = f.get(o);
43 
44                 if (i++ > 0) {
45                     sb.append(',');
46                 }
47 
48                 sb.append(f.getName());
49                 sb.append('=');
50 
51                 if (value.getClass().isArray()) {
52                     if (value.getClass() == boolean[].class) {
53                         sb.append(Arrays.toString((boolean[]) value));
54                     } else if (value.getClass() == byte[].class) {
55                         sb.append(Arrays.toString((byte[]) value));
56                     } else if (value.getClass() == char[].class) {
57                         sb.append(Arrays.toString((char[]) value));
58                     } else if (value.getClass() == double[].class) {
59                         sb.append(Arrays.toString((double[]) value));
60                     } else if (value.getClass() == float[].class) {
61                         sb.append(Arrays.toString((float[]) value));
62                     } else if (value.getClass() == int[].class) {
63                         sb.append(Arrays.toString((int[]) value));
64                     } else if (value.getClass() == long[].class) {
65                         sb.append(Arrays.toString((long[]) value));
66                     } else if (value.getClass() == short[].class) {
67                         sb.append(Arrays.toString((short[]) value));
68                     } else {
69                         sb.append(Arrays.toString((Object[]) value));
70                     }
71                 } else if (value.getClass() == Character.class) {
72                     sb.append('\'').append(value).append('\'');
73                 } else if (value.getClass() == String.class) {
74                     sb.append('"').append(value).append('"');
75                 } else {
76                     sb.append(value);
77                 }
78             } catch (IllegalAccessException unexpected) {
79                 throw new AssertionError(unexpected);
80             }
81         }
82         sb.append("]");
83         return sb.toString();
84     }
85 }
86