Search in sources :

Example 11 with BeanInfo

use of java.beans.BeanInfo in project lucene-solr by apache.

the class SolrPluginUtils method findSetter.

private static Method findSetter(Class<?> clazz, String setterName, String key, Class<?> paramClazz, boolean lenient) {
    BeanInfo beanInfo;
    try {
        beanInfo = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException ie) {
        if (lenient) {
            return null;
        }
        throw new RuntimeException("Error getting bean info for class : " + clazz.getName(), ie);
    }
    for (final boolean matchParamClazz : new boolean[] { true, false }) {
        for (final MethodDescriptor desc : beanInfo.getMethodDescriptors()) {
            final Method m = desc.getMethod();
            final Class<?>[] p = m.getParameterTypes();
            if (m.getName().equals(setterName) && p.length == 1 && (!matchParamClazz || paramClazz.equals(p[0]))) {
                return m;
            }
        }
    }
    if (lenient) {
        return null;
    }
    throw new RuntimeException("No setter corrresponding to '" + key + "' in " + clazz.getName());
}
Also used : BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) MethodDescriptor(java.beans.MethodDescriptor)

Example 12 with BeanInfo

use of java.beans.BeanInfo in project lucene-solr by apache.

the class ParseContextConfig method extract.

private void extract(Element element, SolrResourceLoader loader) throws Exception {
    final NodeList xmlEntries = element.getElementsByTagName("entry");
    for (int i = 0, c1 = xmlEntries.getLength(); i < c1; i++) {
        final NamedNodeMap xmlEntryAttributes = xmlEntries.item(i).getAttributes();
        final String className = xmlEntryAttributes.getNamedItem("class").getNodeValue();
        final String implementationName = xmlEntryAttributes.getNamedItem("impl").getNodeValue();
        final NodeList xmlProperties = ((Element) xmlEntries.item(i)).getElementsByTagName("property");
        final Class<?> interfaceClass = loader.findClass(className, Object.class);
        final BeanInfo beanInfo = Introspector.getBeanInfo(interfaceClass, Introspector.IGNORE_ALL_BEANINFO);
        final HashMap<String, PropertyDescriptor> descriptorMap = new HashMap<>();
        for (final PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
            descriptorMap.put(pd.getName(), pd);
        }
        final Object instance = loader.newInstance(implementationName, Object.class);
        if (!interfaceClass.isInstance(instance)) {
            throw new IllegalArgumentException("Implementation class does not extend " + interfaceClass.getName());
        }
        for (int j = 0, c2 = xmlProperties.getLength(); j < c2; j++) {
            final Node xmlProperty = xmlProperties.item(j);
            final NamedNodeMap xmlPropertyAttributes = xmlProperty.getAttributes();
            final String propertyName = xmlPropertyAttributes.getNamedItem("name").getNodeValue();
            final String propertyValue = xmlPropertyAttributes.getNamedItem("value").getNodeValue();
            final PropertyDescriptor propertyDescriptor = descriptorMap.get(propertyName);
            if (propertyDescriptor == null) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Unknown bean property %s in class %s", propertyName, interfaceClass.getName()));
            }
            final Method method = propertyDescriptor.getWriteMethod();
            if (method == null) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot set bean property %s in class %s (no write method available)", propertyName, interfaceClass.getName()));
            }
            method.invoke(instance, getValueFromString(propertyDescriptor.getPropertyType(), propertyValue));
        }
        entries.put(interfaceClass, instance);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) BeanInfo(java.beans.BeanInfo) Node(org.w3c.dom.Node) Method(java.lang.reflect.Method)

Example 13 with BeanInfo

use of java.beans.BeanInfo in project camel by apache.

the class SObjectTree method updateGeneralObjectId.

boolean updateGeneralObjectId(final String id, final Object object) {
    final Class<? extends Object> clazz = object.getClass();
    final BeanInfo beanInfo;
    try {
        beanInfo = Introspector.getBeanInfo(clazz);
    } catch (final IntrospectionException e) {
        throw new IllegalStateException(e);
    }
    final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    final Optional<PropertyDescriptor> maybeIdProperty = Arrays.stream(propertyDescriptors).filter(pd -> "id".equals(pd.getName())).findFirst();
    if (maybeIdProperty.isPresent()) {
        final Method readMethod = maybeIdProperty.get().getReadMethod();
        try {
            readMethod.invoke(object, id);
            return true;
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new IllegalStateException(e);
        }
    }
    return false;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) XStreamImplicit(com.thoughtworks.xstream.annotations.XStreamImplicit) Arrays(java.util.Arrays) XStreamAlias(com.thoughtworks.xstream.annotations.XStreamAlias) XStreamOmitField(com.thoughtworks.xstream.annotations.XStreamOmitField) Introspector(java.beans.Introspector) AbstractDescribedSObjectBase(org.apache.camel.component.salesforce.api.dto.AbstractDescribedSObjectBase) BeanInfo(java.beans.BeanInfo) Objects.requireNonNull(java.util.Objects.requireNonNull) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) StreamSupport(java.util.stream.StreamSupport) RestError(org.apache.camel.component.salesforce.api.dto.RestError) Method(java.lang.reflect.Method) AbstractSObjectBase(org.apache.camel.component.salesforce.api.dto.AbstractSObjectBase) Set(java.util.Set) Collectors(java.util.stream.Collectors) IntrospectionException(java.beans.IntrospectionException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) PropertyDescriptor(java.beans.PropertyDescriptor) Optional(java.util.Optional) ObjectHelper(org.apache.camel.util.ObjectHelper) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 14 with BeanInfo

use of java.beans.BeanInfo in project hibernate-orm by hibernate.

the class Cloneable method checkListeners.

private void checkListeners() {
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(getClass(), Object.class);
        internalCheckListeners(beanInfo);
    } catch (IntrospectionException t) {
        throw new HibernateException("Unable to validate listener config", t);
    } finally {
        if (beanInfo != null) {
            // release the jdk internal caches everytime to ensure this
            // plays nicely with destroyable class-loaders
            Introspector.flushFromCaches(getClass());
        }
    }
}
Also used : HibernateException(org.hibernate.HibernateException) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 15 with BeanInfo

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

the class ReflectUtils method getPropertiesHelper.

private static PropertyDescriptor[] getPropertiesHelper(Class type, boolean read, boolean write) {
    try {
        BeanInfo info = Introspector.getBeanInfo(type, Object.class);
        PropertyDescriptor[] all = info.getPropertyDescriptors();
        if (read && write) {
            return all;
        }
        List properties = new ArrayList(all.length);
        for (int i = 0; i < all.length; i++) {
            PropertyDescriptor pd = all[i];
            if ((read && pd.getReadMethod() != null) || (write && pd.getWriteMethod() != null)) {
                properties.add(pd);
            }
        }
        return (PropertyDescriptor[]) properties.toArray(new PropertyDescriptor[properties.size()]);
    } catch (IntrospectionException e) {
        throw new CodeGenerationException(e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) ArrayList(java.util.ArrayList) List(java.util.List)

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