Search in sources :

Example 26 with IntrospectionException

use of java.beans.IntrospectionException 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 27 with IntrospectionException

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

the class EmbeddableDocumentTest method testIntrospection.

@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 28 with IntrospectionException

use of java.beans.IntrospectionException in project jdk8u_jdk by JetBrains.

the class Test5102804 method getReference.

private static Reference getReference() {
    try {
        ClassLoader loader = new Loader();
        Class type = Class.forName(BEAN_NAME, true, loader);
        if (!type.getClassLoader().equals(loader)) {
            throw new Error("Wrong class loader");
        }
        BeanInfo info = Introspector.getBeanInfo(type);
        if (0 != info.getDefaultPropertyIndex()) {
            throw new Error("Wrong bean info found");
        }
        return new WeakReference<Class>(type);
    } catch (IntrospectionException exception) {
        throw new Error("Introspection Error", exception);
    } catch (ClassNotFoundException exception) {
        throw new Error("Class Not Found", exception);
    }
}
Also used : BeanInfo(java.beans.BeanInfo) SimpleBeanInfo(java.beans.SimpleBeanInfo) WeakReference(java.lang.ref.WeakReference) IntrospectionException(java.beans.IntrospectionException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader)

Example 29 with IntrospectionException

use of java.beans.IntrospectionException in project jdk8u_jdk by JetBrains.

the class Test4520754 method getBeanInfo.

private static BeanInfo getBeanInfo(Boolean mark, Class type) {
    System.out.println("test=" + mark + " for " + type);
    BeanInfo info;
    try {
        info = Introspector.getBeanInfo(type);
    } catch (IntrospectionException exception) {
        throw new Error("unexpected exception", exception);
    }
    if (info == null) {
        throw new Error("could not find BeanInfo for " + type);
    }
    if (mark != info.getBeanDescriptor().getValue("test")) {
        throw new Error("could not find marked BeanInfo for " + type);
    }
    return info;
}
Also used : ComponentBeanInfo(infos.ComponentBeanInfo) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 30 with IntrospectionException

use of java.beans.IntrospectionException in project opennms by OpenNMS.

the class OnmsRestService method applyQueryFilters.

protected static void applyQueryFilters(final MultivaluedMap<String, String> p, final CriteriaBuilder builder, final Integer defaultLimit) {
    final MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.putAll(p);
    builder.distinct();
    builder.limit(defaultLimit);
    if (params.containsKey("limit")) {
        builder.limit(Integer.valueOf(params.getFirst("limit")));
        params.remove("limit");
    }
    if (params.containsKey("offset")) {
        builder.offset(Integer.valueOf(params.getFirst("offset")));
        params.remove("offset");
    }
    if (params.containsKey("orderBy")) {
        builder.clearOrder();
        builder.orderBy(params.getFirst("orderBy"));
        params.remove("orderBy");
        if (params.containsKey("order")) {
            if ("desc".equalsIgnoreCase(params.getFirst("order"))) {
                builder.desc();
            } else {
                builder.asc();
            }
            params.remove("order");
        }
    }
    final String query = removeParameter(params, "query");
    if (query != null)
        builder.sql(query);
    final String matchType;
    final String match = removeParameter(params, "match");
    if (match == null) {
        matchType = "all";
    } else {
        matchType = match;
    }
    builder.match(matchType);
    final Class<?> criteriaClass = builder.toCriteria().getCriteriaClass();
    final BeanWrapper wrapper = getBeanWrapperForClass(criteriaClass);
    final String comparatorParam = removeParameter(params, "comparator", "eq").toLowerCase();
    final Criteria currentCriteria = builder.toCriteria();
    for (final String key : params.keySet()) {
        for (final String paramValue : params.get(key)) {
            // the actual implementation com.sun.jersey.core.util.MultivaluedMapImpl returns a String, so this is fine in some way ...
            if ("null".equalsIgnoreCase(paramValue)) {
                builder.isNull(key);
            } else if ("notnull".equalsIgnoreCase(paramValue)) {
                builder.isNotNull(key);
            } else {
                Object value;
                Class<?> type = Object.class;
                try {
                    type = currentCriteria.getType(key);
                } catch (final IntrospectionException e) {
                    LOG.debug("Unable to determine type for key {}", key);
                }
                if (type == null) {
                    type = Object.class;
                }
                LOG.warn("comparator = {}, key = {}, propertyType = {}", comparatorParam, key, type);
                if (comparatorParam.equals("contains") || comparatorParam.equals("iplike") || comparatorParam.equals("ilike") || comparatorParam.equals("like")) {
                    value = paramValue;
                } else {
                    LOG.debug("convertIfNecessary({}, {})", key, paramValue);
                    try {
                        value = wrapper.convertIfNecessary(paramValue, type);
                    } catch (final Throwable t) {
                        LOG.debug("failed to introspect (key = {}, value = {})", key, paramValue, t);
                        value = paramValue;
                    }
                }
                try {
                    final Method m = builder.getClass().getMethod(comparatorParam, String.class, Object.class);
                    m.invoke(builder, new Object[] { key, value });
                } catch (final Throwable t) {
                    LOG.warn("Unable to find method for comparator: {}, key: {}, value: {}", comparatorParam, key, value, t);
                }
            }
        }
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) IntrospectionException(java.beans.IntrospectionException) MultivaluedMapImpl(org.opennms.web.rest.support.MultivaluedMapImpl) Criteria(org.opennms.core.criteria.Criteria) Method(java.lang.reflect.Method)

Aggregations

IntrospectionException (java.beans.IntrospectionException)94 PropertyDescriptor (java.beans.PropertyDescriptor)54 BeanInfo (java.beans.BeanInfo)38 Method (java.lang.reflect.Method)38 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)24 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)16 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 HashMap (java.util.HashMap)7 EventSetDescriptor (java.beans.EventSetDescriptor)6 ArrayList (java.util.ArrayList)6 Field (java.lang.reflect.Field)5 RecursiveChildrenListManager (cern.gp.explorer.test.helpers.RecursiveChildrenListManager)3 SimpleDemoBean (cern.gp.explorer.test.helpers.SimpleDemoBean)3 GPNode (cern.gp.nodes.GPNode)3 Map (java.util.Map)3 ResourceBundle (java.util.ResourceBundle)3 TreeExplorer (cern.gp.explorer.TreeExplorer)2 DefaultActionGroup (com.intellij.openapi.actionSystem.DefaultActionGroup)2 MethodDescriptor (java.beans.MethodDescriptor)2 SimpleBeanInfo (java.beans.SimpleBeanInfo)2