01: /*
02: * ReflectionUtil.java
03: *
04: * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lang;
10:
11: import java.lang.reflect.Method;
12: import java.lang.reflect.Modifier;
13: import java.util.HashSet;
14: import java.util.Set;
15: import pnuts.compiler.ClassFile;
16:
17: public class ReflectionUtil {
18:
19: public static Method[] getInheritableMethods(Class cls) {
20: Set s = new HashSet(); // signatures
21: Set m = new HashSet(); // methods
22: Class c = cls;
23: while (c != null) {
24: getInheritableMethods(c, s, m);
25: c = c.getSuperclass();
26: }
27: Method[] results = new Method[m.size()];
28: return (Method[]) m.toArray(results);
29: }
30:
31: static void getInheritableMethods(Class cls, Set signatures,
32: Set methods) {
33: Method _methods[] = cls.getDeclaredMethods();
34: for (int j = 0; j < _methods.length; j++) {
35: Method m = _methods[j];
36: int modifiers = m.getModifiers();
37: if (!Modifier.isPublic(modifiers)
38: && !Modifier.isProtected(modifiers)
39: || Modifier.isStatic(modifiers)
40: || Modifier.isFinal(modifiers)) {
41: continue;
42: }
43: String sig = m.getName()
44: + ClassFile.signature(m.getParameterTypes());
45: if (signatures.add(sig)) {
46: methods.add(m);
47: }
48: }
49: Class[] interfaces = cls.getInterfaces();
50: for (int j = 0; j < interfaces.length; j++) {
51: getInheritableMethods(interfaces[j], signatures, methods);
52: }
53: }
54: }
|