Search in sources :

Example 21 with AccessibleObject

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

Example 22 with AccessibleObject

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

Example 23 with AccessibleObject

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");
        }
    }
}
Also used : ParamField(org.apache.tika.config.ParamField) TikaConfig(org.apache.tika.config.TikaConfig) ArrayList(java.util.ArrayList) TikaConfigException(org.apache.tika.exception.TikaConfigException) TikaConfigException(org.apache.tika.exception.TikaConfigException) AccessibleObject(java.lang.reflect.AccessibleObject) HashSet(java.util.HashSet)

Example 24 with AccessibleObject

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

Example 25 with AccessibleObject

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);
}
Also used : MethodTimeInterceptor(io.leopard.topnb.methodtime.MethodTimeInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AccessibleObject(java.lang.reflect.AccessibleObject) Test(org.junit.Test) PerformanceUtilTest(io.leopard.topnb.methodtime.PerformanceUtilTest)

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