1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.plus.annotation;
15
16 import java.lang.reflect.Method;
17 import java.lang.reflect.Modifier;
18
19 import org.eclipse.jetty.util.IntrospectionUtil;
20
21
22
23
24
25
26
27
28 public abstract class LifeCycleCallback
29 {
30 public static final Object[] __EMPTY_ARGS = new Object[] {};
31 private Method _target;
32 private Class _targetClass;
33
34
35 public LifeCycleCallback()
36 {
37 }
38
39
40
41
42
43 public Class getTargetClass()
44 {
45 return _targetClass;
46 }
47
48
49
50
51
52 public void setTargetClass(Class clazz)
53 {
54 _targetClass = clazz;
55 }
56
57
58
59
60
61 public Method getTarget()
62 {
63 return _target;
64 }
65
66
67
68
69 public void setTarget(Method target)
70 {
71 this._target = target;
72 }
73
74
75
76
77 public void setTarget (Class clazz, String methodName)
78 {
79 try
80 {
81 Method method = IntrospectionUtil.findMethod(clazz, methodName, null, true, true);
82 validate(clazz, method);
83 _target = method;
84 _targetClass = clazz;
85 }
86 catch (NoSuchMethodException e)
87 {
88 throw new IllegalArgumentException ("Method "+methodName+" not found on class "+clazz.getName());
89 }
90 }
91
92
93
94
95 public void callback (Object instance)
96 throws Exception
97 {
98 if (getTarget() != null)
99 {
100 boolean accessibility = getTarget().isAccessible();
101 getTarget().setAccessible(true);
102 getTarget().invoke(instance, __EMPTY_ARGS);
103 getTarget().setAccessible(accessibility);
104 }
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public Method findMethod (Package pack, Class clazz, String methodName, boolean checkInheritance)
120 {
121 if (clazz == null)
122 return null;
123
124 try
125 {
126 Method method = clazz.getDeclaredMethod(methodName, null);
127 if (checkInheritance)
128 {
129 int modifiers = method.getModifiers();
130 if (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers) || (!Modifier.isPrivate(modifiers)&&(pack.equals(clazz.getPackage()))))
131 return method;
132 else
133 return findMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, true);
134 }
135 return method;
136 }
137 catch (NoSuchMethodException e)
138 {
139 return findMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, true);
140 }
141 }
142
143 public boolean equals (Object o)
144 {
145 if (o==null)
146 return false;
147 if (!(o instanceof LifeCycleCallback))
148 return false;
149 LifeCycleCallback callback = (LifeCycleCallback)o;
150
151 if (callback.getTargetClass()==null)
152 {
153 if (getTargetClass() != null)
154 return false;
155 }
156 else if(!callback.getTargetClass().equals(getTargetClass()))
157 return false;
158 if (callback.getTarget()==null)
159 {
160 if (getTarget() != null)
161 return false;
162 }
163 else if (!callback.getTarget().equals(getTarget()))
164 return false;
165
166 return true;
167 }
168
169 public abstract void validate (Class clazz, Method m);
170 }