Search in sources :

Example 26 with AccessibleObject

use of java.lang.reflect.AccessibleObject in project j2objc by google.

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 27 with AccessibleObject

use of java.lang.reflect.AccessibleObject in project j2objc by google.

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 28 with AccessibleObject

use of java.lang.reflect.AccessibleObject in project j2objc by google.

the class AccessibleObjectTest method test_isAccessible.

/**
     * java.lang.reflect.AccessibleObject#isAccessible()
     */
public void test_isAccessible() {
    // java.lang.reflect.AccessibleObject.isAccessible()
    try {
        AccessibleObject ao = TestClass.class.getField("aField");
        ao.setAccessible(true);
        assertTrue("Returned false to isAccessible", ao.isAccessible());
        ao.setAccessible(false);
        assertTrue("Returned true to isAccessible", !ao.isAccessible());
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
}
Also used : AccessibleObject(java.lang.reflect.AccessibleObject)

Example 29 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 30 with AccessibleObject

use of java.lang.reflect.AccessibleObject in project android_frameworks_base by ParanoidAndroid.

the class ViewDebug method getExportedPropertyFields.

private static Field[] getExportedPropertyFields(Class<?> klass) {
    if (sFieldsForClasses == null) {
        sFieldsForClasses = new HashMap<Class<?>, Field[]>();
    }
    if (sAnnotations == null) {
        sAnnotations = new HashMap<AccessibleObject, ExportedProperty>(512);
    }
    final HashMap<Class<?>, Field[]> map = sFieldsForClasses;
    Field[] fields = map.get(klass);
    if (fields != null) {
        return fields;
    }
    final ArrayList<Field> foundFields = new ArrayList<Field>();
    fields = klass.getDeclaredFields();
    int count = fields.length;
    for (int i = 0; i < count; i++) {
        final Field field = fields[i];
        if (field.isAnnotationPresent(ExportedProperty.class)) {
            field.setAccessible(true);
            foundFields.add(field);
            sAnnotations.put(field, field.getAnnotation(ExportedProperty.class));
        }
    }
    fields = foundFields.toArray(new Field[foundFields.size()]);
    map.put(klass, fields);
    return fields;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) AccessibleObject(java.lang.reflect.AccessibleObject)

Aggregations

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