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);
}
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());
}
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());
}
}
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 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;
}
Aggregations