41public class Introspection {
42 private Introspection() {
61 List<Method> lst = internalGetMethods(c);
64 return lst.toArray(
new Method[lst.size()]);
67 private static List<Method> internalGetMethods(Class<?> c) {
69 List<Method> methods =
new ArrayList<Method>();
70 Method[] mt = c.getDeclaredMethods();
71 for (
int i = 0; i < mt.length; i++)
74 List<Method> inheritedMethods =
new ArrayList<Method>();
75 Class<?>[] iface = c.getInterfaces();
76 for (
int i = 0; i < iface.length; i++)
77 inheritedMethods.addAll(internalGetMethods(iface[i]));
78 if (!c.isInterface()) {
79 Class<?> s = c.getSuperclass();
81 List<Method> supers = internalGetMethods(s);
82 for (Method m : supers) {
85 if (m !=
null && !Modifier.isAbstract(m.getModifiers()))
86 removeByNameAndSignature(inheritedMethods, m);
88 supers.addAll(inheritedMethods);
89 inheritedMethods = supers;
94 for (Method m : methods)
95 removeByNameAndSignature(inheritedMethods, m);
97 for (Method m : inheritedMethods) {
100 if (!methods.contains(m))
106 private static void removeByNameAndSignature(List<Method> methods, Method m) {
107 for (ListIterator<Method> it = methods.listIterator(); it.hasNext();) {
108 Method tst = it.next();
111 if (tst.getName().equals(m.getName()) && tst.getReturnType() == m.getReturnType() &&
sameSignature(tst, m))
126 Class<?>[] pt1 = m1.getParameterTypes();
127 Class<?>[] pt2 = m2.getParameterTypes();
128 if (pt1.length != pt2.length)
130 for (
int i = 0; i < pt1.length; i++)
131 if (pt1[i] != pt2[i])
151 List<Field> lst =
new ArrayList<Field>();
152 processFields(c, lst);
153 Set<Class<?>> traversedInterfaces =
new HashSet<Class<?>>();
154 processInterfaceFields(c, lst, traversedInterfaces);
156 if (!c.isInterface()) {
157 Class<?> s = c.getSuperclass();
159 processFields(s, lst);
160 processInterfaceFields(s, lst, traversedInterfaces);
161 s = s.getSuperclass();
166 return lst.toArray(
new Field[lst.size()]);
169 private static void processFields(
final Class<?> c,
final List<Field> lst) {
170 Field[] f = c.getDeclaredFields();
171 for (
int i = 0; i < f.length; i++)
175 private static void processInterfaceFields(
final Class<?> c,
final List<Field> lst,
176 final Set<Class<?>> traversedInterfaces) {
177 Class<?>[] iface = c.getInterfaces();
178 for (
int i = 0; i < iface.length; i++) {
179 if (traversedInterfaces.contains(iface[i]))
181 traversedInterfaces.add(iface[i]);
182 processFields(iface[i], lst);
183 processInterfaceFields(iface[i], lst, traversedInterfaces);
196 public static Method
getMethod(Class<?> c, String name, Class[] pt)
throws NoSuchMethodException {
198 return c.getDeclaredMethod(name, pt);
199 }
catch (NoSuchMethodException nme) {
201 if (!c.isInterface())
203 Class<?> s = c.getSuperclass();
206 }
catch (NoSuchMethodException nme) {
208 Class[] iface = c.getInterfaces();
209 for (
int i = 0; i < iface.length; i++)
212 }
catch (NoSuchMethodException nme) {
214 throw new NoSuchMethodException(
"Cannot find method " + name +
" in class " + c.getName());
228 public static Field
getField(Class<?> c, String name)
throws NoSuchFieldException {
230 return c.getDeclaredField(name);
231 }
catch (NoSuchFieldException nfe) {
233 Class[] iface = c.getInterfaces();
234 for (
int i = 0; i < iface.length; i++)
237 }
catch (NoSuchFieldException nme) {
239 if (!c.isInterface())
241 Class s = c.getSuperclass();
244 }
catch (NoSuchFieldException nfe) {
246 throw new NoSuchFieldException(
"Cannot find field " + name +
" in " + c.getName());
259 Class<?> enumType = val.getClass();
260 Field[] f = enumType.getFields();
261 for (
int i = 0; i < f.length; i++) {
262 if (Modifier.isPublic(f[i].getModifiers()) && Modifier.isStatic(f[i].getModifiers())
263 && Modifier.isFinal(f[i].getModifiers())) {
265 if (f[i].
get(
null) == val)
266 return f[i].getName();
267 }
catch (IllegalAccessException iae) {
287 public static <T> T
valueOf(Class<T> cls, String name) {
289 Field field = cls.getField(name);
290 if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())
291 && cls.isAssignableFrom(field.getType()))
292 return (T) field.get(
null);
293 }
catch (NoSuchFieldException nfe) {
294 }
catch (IllegalAccessException iae) {
296 throw new IllegalArgumentException(
"Invalid field name: " + name);
313 Field[] fields = cls.getFields();
315 for (
int i = 0; i < fields.length; i++) {
316 Field field = fields[i];
317 if (field.getName().equalsIgnoreCase(name) && Modifier.isStatic(field.getModifiers())
318 && Modifier.isFinal(field.getModifiers()) && cls.isAssignableFrom(field.getType()))
320 T res2 = (T) field.get(
null);
321 if (res !=
null && res2 !=
null)
322 throw new IllegalArgumentException(
"Found more than one field with the same name in class "
323 + cls.getName() +
" if case is ignored");
325 }
catch (IllegalAccessException iae) {
329 throw new IllegalArgumentException(
"Invalid field name: " + name);