View Javadoc
1   package eteg.util;
2   
3   import java.lang.reflect.Method;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.List;
7   
8   /***
9    *
10   * <pre>
11   * $Header: /cvsroot/sinon/sinon/src/java/eteg/util/ReflectionUtil.java,v 1.1.1.1 2005/06/17 14:10:29 thiagohp Exp $
12   * </pre>
13   *
14   * @since 05/09/2004.
15   * @author Thiago H. de Paula Figueiredo (last modification by $Author: thiagohp $)
16   * @version $Revision: 1.1.1.1 $
17   */
18  public class ReflectionUtil {
19  
20      private static final String GETTER_PREFIX = "get";
21      private static final int GETTER_PREFIX_SIZE = GETTER_PREFIX.length();
22      private static String NEWLINE = System.getProperty("line.separator");
23      private static String NEWLINE_TAB = NEWLINE + "\t";
24      private static final String BUFFER_APPEND_NEWLINE = "buffer.append(\"//n\");";
25      private static final String BUFFER_APPEND_TAB = "buffer.append(\"//t\");";
26      private static final String EMPTY_STRING = "";
27  
28      final public static String toString(Object object) {
29          return toString(object, 0);
30      }
31  
32      final public static String toString(Object object, int tabLevel) {
33          return toString(object, tabLevel, EMPTY_STRING);
34      }
35  
36      final public static String toString(Object object, int tabLevel, String prefix) {
37  
38          Class objectClass = object.getClass();
39          Method[] getters = getGetterMethods(objectClass);
40          StringBuffer buffer = new StringBuffer();
41  
42          if (object.getClass().isPrimitive()) {
43  
44              buffer.append(tabs(tabLevel));
45              buffer.append(object);
46              buffer.append(NEWLINE);
47  
48              return buffer.toString();
49  
50          }
51  
52          if (tabLevel == 0) {
53  
54              buffer.append(tabs(tabLevel));
55              buffer.append("[");
56              buffer.append(objectClass.getName());
57              buffer.append(NEWLINE);
58  
59          }
60  
61          tabLevel++;
62  
63          for (int i = 0; i < getters.length; i++) {
64  
65              Method getter = getters[i];
66  
67              String propertyName = getPropertyName(getter);
68  
69              final Class returnType = getter.getReturnType();
70  
71              System.out.print(buffer);
72              buffer.setLength(0);
73              Object value = null;
74  
75              try {
76                  value = getter.invoke(object, null);
77              } catch (Exception e) {
78                  e.printStackTrace();
79              }
80  
81              if (value == null ||
82                      returnType.isPrimitive() ||
83                      returnType == String.class ||
84                      value == null ||
85                      Collection.class.isAssignableFrom(value.getClass())) {
86  
87                  if (value != null && value.getClass() == String.class) {
88                      value = ((String) value).trim();
89                  }
90  
91                  buffer.append(tabs(tabLevel));
92                  buffer.append(prefix);
93                  buffer.append(propertyName);
94                  buffer.append(" = ");
95                  buffer.append(value);
96                  buffer.append(NEWLINE);
97  
98              }
99  
100             else if (returnType.isArray()) {
101 
102                 buffer.append(tabs(tabLevel));
103                 buffer.append(propertyName);
104                 buffer.append("[] = ");
105 
106                 Object[] objects = (Object[]) value;
107 
108                 if (objects.length == 0) {
109 
110                     buffer.append("empty array");
111                     buffer.append(NEWLINE);
112 
113                 }
114 
115                 else {
116 
117                     for (int j = 0; j < objects.length; j++) {
118                         buffer.append(tabs(tabLevel));
119                         buffer.append("[" + j + "] ");
120                         buffer.append(toString(objects[j], tabLevel + 1));
121                     }
122 
123                 }
124 
125             }
126 
127             else {
128 
129                 buffer.append(tabs(tabLevel));
130                 buffer.append(propertyName);
131                 buffer.append(NEWLINE);
132                 buffer.append(toString(value, tabLevel + 1));
133 
134             }
135 
136         }
137 
138         tabLevel --;
139 
140         if (buffer.length() > 0 && buffer.charAt(buffer.length() - 1) != '\n') {
141             buffer.append(NEWLINE);
142         }
143 
144         if (tabLevel == 0) {
145 
146             buffer.append(tabs(tabLevel));
147             buffer.append("]");
148             buffer.append(NEWLINE);
149 
150         }
151 
152         return buffer.toString();
153 
154     }
155 
156     final public static String tabs(int n) {
157 
158         StringBuffer buffer = new StringBuffer();
159 
160         for (int i = 0; i < n; i++) {
161             buffer.append("\t");
162         }
163 
164         return buffer.toString();
165 
166     }
167 
168     final public static String createToStringMethod(Class someClass) {
169 
170         Method[] getters = getGetterMethods(someClass);
171         StringBuffer buffer = new StringBuffer();
172 
173         buffer.append("public String toString() {");
174         buffer.append(NEWLINE_TAB);
175         buffer.append("StringBuffer buffer = new StringBuffer();");
176         buffer.append(NEWLINE_TAB);
177         buffer.append(bufferAppend("["));
178         buffer.append(bufferAppend(someClass.getName()));
179         buffer.append(NEWLINE_TAB);
180         buffer.append(bufferAppend(": "));
181         buffer.append(NEWLINE_TAB);
182         buffer.append(BUFFER_APPEND_NEWLINE);
183         buffer.append(BUFFER_APPEND_TAB);
184 
185         for (int i = 0; i < getters.length; i++) {
186 
187             String propertyName = getPropertyName(getters[i]);
188             buffer.append(BUFFER_APPEND_TAB);
189             buffer.append(bufferAppend(propertyName));
190             buffer.append(NEWLINE_TAB);
191             buffer.append(bufferAppend(" = "));
192             buffer.append(NEWLINE_TAB);
193             buffer.append("buffer.append(" + getters[i].getName() + "());");
194             buffer.append(NEWLINE_TAB);
195             buffer.append(BUFFER_APPEND_NEWLINE);
196             buffer.append(NEWLINE_TAB);
197             buffer.append(BUFFER_APPEND_TAB);
198             buffer.append(NEWLINE_TAB);
199 
200 //            if (i < getters.length - 1) {
201 //                buffer.append(bufferAppend(", "));
202 //                buffer.append(NEWLINE_TAB);
203 //            }
204 
205         }
206 
207         buffer.append(bufferAppend("]"));
208         buffer.append(NEWLINE_TAB);
209         buffer.append("return buffer.toString();");
210         buffer.append(NEWLINE);
211         buffer.append("}");
212 
213         return buffer.toString();
214 
215     }
216 
217     private static String bufferAppend(String string) {
218         return "buffer.append(\"" + string + "\");";
219     }
220 
221     public static Method[] getGetterMethods(Class someClass) {
222 
223         Method[] allMethods = someClass.getMethods();
224 
225         List getters = new ArrayList();
226 
227         for (int i = 0; i < allMethods.length; i++) {
228 
229             String name = allMethods[i].getName();
230             Class[] parameterTypes = allMethods[i].getParameterTypes();
231 
232             if (name.startsWith(GETTER_PREFIX) &&
233                     parameterTypes.length == 0 &&
234                     name.equals("getClass") == false) {
235 
236                 getters.add(allMethods[i]);
237 
238             }
239 
240         }
241 
242         Method[] getterNames = new Method[getters.size()];
243         getters.toArray(getterNames);
244 
245         return getterNames;
246 
247     }
248 
249     public static String getPropertyName(final Method getterMethod) {
250         return getPropertyName(getterMethod.getName());
251     }
252 
253     public static String getPropertyName(final String methodName) {
254 
255         StringBuffer propertyName = new StringBuffer(methodName);
256         propertyName.delete(0, GETTER_PREFIX_SIZE);
257 
258         char c = propertyName.charAt(0);
259         propertyName.setCharAt(0, Character.toLowerCase(c));
260 
261         return propertyName.toString();
262 
263     }
264 
265 }