1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.javaflow.utils; |
18 | |
|
19 | |
import java.io.ByteArrayInputStream; |
20 | |
import java.io.ByteArrayOutputStream; |
21 | |
import java.io.IOException; |
22 | |
import java.io.ObjectInputStream; |
23 | |
import java.io.ObjectOutputStream; |
24 | |
import java.lang.reflect.Field; |
25 | |
import java.lang.reflect.Method; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.Map; |
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public final class ReflectionUtils { |
36 | |
|
37 | 0 | private final static Log log = LogFactory.getLog(ReflectionUtils.class); |
38 | |
|
39 | |
public interface Matcher { |
40 | |
boolean matches(final String pName); |
41 | |
} |
42 | |
|
43 | |
public interface Indexer { |
44 | |
void put(final Map pMap, final String pKey, final Object pObject); |
45 | |
} |
46 | |
|
47 | 0 | private static Indexer defaultIndexer = new DefaultIndexer(); |
48 | 0 | private static Matcher defaultMatcher = new DefaultMatcher(); |
49 | |
|
50 | 0 | public static class DefaultMatcher implements Matcher { |
51 | |
public boolean matches(final String pName) { |
52 | 0 | return true; |
53 | |
} |
54 | |
} |
55 | |
|
56 | 0 | public static class DefaultIndexer implements Indexer { |
57 | |
public void put(final Map pMap, final String pKey, final Object pObject) { |
58 | 0 | pMap.put(pKey, pObject); |
59 | 0 | } |
60 | |
} |
61 | |
|
62 | |
public static Map discoverFields( |
63 | |
final Class pClazz, |
64 | |
final Matcher pMatcher |
65 | |
) { |
66 | |
|
67 | 0 | return discoverFields(pClazz, pMatcher, defaultIndexer); |
68 | |
} |
69 | |
|
70 | |
public static Map discoverFields( |
71 | |
final Class pClazz |
72 | |
) { |
73 | |
|
74 | 0 | return discoverFields(pClazz, defaultMatcher, defaultIndexer); |
75 | |
} |
76 | |
|
77 | |
public static Map discoverFields( |
78 | |
final Class pClazz, |
79 | |
final Matcher pMatcher, |
80 | |
final Indexer pIndexer |
81 | |
) { |
82 | |
|
83 | 0 | log.debug("discovering fields on " + pClazz.getName()); |
84 | |
|
85 | 0 | final Map result = new HashMap(); |
86 | |
|
87 | 0 | Class current = pClazz; |
88 | |
do { |
89 | 0 | final Field[] fields = current.getDeclaredFields(); |
90 | 0 | for (int i = 0; i < fields.length; i++) { |
91 | 0 | final String fname = fields[i].getName(); |
92 | 0 | if (pMatcher.matches(fname)) { |
93 | 0 | pIndexer.put(result, fname, fields[i]); |
94 | |
|
95 | 0 | log.debug("discovered field " + fname + " -> " + fields[i]); |
96 | |
} |
97 | |
} |
98 | 0 | current = current.getSuperclass(); |
99 | 0 | } while(current != null); |
100 | |
|
101 | 0 | return result; |
102 | |
} |
103 | |
|
104 | |
|
105 | |
public static Map discoverMethods( |
106 | |
final Class pClazz, |
107 | |
final Matcher pMatcher |
108 | |
) { |
109 | |
|
110 | 0 | return discoverMethods(pClazz, pMatcher, defaultIndexer); |
111 | |
} |
112 | |
|
113 | |
public static Map discoverMethods( |
114 | |
final Class pClazz |
115 | |
) { |
116 | |
|
117 | 0 | return discoverMethods(pClazz, defaultMatcher, defaultIndexer); |
118 | |
} |
119 | |
|
120 | |
public static Map discoverMethods( |
121 | |
final Class pClazz, |
122 | |
final Matcher pMatcher, |
123 | |
final Indexer pIndexer |
124 | |
) { |
125 | |
|
126 | 0 | log.debug("discovering methods on " + pClazz.getName()); |
127 | |
|
128 | 0 | final Map result = new HashMap(); |
129 | |
|
130 | 0 | Class current = pClazz; |
131 | |
do { |
132 | 0 | final Method[] methods = current.getDeclaredMethods(); |
133 | 0 | for (int i = 0; i < methods.length; i++) { |
134 | 0 | final String mname = methods[i].getName(); |
135 | 0 | if (pMatcher.matches(mname)) { |
136 | 0 | pIndexer.put(result, mname, methods[i]); |
137 | |
|
138 | 0 | log.debug("discovered method " + mname + " -> " + methods[i]); |
139 | |
} |
140 | |
} |
141 | 0 | current = current.getSuperclass(); |
142 | 0 | } while(current != null); |
143 | |
|
144 | 0 | return result; |
145 | |
} |
146 | |
|
147 | |
public static Object cast(Object o) throws IOException, ClassNotFoundException { |
148 | 0 | final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
149 | 0 | final ObjectOutputStream oos = new ObjectOutputStream(buffer); |
150 | 0 | oos.writeObject(o); |
151 | 0 | oos.flush(); |
152 | 0 | final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); |
153 | 0 | return ois.readObject(); |
154 | |
} |
155 | |
|
156 | |
public static String getClassName(final Object o) { |
157 | 0 | if (o == null) { |
158 | 0 | return "unknown"; |
159 | |
} |
160 | |
|
161 | 0 | return o.getClass().getName() + "@" + o.hashCode(); |
162 | |
} |
163 | |
|
164 | |
public static String getClassLoaderName(final Object o) { |
165 | 0 | if (o == null) { |
166 | 0 | return "unknown"; |
167 | |
} |
168 | |
|
169 | 0 | return getClassName(o.getClass().getClassLoader()); |
170 | |
} |
171 | |
} |