1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.spring.util; |
18 |
|
|
19 |
|
import java.lang.annotation.Annotation; |
20 |
|
import java.lang.reflect.Field; |
21 |
|
import java.lang.reflect.InvocationTargetException; |
22 |
|
import java.lang.reflect.Method; |
23 |
|
import java.lang.reflect.Modifier; |
24 |
|
|
25 |
0 |
public class ReflectionUtils extends org.springframework.util.ReflectionUtils { |
26 |
|
public static <T extends Annotation> void callLifecycleMethod(final Object bean, final Class<T> annotation) { |
27 |
0 |
ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() { |
28 |
0 |
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { |
29 |
0 |
if (method.getAnnotation(annotation) != null) { |
30 |
|
try { |
31 |
0 |
method.invoke(bean, (Object[])null); |
32 |
0 |
} catch (IllegalArgumentException ex) { |
33 |
0 |
throw new IllegalStateException("Failure to invoke " + method + " on " + bean.getClass() + ": args=[]", ex); |
34 |
0 |
} catch (IllegalAccessException ex) { |
35 |
0 |
throw new UnsupportedOperationException(ex.toString()); |
36 |
0 |
} catch (InvocationTargetException ex) { |
37 |
0 |
throw new UnsupportedOperationException("PostConstruct method on bean threw exception", ex.getTargetException()); |
38 |
0 |
} |
39 |
|
} |
40 |
0 |
} |
41 |
|
}); |
42 |
0 |
} |
43 |
|
|
44 |
|
public static void setField(Field f, Object instance, Object value) { |
45 |
|
try { |
46 |
8 |
boolean oldAccessible = f.isAccessible(); |
47 |
8 |
boolean shouldSetAccessible = !Modifier.isPublic(f.getModifiers()) && !oldAccessible; |
48 |
8 |
if (shouldSetAccessible) { |
49 |
8 |
f.setAccessible(true); |
50 |
|
} |
51 |
8 |
f.set(instance, value); |
52 |
8 |
if (shouldSetAccessible) { |
53 |
8 |
f.setAccessible(oldAccessible); |
54 |
|
} |
55 |
0 |
} catch (IllegalArgumentException ex) { |
56 |
0 |
throw new UnsupportedOperationException("Cannot inject value of class '" + value.getClass() + "' into " + f); |
57 |
0 |
} catch (IllegalAccessException ex) { |
58 |
0 |
ReflectionUtils.handleReflectionException(ex); |
59 |
8 |
} |
60 |
8 |
} |
61 |
|
} |