1 package org.kite9.framework.common;
2
3 import java.lang.annotation.Annotation;
4 import java.lang.reflect.Method;
5
6 import org.kite9.framework.Kite9Item;
7
8 public class StackHelp {
9
10 /***
11 * Finds the method corresponding to the {@link Kite9Item}.
12 */
13 public static Method getKite9Item() {
14 return getAnnotatedMethod(Kite9Item.class);
15 }
16
17 public static <X extends Annotation> Method getAnnotatedMethod(Class<X> ann) {
18 StackTraceElement[] elems = Thread.currentThread().getStackTrace();
19 for (int i = 0; i < elems.length; i++) {
20 StackTraceElement e = elems[i];
21 String name = e.getClassName();
22 String methodName = e.getMethodName();
23 Method m = getMethod(name, methodName);
24 if (m != null) {
25 if (m.getAnnotation(ann) != null) {
26 return m;
27 }
28 }
29 }
30
31 throw new StackSearchException(ann);
32 }
33
34 private static Method getMethod(String name, String methodName) {
35 try {
36 Class<?> cl = null;
37 try {
38 cl = Class.forName(name);
39 } catch (Exception e) {
40 }
41
42 if (cl==null) {
43 cl = Thread.currentThread().getContextClassLoader().loadClass(name);
44 }
45
46 Method m = cl.getDeclaredMethod(methodName, new Class[] {});
47 return m;
48 } catch (Exception e) {
49 return null;
50 }
51 }
52
53 }