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);
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations