use of java.lang.reflect.AccessibleObject in project camel by apache.
the class MethodInfo method createMethodInvocation.
public MethodInvocation createMethodInvocation(final Object pojo, final Exchange exchange) {
final Object[] arguments = parametersExpression.evaluate(exchange, Object[].class);
return new MethodInvocation() {
public Method getMethod() {
return method;
}
public Object[] getArguments() {
return arguments;
}
public boolean proceed(AsyncCallback callback) {
Object body = exchange.getIn().getBody();
if (body != null && body instanceof StreamCache) {
// ensure the stream cache is reset before calling the method
((StreamCache) body).reset();
}
try {
return doProceed(callback);
} catch (InvocationTargetException e) {
exchange.setException(e.getTargetException());
callback.done(true);
return true;
} catch (Throwable e) {
exchange.setException(e);
callback.done(true);
return true;
}
}
private boolean doProceed(AsyncCallback callback) throws Exception {
// dynamic router should be invoked beforehand
if (dynamicRouter != null) {
if (!dynamicRouter.isStarted()) {
ServiceHelper.startService(dynamicRouter);
}
// use a expression which invokes the method to be used by dynamic router
Expression expression = new DynamicRouterExpression(pojo);
return dynamicRouter.doRoutingSlip(exchange, expression, callback);
}
// invoke pojo
if (LOG.isTraceEnabled()) {
LOG.trace(">>>> invoking: {} on bean: {} with arguments: {} for exchange: {}", new Object[] { method, pojo, asString(arguments), exchange });
}
Object result = invoke(method, pojo, arguments, exchange);
// the method may be a closure or chained method returning a callable which should be called
if (result instanceof Callable) {
LOG.trace("Method returned Callback which will be called: {}", result);
Object callableResult = ((Callable) result).call();
if (callableResult != null) {
result = callableResult;
} else {
// if callable returned null we should not change the body
result = Void.TYPE;
}
}
if (recipientList != null) {
// ensure its started
if (!recipientList.isStarted()) {
ServiceHelper.startService(recipientList);
}
return recipientList.sendToRecipientList(exchange, result, callback);
}
if (routingSlip != null) {
if (!routingSlip.isStarted()) {
ServiceHelper.startService(routingSlip);
}
return routingSlip.doRoutingSlip(exchange, result, callback);
}
//If it's Java 8 async result
if (CompletionStage.class.isAssignableFrom(getMethod().getReturnType())) {
CompletionStage<?> completionStage = (CompletionStage<?>) result;
completionStage.whenComplete((resultObject, e) -> {
if (e != null) {
exchange.setException(e);
} else if (resultObject != null) {
fillResult(exchange, resultObject);
}
callback.done(false);
});
return false;
}
// if the method returns something then set the value returned on the Exchange
if (!getMethod().getReturnType().equals(Void.TYPE) && result != Void.TYPE) {
fillResult(exchange, result);
}
// we did not use any of the eips, but just invoked the bean
// so notify the callback we are done synchronously
callback.done(true);
return true;
}
public Object getThis() {
return pojo;
}
public AccessibleObject getStaticPart() {
return method;
}
};
}
use of java.lang.reflect.AccessibleObject in project robovm by robovm.
the class AccessibleObjectTest method test_setAccessible.
/**
* java.lang.reflect.AccessibleObject#setAccessible(boolean)
*/
public void test_setAccessible() throws Exception {
AccessibleObject ao = TestClass.class.getField("aField");
ao.setAccessible(true);
assertTrue("Returned false to isAccessible", ao.isAccessible());
ao.setAccessible(false);
assertFalse("Returned true to isAccessible", ao.isAccessible());
}
use of java.lang.reflect.AccessibleObject in project robovm by robovm.
the class AccessibleObjectTest method test_isAnnotationPresent.
public void test_isAnnotationPresent() throws Exception {
AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
assertTrue("Missing @AnnotationRuntime0", ao.isAnnotationPresent(AnnotationRuntime0.class));
assertFalse("AnnotationSource0 should not be visible at runtime", ao.isAnnotationPresent(AnnotationSource0.class));
boolean npeThrown = false;
try {
ao.isAnnotationPresent(null);
fail("NPE expected");
} catch (NullPointerException e) {
npeThrown = true;
}
assertTrue("NPE expected", npeThrown);
}
use of java.lang.reflect.AccessibleObject in project robovm by robovm.
the class AccessibleObjectTest method test_getAnnotations.
public void test_getAnnotations() throws Exception {
AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
Annotation[] annotations = ao.getAnnotations();
assertEquals(2, annotations.length);
Set<Class<?>> ignoreOrder = new HashSet<Class<?>>();
ignoreOrder.add(annotations[0].annotationType());
ignoreOrder.add(annotations[1].annotationType());
assertTrue("Missing @AnnotationRuntime0", ignoreOrder.contains(AnnotationRuntime0.class));
assertTrue("Missing @AnnotationRuntime1", ignoreOrder.contains(AnnotationRuntime1.class));
}
use of java.lang.reflect.AccessibleObject in project roboguice by roboguice.
the class JpaPersistModule method bindFinder.
private <T> void bindFinder(Class<T> iface) {
if (!isDynamicFinderValid(iface)) {
return;
}
InvocationHandler finderInvoker = new InvocationHandler() {
@Inject
JpaFinderProxy finderProxy;
public Object invoke(final Object thisObject, final Method method, final Object[] args) throws Throwable {
// Don't intercept non-finder methods like equals and hashcode.
if (!method.isAnnotationPresent(Finder.class)) {
// and hashcode as a proxy (!) for the proxy's equals and hashcode.
return method.invoke(this, args);
}
return finderProxy.invoke(new MethodInvocation() {
public Method getMethod() {
return method;
}
public Object[] getArguments() {
return null == args ? new Object[0] : args;
}
public Object proceed() throws Throwable {
return method.invoke(thisObject, args);
}
public Object getThis() {
throw new UnsupportedOperationException("Bottomless proxies don't expose a this.");
}
public AccessibleObject getStaticPart() {
throw new UnsupportedOperationException();
}
});
}
};
requestInjection(finderInvoker);
// Proxy must produce instance of type given.
@SuppressWarnings("unchecked") T proxy = (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { iface }, finderInvoker);
bind(iface).toInstance(proxy);
}
Aggregations