KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > util > RtHelper


1 package spoon.support.util;
2
3 import java.lang.reflect.Field JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.lang.reflect.Modifier JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Set JavaDoc;
10 import java.util.TreeSet JavaDoc;
11
12 import spoon.reflect.code.CtExpression;
13 import spoon.reflect.code.CtInvocation;
14 import spoon.reflect.code.CtLiteral;
15 import spoon.reflect.declaration.ModifierKind;
16 import spoon.reflect.reference.CtTypeReference;
17
18 /**
19  * This class is a helper for runtime reflection.
20  */

21 public abstract class RtHelper {
22
23     private RtHelper() {
24     }
25
26     /**
27      * Gets all the runtime fields for a given class (including the
28      * superclasses').
29      */

30     public static Field JavaDoc[] getAllFields(Class JavaDoc<?> c) {
31         List JavaDoc<Field JavaDoc> fields = new ArrayList JavaDoc<Field JavaDoc>();
32         while (c != null && c != Object JavaDoc.class) {
33             for (Field JavaDoc f : c.getDeclaredFields()) {
34                 fields.add(f);
35             }
36             // fields.addAll(Arrays.asList(c.getDeclaredFields()));
37
c = c.getSuperclass();
38         }
39         Field JavaDoc[] result = new Field JavaDoc[fields.size()];
40         return fields.toArray(result);
41     }
42
43     /**
44      * Gets all the runtime methods for a given class or interface (including
45      * the superclasses' or interfaces').
46      */

47     public static Method JavaDoc[] getAllMethods(Class JavaDoc<?> c) {
48         List JavaDoc<Method JavaDoc> methods = new ArrayList JavaDoc<Method JavaDoc>();
49         if (c.isInterface()) {
50             getAllIMethods(c, methods);
51         } else {
52             while (c != null && c != Object JavaDoc.class) {
53                 for (Method JavaDoc m : c.getDeclaredMethods())
54                     methods.add(m);
55                 // methods.addAll(Arrays.asList(c.getDeclaredMethods()));
56
c = c.getSuperclass();
57             }
58         }
59         Method JavaDoc[] result = new Method JavaDoc[methods.size()];
60         return methods.toArray(result);
61     }
62
63     private static void getAllIMethods(Class JavaDoc<?> c, List JavaDoc<Method JavaDoc> methods) {
64         for (Method JavaDoc m : c.getDeclaredMethods())
65             methods.add(m);
66         for (Class JavaDoc i : c.getInterfaces()) {
67             getAllIMethods(i, methods);
68         }
69     }
70
71     /**
72      * Actually invokes from a compile-time invocation (by using runtime
73      * reflection).
74      */

75     @SuppressWarnings JavaDoc("unchecked")
76     public static <T> T invoke(CtInvocation<T> i) throws NoSuchMethodException JavaDoc,
77             IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
78         Object JavaDoc target = i.getTarget() == null ? null : ((CtLiteral<?>) i
79                 .getTarget()).getValue();
80         List JavaDoc<Object JavaDoc> args = new ArrayList JavaDoc<Object JavaDoc>();
81         for (CtExpression e : i.getArguments()) {
82             args.add(((CtLiteral<?>) e).getValue());
83         }
84         Class JavaDoc<?> c = i.getExecutable().getDeclaringType().getActualClass();
85         ArrayList JavaDoc<Class JavaDoc<?>> argTypes = new ArrayList JavaDoc<Class JavaDoc<?>>();
86         for (CtTypeReference<?> type : i.getExecutable().getParameterTypes()) {
87             argTypes.add(type.getActualClass());
88         }
89         return (T) c.getMethod(i.getExecutable().getSimpleName(),
90                 argTypes.toArray(new Class JavaDoc[argTypes.size()])).invoke(target,
91                 args.toArray());
92     }
93
94     /**
95      * Return the set of modifiers defined by the modifiers integer
96      * (java.lang.reflect).
97      */

98     public static Set JavaDoc<ModifierKind> getModifiers(int mod) {
99         Set JavaDoc<ModifierKind> set = new TreeSet JavaDoc<ModifierKind>();
100         if (Modifier.isAbstract(mod)) {
101             set.add(ModifierKind.ABSTRACT);
102         }
103         if (Modifier.isFinal(mod)) {
104             set.add(ModifierKind.FINAL);
105         }
106         if (Modifier.isNative(mod)) {
107             set.add(ModifierKind.NATIVE);
108         }
109         if (Modifier.isPrivate(mod)) {
110             set.add(ModifierKind.PRIVATE);
111         }
112         if (Modifier.isProtected(mod)) {
113             set.add(ModifierKind.PROTECTED);
114         }
115         if (Modifier.isPublic(mod)) {
116             set.add(ModifierKind.PUBLIC);
117         }
118         if (Modifier.isStatic(mod)) {
119             set.add(ModifierKind.STATIC);
120         }
121         if (Modifier.isStrict(mod)) {
122             set.add(ModifierKind.STRICTFP);
123         }
124         if (Modifier.isSynchronized(mod)) {
125             set.add(ModifierKind.SYNCHRONIZED);
126         }
127         if (Modifier.isTransient(mod)) {
128             set.add(ModifierKind.TRANSIENT);
129         }
130         if (Modifier.isVolatile(mod)) {
131             set.add(ModifierKind.VOLATILE);
132         }
133         return set;
134     }
135
136 }
137
Popular Tags