001: package U2.T2.Reflection;
002:
003: import java.util.*;
004: import java.lang.reflect.*;
005:
006: /**
007: * Containing some utility methods related to reflection.
008: */
009: public class ReflUtil {
010:
011: /**
012: * To obtain all non-interface superclasses of a given class. The
013: * class "Object" is not included in the returned list.
014: */
015: public static List<Class> getAllSuperClasses(Class C) {
016: List<Class> collected = new LinkedList<Class>();
017: if (C.isInterface() || C.getName().equals("java.lang.Object"))
018: return collected;
019: getAllSuperClasses_worker(C.getSuperclass(), collected);
020: return collected;
021: }
022:
023: // the working method for getAllSuperClasses:
024: private static void getAllSuperClasses_worker(Class C,
025: List<Class> collected) {
026: if (C.isInterface() || C.getName().equals("java.lang.Object"))
027: return;
028: getAllSuperClasses_worker(C.getSuperclass(), collected);
029: collected.add(C);
030: }
031:
032: /**
033: * Get the public constructors of a given class.
034: */
035: public static List<Constructor> getPubCons(Class C) {
036: List<Constructor> result = new LinkedList<Constructor>();
037: Constructor[] cons = C.getDeclaredConstructors();
038: for (int i = 0; i < cons.length; i++)
039: if (Modifier.isPublic(cons[i].getModifiers()))
040: result.add(cons[i]);
041: return result;
042: }
043:
044: /**
045: * Check if a method m can accept an object of class C as a
046: * parameter.
047: */
048: public static boolean canAcceptAsParameter(Class C, Method m) {
049:
050: Class[] paramTypes = m.getParameterTypes();
051: boolean found = false;
052: for (int i = 0; i < paramTypes.length && !found; i++)
053:
054: found = paramTypes[i].isAssignableFrom(C);
055:
056: return found;
057: }
058:
059: /**
060: * Remove all non-public methods from a given list of methods.
061: */
062: public static void removeNonPubMethods(List<Method> ms) {
063: LinkedList<Method> toBeRemoved = new LinkedList<Method>();
064: for (Method m : ms)
065: if (!Modifier.isPublic(m.getModifiers()))
066: toBeRemoved.add(m);
067: ms.removeAll(toBeRemoved);
068: }
069:
070: /**
071: * Remove all non-public constructors from a given list of
072: * constructors.
073: */
074: public static void removeNonPubCons(List<Constructor> cs) {
075: LinkedList<Constructor> toBeRemoved = new LinkedList<Constructor>();
076: for (Constructor c : cs)
077: if (!Modifier.isPublic(c.getModifiers()))
078: toBeRemoved.add(c);
079: cs.removeAll(toBeRemoved);
080: }
081:
082: /**
083: * Remove all non-public fields from a given list of fields.
084: */
085: public static void removeNonPubFields(List<Field> fs) {
086: LinkedList<Field> toBeRemoved = new LinkedList<Field>();
087: for (Field f : fs)
088: if (!Modifier.isPublic(f.getModifiers()))
089: toBeRemoved.add(f);
090: fs.removeAll(toBeRemoved);
091: }
092:
093: /**
094: * Remove from a given list of methods, all methods that are not
095: * declared by a given class C.
096: */
097: public static void removeNonCMethods(List<Method> ms, Class C) {
098: LinkedList<Method> toBeRemoved = new LinkedList<Method>();
099: for (Method m : ms)
100: if (m.getDeclaringClass() != C)
101: toBeRemoved.add(m);
102: ms.removeAll(toBeRemoved);
103: }
104:
105: /**
106: * Remove from a given list of fields, all fields that are not
107: * declared by a given class C.
108: */
109: public static void removeNonCFields(List<Field> fs, Class C) {
110: LinkedList<Field> toBeRemoved = new LinkedList<Field>();
111: for (Field f : fs)
112: if (f.getDeclaringClass() != C)
113: toBeRemoved.add(f);
114: fs.removeAll(toBeRemoved);
115: }
116:
117: }
|