use of java.lang.reflect.AccessibleObject in project android_frameworks_base by crdroidandroid.
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;
}
try {
final Field[] declaredFields = klass.getDeclaredFieldsUnchecked(false);
final ArrayList<Field> foundFields = new ArrayList<Field>();
for (final Field field : declaredFields) {
// Fields which can't be resolved have a null type.
if (field.getType() != null && 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);
} catch (NoClassDefFoundError e) {
throw new AssertionError(e);
}
return fields;
}
use of java.lang.reflect.AccessibleObject in project tika by apache.
the class AnnotationUtils method collectInfo.
/**
* Collects all the fields and methods for an annotation
* @param clazz bean class with annotations
* @param annotation annotation class
* @return list of accessible objects such as fields and methods
*/
private static List<AccessibleObject> collectInfo(Class<?> clazz, Class<? extends Annotation> annotation) {
Class superClazz = clazz;
List<AccessibleObject> members = new ArrayList<>();
List<AccessibleObject> annotatedMembers = new ArrayList<>();
//walk through the inheritance chain
while (superClazz != null && superClazz != Object.class) {
members.addAll(Arrays.asList(superClazz.getDeclaredFields()));
members.addAll(Arrays.asList(superClazz.getDeclaredMethods()));
superClazz = superClazz.getSuperclass();
}
for (final AccessibleObject member : members) {
if (member.isAnnotationPresent(annotation)) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
member.setAccessible(true);
return null;
}
});
annotatedMembers.add(member);
}
}
return annotatedMembers;
}
use of java.lang.reflect.AccessibleObject in project tika by apache.
the class AnnotationUtils method assignFieldParams.
/**
* Assigns the param values to bean
* @throws TikaConfigException when an error occurs while assigning params
*/
public static void assignFieldParams(Object bean, Map<String, Param> params) throws TikaConfigException {
Class<?> beanClass = bean.getClass();
if (!PARAM_INFO.containsKey(beanClass)) {
synchronized (TikaConfig.class) {
if (!PARAM_INFO.containsKey(beanClass)) {
List<AccessibleObject> aObjs = collectInfo(beanClass, org.apache.tika.config.Field.class);
List<ParamField> fields = new ArrayList<>(aObjs.size());
for (AccessibleObject aObj : aObjs) {
fields.add(new ParamField(aObj));
}
PARAM_INFO.put(beanClass, fields);
}
}
}
List<ParamField> fields = PARAM_INFO.get(beanClass);
Set<String> validFieldNames = new HashSet<>();
for (ParamField field : fields) {
validFieldNames.add(field.getName());
Param<?> param = params.get(field.getName());
if (param != null) {
if (field.getType().isAssignableFrom(param.getType())) {
try {
field.assignValue(bean, param.getValue());
} catch (Exception e) {
throw new TikaConfigException(e.getMessage(), e);
}
} else {
String msg = String.format(Locale.ROOT, "Value '%s' of type '%s' cant be" + " assigned to field '%s' of defined type '%s'", param.getValue(), param.getValue().getClass(), field.getName(), field.getType());
throw new TikaConfigException(msg);
}
} else if (field.isRequired()) {
//param not supplied but field is declared as required?
String msg = String.format(Locale.ROOT, "Param %s is required for %s," + " but it is not given in config.", field.getName(), bean.getClass().getName());
throw new TikaConfigException(msg);
} else {
//FIXME: SLF4j is not showing up for import, fix it and send this to LOG.debug
//LOG.debug("Param not supplied, field is not mandatory");
}
}
}
use of java.lang.reflect.AccessibleObject in project android_frameworks_base by AOSPA.
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;
}
try {
final Field[] declaredFields = klass.getDeclaredFieldsUnchecked(false);
final ArrayList<Field> foundFields = new ArrayList<Field>();
for (final Field field : declaredFields) {
// Fields which can't be resolved have a null type.
if (field.getType() != null && 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);
} catch (NoClassDefFoundError e) {
throw new AssertionError(e);
}
return fields;
}
use of java.lang.reflect.AccessibleObject in project leopard by tanhaichao.
the class TopnbInterceptorTest method invoke.
// @Test
// public void getContext() {
// Assert.assertNotNull(TopnbInterceptor.getContext("applicationContext.xml"));
// Assert.assertNotNull(TopnbInterceptor.getContext("applicationContext.xml", false));
// Assert.assertNotNull(TopnbInterceptor.getContext("applicationContext.xml", true));
// }
@Test
public void invoke() throws Throwable {
MethodInvocation invocation = new MethodInvocation() {
@Override
public Object[] getArguments() {
return null;
}
@Override
public AccessibleObject getStaticPart() {
return null;
}
@Override
public Object getThis() {
return TopnbInterceptorTest.this;
}
@Override
public Object proceed() throws Throwable {
return "ok";
}
@Override
public Method getMethod() {
try {
return TopnbInterceptorTest.class.getMethod("invoke");
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
};
Object result = new MethodTimeInterceptor().invoke(invocation);
Assert.assertEquals("ok", result);
}
Aggregations