Search in sources :

Example 41 with AccessibleObject

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;
        }
    };
}
Also used : StreamCache(org.apache.camel.StreamCache) Expression(org.apache.camel.Expression) AsyncCallback(org.apache.camel.AsyncCallback) AccessibleObject(java.lang.reflect.AccessibleObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) Callable(java.util.concurrent.Callable) CompletionStage(java.util.concurrent.CompletionStage)

Example 42 with AccessibleObject

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());
}
Also used : AccessibleObject(java.lang.reflect.AccessibleObject)

Example 43 with AccessibleObject

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);
}
Also used : AccessibleObject(java.lang.reflect.AccessibleObject)

Example 44 with AccessibleObject

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));
}
Also used : AccessibleObject(java.lang.reflect.AccessibleObject) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Example 45 with AccessibleObject

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);
}
Also used : Finder(com.google.inject.persist.finder.Finder) DynamicFinder(com.google.inject.persist.finder.DynamicFinder) AccessibleObject(java.lang.reflect.AccessibleObject) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Aggregations

AccessibleObject (java.lang.reflect.AccessibleObject)54 ArrayList (java.util.ArrayList)17 Method (java.lang.reflect.Method)14 Field (java.lang.reflect.Field)9 Annotation (java.lang.annotation.Annotation)8 PropertyModel (org.qi4j.runtime.property.PropertyModel)6 HashSet (java.util.HashSet)5 MethodInvocation (org.aopalliance.intercept.MethodInvocation)4 Property (org.qi4j.api.property.Property)4 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 BuilderEntityState (org.qi4j.runtime.unitofwork.BuilderEntityState)3 DynamicFinder (com.google.inject.persist.finder.DynamicFinder)2 Finder (com.google.inject.persist.finder.Finder)2 Constructor (java.lang.reflect.Constructor)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 DeploymentReflectionIndex (org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex)2 ResourceInjectionTargetMetaData (org.jboss.metadata.javaee.spec.ResourceInjectionTargetMetaData)2 Module (org.jboss.modules.Module)2