Search in sources :

Example 21 with PropertyDescriptor

use of java.beans.PropertyDescriptor 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 22 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project ignite by apache.

the class PropertyMappingHelper method getPojoPropertyDescriptors.

/**
     * Extracts all property descriptors from a class.
     *
     * @param clazz class which property descriptors should be extracted.
     * @param primitive boolean flag indicating that only property descriptors for primitive properties
     *      should be extracted.
     *
     * @return list of class property descriptors
     */
public static List<PropertyDescriptor> getPojoPropertyDescriptors(Class clazz, boolean primitive) {
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clazz);
    List<PropertyDescriptor> list = new ArrayList<>(descriptors == null ? 1 : descriptors.length);
    if (descriptors == null || descriptors.length == 0)
        return list;
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getReadMethod() == null || (primitive && !isPrimitivePropertyDescriptor(descriptor)))
            continue;
        list.add(descriptor);
    }
    return list;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList)

Example 23 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project ignite by apache.

the class KeyPersistenceSettings method getPartitionKeyDescriptors.

/**
     * @return POJO field descriptors for partition key.
     */
private List<PropertyDescriptor> getPartitionKeyDescriptors() {
    List<PropertyDescriptor> primitivePropDescriptors = PropertyMappingHelper.getPojoPropertyDescriptors(getJavaClass(), true);
    boolean valid = false;
    for (PropertyDescriptor desc : primitivePropDescriptors) {
        if (desc.getWriteMethod() != null) {
            valid = true;
            break;
        }
    }
    if (!valid) {
        throw new IgniteException("Partition key can't have only calculated read-only fields, there should be " + "some fields with setter method");
    }
    return primitivePropDescriptors;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IgniteException(org.apache.ignite.IgniteException)

Example 24 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project ignite by apache.

the class ValuePersistenceSettings method detectFields.

/**
     * Extracts POJO fields from a list of corresponding XML field nodes.
     *
     * @param fieldNodes Field nodes to process.
     * @return POJO fields list.
     */
private List<PojoField> detectFields(NodeList fieldNodes) {
    List<PojoField> list = new LinkedList<>();
    if (fieldNodes == null || fieldNodes.getLength() == 0) {
        List<PropertyDescriptor> primitivePropDescriptors = PropertyMappingHelper.getPojoPropertyDescriptors(getJavaClass(), true);
        for (PropertyDescriptor desc : primitivePropDescriptors) {
            // Skip POJO field if it's read-only
            if (desc.getWriteMethod() != null)
                list.add(new PojoValueField(desc));
        }
        return list;
    }
    List<PropertyDescriptor> allPropDescriptors = PropertyMappingHelper.getPojoPropertyDescriptors(getJavaClass(), false);
    int cnt = fieldNodes.getLength();
    for (int i = 0; i < cnt; i++) {
        PojoValueField field = new PojoValueField((Element) fieldNodes.item(i), getJavaClass());
        PropertyDescriptor desc = findPropertyDescriptor(allPropDescriptors, field.getName());
        if (desc == null) {
            throw new IllegalArgumentException("Specified POJO field '" + field.getName() + "' doesn't exist in '" + getJavaClass().getName() + "' class");
        }
        list.add(field);
    }
    return list;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) LinkedList(java.util.LinkedList)

Example 25 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method populateBean.

/**
	 * Populate the bean instance in the given BeanWrapper with the property values
	 * from the bean definition.
	 * @param beanName the name of the bean
	 * @param mbd the bean definition for the bean
	 * @param bw BeanWrapper with bean instance
	 */
protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) {
    PropertyValues pvs = mbd.getPropertyValues();
    if (bw == null) {
        if (!pvs.isEmpty()) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
        } else {
            // Skip property population phase for null instance.
            return;
        }
    }
    // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
    // state of the bean before properties are set. This can be used, for example,
    // to support styles of field injection.
    boolean continueWithPropertyPopulation = true;
    if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof InstantiationAwareBeanPostProcessor) {
                InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
                    continueWithPropertyPopulation = false;
                    break;
                }
            }
        }
    }
    if (!continueWithPropertyPopulation) {
        return;
    }
    if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
        MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
        // Add property values based on autowire by name if applicable.
        if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
            autowireByName(beanName, mbd, bw, newPvs);
        }
        // Add property values based on autowire by type if applicable.
        if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
            autowireByType(beanName, mbd, bw, newPvs);
        }
        pvs = newPvs;
    }
    boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
    boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
    if (hasInstAwareBpps || needsDepCheck) {
        PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
        if (hasInstAwareBpps) {
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                if (bp instanceof InstantiationAwareBeanPostProcessor) {
                    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                    pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                    if (pvs == null) {
                        return;
                    }
                }
            }
        }
        if (needsDepCheck) {
            checkDependencies(beanName, mbd, filteredPds, pvs);
        }
    }
    applyPropertyValues(beanName, mbd, bw, pvs);
}
Also used : InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor) BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertyValues(org.springframework.beans.PropertyValues) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyDescriptor(java.beans.PropertyDescriptor) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor) MutablePropertyValues(org.springframework.beans.MutablePropertyValues)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)841 Method (java.lang.reflect.Method)400 BeanInfo (java.beans.BeanInfo)342 IntrospectionException (java.beans.IntrospectionException)191 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)171 SimpleBeanInfo (java.beans.SimpleBeanInfo)142 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)141 InvocationTargetException (java.lang.reflect.InvocationTargetException)109 ArrayList (java.util.ArrayList)90 HashMap (java.util.HashMap)66 Field (java.lang.reflect.Field)60 Map (java.util.Map)52 Test (org.junit.Test)39 IOException (java.io.IOException)35 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)34 List (java.util.List)33 Type (java.lang.reflect.Type)21 HashSet (java.util.HashSet)21 Set (java.util.Set)20 LinkedHashMap (java.util.LinkedHashMap)19