| 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 |
|
} |
| 33 |
0 |
catch (IllegalArgumentException ex) { |
| 34 |
0 |
throw new IllegalStateException("Failure to invoke " + method + " on " |
| 35 |
|
+ bean.getClass() + ": args=[]", ex); |
| 36 |
|
} |
| 37 |
0 |
catch (IllegalAccessException ex) { |
| 38 |
0 |
throw new UnsupportedOperationException(ex.toString()); |
| 39 |
|
} |
| 40 |
0 |
catch (InvocationTargetException ex) { |
| 41 |
0 |
throw new UnsupportedOperationException("PostConstruct method on bean threw exception", |
| 42 |
|
ex.getTargetException()); |
| 43 |
0 |
} |
| 44 |
|
} |
| 45 |
0 |
} |
| 46 |
|
}); |
| 47 |
0 |
} |
| 48 |
|
|
| 49 |
|
public static void setField(Field f, Object instance, Object value) { |
| 50 |
|
try { |
| 51 |
8 |
boolean oldAccessible = f.isAccessible(); |
| 52 |
8 |
boolean shouldSetAccessible = !Modifier.isPublic(f.getModifiers()) && !oldAccessible; |
| 53 |
8 |
if (shouldSetAccessible) { |
| 54 |
8 |
f.setAccessible(true); |
| 55 |
|
} |
| 56 |
8 |
f.set(instance, value); |
| 57 |
8 |
if (shouldSetAccessible) { |
| 58 |
8 |
f.setAccessible(oldAccessible); |
| 59 |
|
} |
| 60 |
|
} |
| 61 |
0 |
catch (IllegalArgumentException ex) { |
| 62 |
0 |
throw new UnsupportedOperationException("Cannot inject value of class '" |
| 63 |
|
+ value.getClass() + "' into " + f); |
| 64 |
|
} |
| 65 |
0 |
catch (IllegalAccessException ex) { |
| 66 |
0 |
ReflectionUtils.handleReflectionException(ex); |
| 67 |
8 |
} |
| 68 |
8 |
} |
| 69 |
|
} |