Search in sources :

Example 1 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method createBeanInstance.

/**
	 * Create a new instance for the specified bean, using an appropriate instantiation strategy:
	 * factory method, constructor autowiring, or simple instantiation.
	 * @param beanName the name of the bean
	 * @param mbd the bean definition for the bean
	 * @param args explicit arguments to use for constructor or factory method invocation
	 * @return BeanWrapper for the new instance
	 * @see #instantiateUsingFactoryMethod
	 * @see #autowireConstructor
	 * @see #instantiateBean
	 */
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
    // Make sure bean class is actually resolved at this point.
    Class<?> beanClass = resolveBeanClass(mbd, beanName);
    if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
    }
    Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
    if (instanceSupplier != null) {
        BeanWrapper bw = new BeanWrapperImpl(instanceSupplier.get());
        initBeanWrapper(bw);
        return bw;
    }
    if (mbd.getFactoryMethodName() != null) {
        return instantiateUsingFactoryMethod(beanName, mbd, args);
    }
    // Shortcut when re-creating the same bean...
    boolean resolved = false;
    boolean autowireNecessary = false;
    if (args == null) {
        synchronized (mbd.constructorArgumentLock) {
            if (mbd.resolvedConstructorOrFactoryMethod != null) {
                resolved = true;
                autowireNecessary = mbd.constructorArgumentsResolved;
            }
        }
    }
    if (resolved) {
        if (autowireNecessary) {
            return autowireConstructor(beanName, mbd, null, null);
        } else {
            return instantiateBean(beanName, mbd);
        }
    }
    // Need to determine the constructor...
    Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
    if (ctors != null || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
        return autowireConstructor(beanName, mbd, ctors, args);
    }
    // No special handling: simply use no-arg constructor.
    return instantiateBean(beanName, mbd);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Constructor(java.lang.reflect.Constructor)

Example 2 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method autowireBeanProperties.

@Override
public void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) throws BeansException {
    if (autowireMode == AUTOWIRE_CONSTRUCTOR) {
        throw new IllegalArgumentException("AUTOWIRE_CONSTRUCTOR not supported for existing bean instance");
    }
    // Use non-singleton bean definition, to avoid registering bean as dependent bean.
    RootBeanDefinition bd = new RootBeanDefinition(ClassUtils.getUserClass(existingBean), autowireMode, dependencyCheck);
    bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    BeanWrapper bw = new BeanWrapperImpl(existingBean);
    initBeanWrapper(bw);
    populateBean(bd.getBeanClass().getName(), bd, bw);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl)

Example 3 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method applyBeanPropertyValues.

@Override
public void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException {
    markBeanAsCreated(beanName);
    BeanDefinition bd = getMergedBeanDefinition(beanName);
    BeanWrapper bw = new BeanWrapperImpl(existingBean);
    initBeanWrapper(bw);
    applyPropertyValues(beanName, bd, bw, bd.getPropertyValues());
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 4 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project grails-core by grails.

the class GrailsDomainClassValidator method validate.

/**
     * @see CascadingValidator#validate(Object, org.springframework.validation.Errors, boolean)
     */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void validate(Object obj, Errors errors, boolean cascade) {
    if (obj == null) {
        throw new IllegalArgumentException("Argument [" + obj + "] is not an instance of [" + domainClass.getClazz() + "] which this validator is configured for");
    }
    BeanWrapper bean = new BeanWrapperImpl(obj);
    Map constrainedProperties = domainClass.getConstrainedProperties();
    Set<String> constrainedPropertyNames = new HashSet(constrainedProperties.keySet());
    for (Object key : constrainedProperties.keySet()) {
        String propertyName = (String) key;
        validatePropertyWithConstraint(propertyName, obj, errors, bean, constrainedProperties);
    }
    GrailsDomainClassProperty[] persistentProperties = domainClass.getPersistentProperties();
    for (GrailsDomainClassProperty persistentProperty : persistentProperties) {
        String propertyName = persistentProperty.getName();
        if ((persistentProperty.isAssociation() || persistentProperty.isEmbedded()) && cascade) {
            cascadeToAssociativeProperty(errors, bean, persistentProperty);
        }
        // Remove this property from the set of constrained property
        // names because we have already processed it.
        constrainedPropertyNames.remove(propertyName);
    }
    postValidate(obj, errors);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) GString(groovy.lang.GString) Map(java.util.Map) HashSet(java.util.HashSet)

Example 5 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project grails-core by grails.

the class GrailsDomainClassValidator method cascadeValidationToOne.

/**
     * Cascades validation to a one-to-one or many-to-one property.
     *
     * @param errors The Errors instance
     * @param bean The original BeanWrapper
     * @param associatedObject The associated object's current value
     * @param persistentProperty The GrailsDomainClassProperty instance
     * @param propertyName The name of the property
     * @param indexOrKey
     */
@SuppressWarnings("rawtypes")
protected void cascadeValidationToOne(Errors errors, BeanWrapper bean, Object associatedObject, GrailsDomainClassProperty persistentProperty, String propertyName, Object indexOrKey) {
    if (associatedObject == null) {
        return;
    }
    GrailsDomainClass associatedDomainClass = getAssociatedDomainClass(associatedObject, persistentProperty);
    if (associatedDomainClass == null || !isOwningInstance(bean, associatedDomainClass) && !persistentProperty.isExplicitSaveUpdateCascade()) {
        return;
    }
    GrailsDomainClassProperty otherSide = null;
    if (persistentProperty.isBidirectional()) {
        otherSide = persistentProperty.getOtherSide();
    }
    Map associatedConstraintedProperties = associatedDomainClass.getConstrainedProperties();
    GrailsDomainClassProperty[] associatedPersistentProperties = associatedDomainClass.getPersistentProperties();
    String nestedPath = errors.getNestedPath();
    try {
        errors.setNestedPath(buildNestedPath(nestedPath, propertyName, indexOrKey));
        for (GrailsDomainClassProperty associatedPersistentProperty : associatedPersistentProperties) {
            if (persistentProperty.isEmbedded() && EMBEDDED_EXCLUDES.contains(associatedPersistentProperty.getName())) {
                continue;
            }
            String associatedPropertyName = associatedPersistentProperty.getName();
            if (associatedConstraintedProperties.containsKey(associatedPropertyName)) {
                validatePropertyWithConstraint(errors.getNestedPath() + associatedPropertyName, associatedObject, errors, new BeanWrapperImpl(associatedObject), associatedConstraintedProperties);
            }
            if (associatedPersistentProperty.equals(otherSide))
                continue;
            if (associatedPersistentProperty.isAssociation()) {
                cascadeToAssociativeProperty(errors, new BeanWrapperImpl(associatedObject), associatedPersistentProperty);
            }
        }
    } finally {
        errors.setNestedPath(nestedPath);
    }
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsDomainClass(grails.core.GrailsDomainClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) GString(groovy.lang.GString) Map(java.util.Map)

Aggregations

BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)117 BeanWrapper (org.springframework.beans.BeanWrapper)79 Test (org.junit.jupiter.api.Test)32 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 Test (org.junit.Test)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)17 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)17 TestBean (org.springframework.beans.testfixture.beans.TestBean)17 PropertyDescriptor (java.beans.PropertyDescriptor)14 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)13 HashSet (java.util.HashSet)11 BeansException (org.springframework.beans.BeansException)9 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 BeanCreationException (org.springframework.beans.factory.BeanCreationException)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 IOException (java.io.IOException)4