Search in sources :

Example 11 with AccessibleObject

use of java.lang.reflect.AccessibleObject in project platform_frameworks_base by android.

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

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

the class AccessibleObjectTest method test_getDeclaredAnnotations.

public void test_getDeclaredAnnotations() throws Exception {
    AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
    Annotation[] annotations = ao.getDeclaredAnnotations();
    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 13 with AccessibleObject

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

the class AccessibleObjectTest method test_getAnnotation.

public void test_getAnnotation() throws Exception {
    AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
    //test error case
    boolean npeThrown = false;
    try {
        ao.getAnnotation(null);
        fail("NPE expected");
    } catch (NullPointerException e) {
        npeThrown = true;
    }
    assertTrue("NPE expected", npeThrown);
    //test inherited on method has no effect
    InheritedRuntime ir = ao.getAnnotation(InheritedRuntime.class);
    assertNull("Inherited Annotations should have no effect", ir);
    //test ordinary runtime annotation
    AnnotationRuntime0 rt0 = ao.getAnnotation(AnnotationRuntime0.class);
    assertNotNull("AnnotationRuntime0 instance expected", rt0);
}
Also used : AccessibleObject(java.lang.reflect.AccessibleObject)

Example 14 with AccessibleObject

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

the class AccessibleObjectTest method test_setAccessible$Ljava_lang_reflect_AccessibleObjectZ.

/**
     * java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[],
     *        boolean)
     */
public void test_setAccessible$Ljava_lang_reflect_AccessibleObjectZ() {
    try {
        AccessibleObject ao = TestClass.class.getField("aField");
        AccessibleObject[] aoa = new AccessibleObject[] { ao };
        AccessibleObject.setAccessible(aoa, true);
        assertTrue("Returned false to isAccessible", ao.isAccessible());
        AccessibleObject.setAccessible(aoa, 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 15 with AccessibleObject

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

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)

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