Search in sources :

Example 1 with PropertyDescriptor

use of org.apache.aries.blueprint.utils.ReflectionUtils.PropertyDescriptor in project aries by apache.

the class BeanRecipe method setProperty.

private void setProperty(Object instance, Class clazz, String propertyName, Object propertyValue) {
    String[] names = propertyName.split("\\.");
    for (int i = 0; i < names.length - 1; i++) {
        PropertyDescriptor pd = getPropertyDescriptor(clazz, names[i]);
        if (pd.allowsGet()) {
            try {
                instance = pd.get(instance, blueprintContainer);
            } catch (Exception e) {
                throw new ComponentDefinitionException("Error getting property: " + names[i] + " on bean " + getName() + " when setting property " + propertyName + " on class " + clazz.getName(), getRealCause(e));
            }
            if (instance == null) {
                throw new ComponentDefinitionException("Error setting compound property " + propertyName + " on bean " + getName() + ". Property " + names[i] + " is null");
            }
            clazz = instance.getClass();
        } else {
            throw new ComponentDefinitionException("No getter for " + names[i] + " property on bean " + getName() + " when setting property " + propertyName + " on class " + clazz.getName());
        }
    }
    // Instantiate value
    if (propertyValue instanceof Recipe) {
        propertyValue = ((Recipe) propertyValue).create();
    }
    final PropertyDescriptor pd = getPropertyDescriptor(clazz, names[names.length - 1]);
    if (pd.allowsSet()) {
        try {
            pd.set(instance, propertyValue, blueprintContainer);
        } catch (Exception e) {
            throw new ComponentDefinitionException("Error setting property: " + pd, getRealCause(e));
        }
    } else {
        throw new ComponentDefinitionException("No setter for " + names[names.length - 1] + " property");
    }
}
Also used : PropertyDescriptor(org.apache.aries.blueprint.utils.ReflectionUtils.PropertyDescriptor) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) Recipe(org.apache.aries.blueprint.di.Recipe) AbstractRecipe(org.apache.aries.blueprint.di.AbstractRecipe) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AbstractRecipe (org.apache.aries.blueprint.di.AbstractRecipe)1 Recipe (org.apache.aries.blueprint.di.Recipe)1 PropertyDescriptor (org.apache.aries.blueprint.utils.ReflectionUtils.PropertyDescriptor)1 UnableToProxyException (org.apache.aries.proxy.UnableToProxyException)1 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)1