Search in sources :

Example 91 with BeanInfo

use of java.beans.BeanInfo in project apex-core by apache.

the class AppDataPushAgent method extractFields.

private JSONObject extractFields(Object o) {
    List<Field> fields;
    Map<String, Method> methods;
    if (cacheFields.containsKey(o.getClass())) {
        fields = cacheFields.get(o.getClass());
    } else {
        fields = new ArrayList<>();
        for (Class<?> c = o.getClass(); c != Object.class; c = c.getSuperclass()) {
            Field[] declaredFields = c.getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                AutoMetric rfa = field.getAnnotation(AutoMetric.class);
                if (rfa != null) {
                    field.setAccessible(true);
                    try {
                        fields.add(field);
                    } catch (Exception ex) {
                        LOG.debug("Error extracting fields for app data: {}. Ignoring.", ex.getMessage());
                    }
                }
            }
        }
        cacheFields.put(o.getClass(), fields);
    }
    JSONObject result = new JSONObject();
    for (Field field : fields) {
        try {
            result.put(field.getName(), field.get(o));
        } catch (Exception ex) {
        // ignore
        }
    }
    if (cacheGetMethods.containsKey(o.getClass())) {
        methods = cacheGetMethods.get(o.getClass());
    } else {
        methods = new HashMap<>();
        try {
            BeanInfo info = Introspector.getBeanInfo(o.getClass());
            for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
                Method method = pd.getReadMethod();
                if (pd.getReadMethod() != null) {
                    AutoMetric rfa = method.getAnnotation(AutoMetric.class);
                    if (rfa != null) {
                        methods.put(pd.getName(), method);
                    }
                }
            }
        } catch (IntrospectionException ex) {
        // ignore
        }
        cacheGetMethods.put(o.getClass(), methods);
    }
    for (Map.Entry<String, Method> entry : methods.entrySet()) {
        try {
            result.put(entry.getKey(), entry.getValue().invoke(o));
        } catch (Exception ex) {
        // ignore
        }
    }
    return result;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) AutoMetric(com.datatorrent.api.AutoMetric) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) JSONException(org.codehaus.jettison.json.JSONException) Field(java.lang.reflect.Field) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 92 with BeanInfo

use of java.beans.BeanInfo in project querydsl by querydsl.

the class QBean method initMethods.

private List<Method> initMethods(Map<String, ? extends Expression<?>> args) {
    try {
        List<Method> methods = new ArrayList<Method>(args.size());
        BeanInfo beanInfo = Introspector.getBeanInfo(getType());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (Map.Entry<String, ? extends Expression<?>> entry : args.entrySet()) {
            String property = entry.getKey();
            Expression<?> expr = entry.getValue();
            Method setter = null;
            for (PropertyDescriptor prop : propertyDescriptors) {
                if (prop.getName().equals(property)) {
                    setter = prop.getWriteMethod();
                    if (!isAssignableFrom(prop.getPropertyType(), expr.getType())) {
                        typeMismatch(prop.getPropertyType(), expr);
                    }
                    break;
                }
            }
            if (setter == null) {
                propertyNotFound(expr, property);
            }
            methods.add(setter);
        }
        return methods;
    } catch (IntrospectionException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 93 with BeanInfo

use of java.beans.BeanInfo in project goci by EBISPOT.

the class EmbeddableDocument method embed.

public void embed(Document document) {
    try {
        Set<String> excludeNames = new HashSet<>();
        BeanInfo objectInfo = Introspector.getBeanInfo(Document.class);
        for (PropertyDescriptor descriptor : objectInfo.getPropertyDescriptors()) {
            excludeNames.add(descriptor.getName());
        }
        try {
            BeanInfo docInfo = Introspector.getBeanInfo(document.getClass());
            for (PropertyDescriptor descriptor : docInfo.getPropertyDescriptors()) {
                if (!excludeNames.contains(descriptor.getName())) {
                    boolean isExcluded = false;
                    Method readMethod = descriptor.getReadMethod();
                    readMethod.setAccessible(true);
                    if (readMethod.isAnnotationPresent(NonEmbeddableField.class)) {
                        isExcluded = true;
                    } else {
                        try {
                            Field field = document.getClass().getDeclaredField(descriptor.getName());
                            field.setAccessible(true);
                            if (field.isAnnotationPresent(NonEmbeddableField.class)) {
                                isExcluded = true;
                            }
                        } catch (NoSuchFieldException e) {
                        // no field with this name, skip
                        }
                    }
                    if (!isExcluded) {
                        try {
                            // determine method to update this document with doc being embedded
                            Method propertyAdderMethod = findPropertyAdder(descriptor);
                            // invoke read method on passed document
                            Object fieldToEmbed = descriptor.getReadMethod().invoke(document);
                            propertyAdderMethod.invoke(this, fieldToEmbed);
                        } catch (InvocationTargetException | IllegalAccessException e) {
                            throw new DocumentEmbeddingException("Failed to read property '" + descriptor.getName() + "'", e);
                        }
                    }
                }
            }
        } catch (IntrospectionException e) {
            throw new DocumentEmbeddingException("Failed to analyse document in preparation for embedding", e);
        }
    } catch (IntrospectionException e) {
        throw new DocumentEmbeddingException("Failed to read Object.class when determining which properties to exclude", e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) DocumentEmbeddingException(uk.ac.ebi.spot.goci.exception.DocumentEmbeddingException) HashSet(java.util.HashSet)

Example 94 with BeanInfo

use of java.beans.BeanInfo in project goci by EBISPOT.

the class EmbeddableDocumentTest method testIntrospection.

/*@Test
    public void testEmbedTrait() {
        try {
            traitDoc.embed(associationDoc);
        }
        catch (DocumentEmbeddingException e) {
            e.printStackTrace();
            fail();
        }
    }*/
@Test
public void testIntrospection() {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(studyDoc.getClass());
        for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
            System.out.println("Display name: " + pd.getDisplayName());
            System.out.println("Name: " + pd.getName());
            System.out.println("Read method: " + pd.getReadMethod());
            System.out.println("\t" + pd);
        }
    } catch (IntrospectionException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Test(org.junit.Test)

Example 95 with BeanInfo

use of java.beans.BeanInfo in project groovy-core by groovy.

the class MetaClassImpl method addProperties.

private void addProperties() {
    BeanInfo info;
    final Class stopClass;
    // introspect
    try {
        if (isBeanDerivative(theClass)) {
            info = (BeanInfo) AccessController.doPrivileged(new PrivilegedExceptionAction() {

                public Object run() throws IntrospectionException {
                    return Introspector.getBeanInfo(theClass, Introspector.IGNORE_ALL_BEANINFO);
                }
            });
        } else {
            info = (BeanInfo) AccessController.doPrivileged(new PrivilegedExceptionAction() {

                public Object run() throws IntrospectionException {
                    return Introspector.getBeanInfo(theClass);
                }
            });
        }
    } catch (PrivilegedActionException pae) {
        throw new GroovyRuntimeException("exception during bean introspection", pae.getException());
    }
    PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
    // build up the metaproperties based on the public fields, property descriptors,
    // and the getters and setters
    setupProperties(descriptors);
    EventSetDescriptor[] eventDescriptors = info.getEventSetDescriptors();
    for (EventSetDescriptor descriptor : eventDescriptors) {
        Method[] listenerMethods = descriptor.getListenerMethods();
        for (Method listenerMethod : listenerMethods) {
            final MetaMethod metaMethod = CachedMethod.find(descriptor.getAddListenerMethod());
            // we skip that here
            if (metaMethod == null)
                continue;
            addToAllMethodsIfPublic(metaMethod);
            String name = listenerMethod.getName();
            if (listeners.containsKey(name)) {
                listeners.put(name, AMBIGUOUS_LISTENER_METHOD);
            } else {
                listeners.put(name, metaMethod);
            }
        }
    }
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) PropertyDescriptor(java.beans.PropertyDescriptor) PrivilegedActionException(java.security.PrivilegedActionException) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) Method(java.lang.reflect.Method) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) EventSetDescriptor(java.beans.EventSetDescriptor) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Aggregations

BeanInfo (java.beans.BeanInfo)420 PropertyDescriptor (java.beans.PropertyDescriptor)328 Method (java.lang.reflect.Method)217 SimpleBeanInfo (java.beans.SimpleBeanInfo)167 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)161 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)148 IntrospectionException (java.beans.IntrospectionException)115 InvocationTargetException (java.lang.reflect.InvocationTargetException)38 HashMap (java.util.HashMap)33 Test (org.junit.jupiter.api.Test)33 ArrayList (java.util.ArrayList)30 Field (java.lang.reflect.Field)17 Map (java.util.Map)17 Test (org.junit.Test)13 EventSetDescriptor (java.beans.EventSetDescriptor)12 MethodDescriptor (java.beans.MethodDescriptor)12 List (java.util.List)11 BeanDescriptor (java.beans.BeanDescriptor)10 HashSet (java.util.HashSet)8 Introspector (java.beans.Introspector)7