当IoC容器的初始化完毕后,我们就要接触IoC容器的核心功能:依赖注入
在基本的IoC容器接口BeanFactory中,有一个getBean的接口方法的定义,这个接口的实现就是触发依赖注入发生的地方。我们从DefaultListableBeanFactory的基类AbstractBeanFactory入手了解getBean()的实现:
- public Object getBean(String name) throws BeansException {
- return doGetBean(name, null, null, false);
- }
这里的getBean接口方法最终通过调用doGetBean来实现的,也就是触发依赖注入发生的地方。
- protected <T> T doGetBean(
- final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
- throws BeansException {
- final String beanName = transformedBeanName(name);
- Object bean;
- // Eagerly check singleton cache for manually registered singletons.
- Object sharedInstance = getSingleton(beanName);
- if (sharedInstance != null && args == null) {
- if (logger.isDebugEnabled()) {
- if (isSingletonCurrentlyInCreation(beanName)) {
- logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
- "' that is not fully initialized yet - a consequence of a circular reference");
- }
- else {
- logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
- }
- }
- bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
- }
- else {
- // Fail if we're already creating this bean instance:
- // We're assumably within a circular reference.
- if (isPrototypeCurrentlyInCreation(beanName)) {
- throw new BeanCurrentlyInCreationException(beanName);
- }
- // Check if bean definition exists in this factory.
- BeanFactory parentBeanFactory = getParentBeanFactory();
- if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
- // Not found -> check parent.
- String nameToLookup = originalBeanName(name);
- if (args != null) {
- // Delegation to parent with explicit args.
- return (T) parentBeanFactory.getBean(nameToLookup, args);
- }
- else {
- // No args -> delegate to standard getBean method.
- return parentBeanFactory.getBean(nameToLookup, requiredType);
- }
- }
- if (!typeCheckOnly) {
- markBeanAsCreated(beanName);
- }
- final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
- checkMergedBeanDefinition(mbd, beanName, args);
- // Guarantee initialization of beans that the current bean depends on.
- String[] dependsOn = mbd.getDependsOn();
- if (dependsOn != null) {
- for (String dependsOnBean : dependsOn) {
- getBean(dependsOnBean);
- registerDependentBean(dependsOnBean, beanName);
- }
- }
- // Create bean instance.
- if (mbd.isSingleton()) {
- sharedInstance = getSingleton(beanName, new ObjectFactory() {
- public Object getObject() throws BeansException {
- try {
- return createBean(beanName, mbd, args);
- }
- catch (BeansException ex) {
- // Explicitly remove instance from singleton cache: It might have been put there
- // eagerly by the creation process, to allow for circular reference resolution.
- // Also remove any beans that received a temporary reference to the bean.
- destroySingleton(beanName);
- throw ex;
- }
- }
- });
- bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
- }
- else if (mbd.isPrototype()) {
- // It's a prototype -> create a new instance.
- Object prototypeInstance = null;
- try {
- beforePrototypeCreation(beanName);
- prototypeInstance = createBean(beanName, mbd, args);
- }
- finally {
- afterPrototypeCreation(beanName);
- }
- bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
- }
- else {
- String scopeName = mbd.getScope();
- final Scope scope = this.scopes.get(scopeName);
- if (scope == null) {
- throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'");
- }
- try {
- Object scopedInstance = scope.get(beanName, new ObjectFactory() {
- public Object getObject() throws BeansException {
- beforePrototypeCreation(beanName);
- try {
- return createBean(beanName, mbd, args);
- }
- finally {
- afterPrototypeCreation(beanName);
- }
- }
- });
- bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
- }
- catch (IllegalStateException ex) {
- throw new BeanCreationException(beanName,
- "Scope '" + scopeName + "' is not active for the current thread; " +
- "consider defining a scoped proxy for this bean if you intend to refer to it from a singleton",
- ex);
- }
- }
- }
- // Check if required type matches the type of the actual bean instance.
- if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
- throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
- }
- return (T) bean;
- }
首先从缓存中取,处理已经被创建过的单件模式的bean,对这种bean的请求不需要重复地创建,接着
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null)主要完成的是判断它是一个FactoryBean还是普通的Bean对象,如果是普通的Bean对象则直接返回,如果是FactoryBean,则创建一个新的对象Bean返回,具体详细的步骤就不讲了,接着检查IoC容器里的BeanDefinition,看是否取得我们需要的Bean.如果在当前俄工厂中取不到,则到双亲BeanFactory中去取,如果当前的双亲工厂娶不到,那就顺着双亲BeanFactory链一直向上寻找。final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName)根据beanName获取到BeanDefinition。
String[] dependsOn = mbd.getDependsOn() 取得当前bean的所有依赖Bean,这样会触发getBean的递归调用,直至渠道一个没有任何依赖的bean为止。
接着就是创建Bean的实例,首先它判断的Bean的生命周期,然后才调用createBean创建Bean的实例。然后对创建出来的Bean进行类型检查,这里的bean已经是包含依赖关系的Bean.
- protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
- throws BeanCreationException {
- if (logger.isDebugEnabled()) {
- logger.debug("Creating instance of bean '" + beanName + "'");
- }
- // Make sure bean class is actually resolved at this point.
- resolveBeanClass(mbd, beanName);
- // Prepare method overrides.
- try {
- mbd.prepareMethodOverrides();
- }
- catch (BeanDefinitionValidationException ex) {
- throw new BeanDefinitionStoreException(mbd.getResourceDescription(),
- beanName, "Validation of method overrides failed", ex);
- }
- try {
- // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
- Object bean = resolveBeforeInstantiation(beanName, mbd);
- if (bean != null) {
- return bean;
- }
- }
- catch (Throwable ex) {
- throw new BeanCreationException(mbd.getResourceDescription(), beanName,
- "BeanPostProcessor before instantiation of bean failed", ex);
- }
- Object beanInstance = doCreateBean(beanName, mbd, args);
- if (logger.isDebugEnabled()) {
- logger.debug("Finished creating instance of bean '" + beanName + "'");
- }
- return beanInstance;
- }
resolveBeanClass(mbd, beanName);
首先判断需要创建的bean是否可以被实例化,这个类是否可以通过类装载器来载入。
Object bean = resolveBeforeInstantiation(beanName, mbd);
接着如果bean配置了PostProcessor,那么这里返回的是一个代理
Object beanInstance = doCreateBean(beanName, mbd, args);
这里是创建bean的调用.
- protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
- // Instantiate the bean.
- BeanWrapper instanceWrapper = null;
- if (mbd.isSingleton()) {
- instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
- }
- if (instanceWrapper == null) {
- instanceWrapper = createBeanInstance(beanName, mbd, args);
- }
- final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
- Class beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
- // Allow post-processors to modify the merged bean definition.
- synchronized (mbd.postProcessingLock) {
- if (!mbd.postProcessed) {
- applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
- mbd.postProcessed = true;
- }
- }
- // Eagerly cache singletons to be able to resolve circular references
- // even when triggered by lifecycle interfaces like BeanFactoryAware.
- boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
- isSingletonCurrentlyInCreation(beanName));
- if (earlySingletonExposure) {
- if (logger.isDebugEnabled()) {
- logger.debug("Eagerly caching bean '" + beanName +
- "' to allow for resolving potential circular references");
- }
- addSingletonFactory(beanName, new ObjectFactory() {
- public Object getObject() throws BeansException {
- return getEarlyBeanReference(beanName, mbd, bean);
- }
- });
- }
- // Initialize the bean instance.
- Object exposedObject = bean;
- try {
- populateBean(beanName, mbd, instanceWrapper);
- if (exposedObject != null) {
- exposedObject = initializeBean(beanName, exposedObject, mbd);
- }
- }
- catch (Throwable ex) {
- if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
- throw (BeanCreationException) ex;
- }
- else {
- throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
- }
- }
- if (earlySingletonExposure) {
- Object earlySingletonReference = getSingleton(beanName, false);
- if (earlySingletonReference != null) {
- if (exposedObject == bean) {
- exposedObject = earlySingletonReference;
- }
- else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
- String[] dependentBeans = getDependentBeans(beanName);
- Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
- for (String dependentBean : dependentBeans) {
- if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
- actualDependentBeans.add(dependentBean);
- }
- }
- if (!actualDependentBeans.isEmpty()) {
- throw new BeanCurrentlyInCreationException(beanName,
- "Bean with name '" + beanName + "' has been injected into other beans [" +
- StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
- "] in its raw version as part of a circular reference, but has eventually been " +
- "wrapped. This means that said other beans do not use the final version of the " +
- "bean. This is often the result of over-eager type matching - consider using " +
- "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
- }
- }
- }
- }
- // Register bean as disposable.
- try {
- registerDisposableBeanIfNecessary(beanName, bean, mbd);
- }
- catch (BeanDefinitionValidationException ex) {
- throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
- }
- return exposedObject;
- }
BeanWrapper是用来持有创建出来的bean对象的。如果是singleton,先把缓存中的同名bean清除。如果bean首次创建,那么缓存中没有bean的实例,即进入到创建bean.
instanceWrapper = createBeanInstance(beanName, mbd, args);
- 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());
- }
- 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);
- }
这里包含使用工厂方法对Bean进行实例化;使用构造函数对Bean进行实例化;使用默认额构造函数对Bean进行实例化,我们接下来看看最常见的实例化过程instantiateBean:
- protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
- try {
- Object beanInstance;
- final BeanFactory parent = this;
- if (System.getSecurityManager() != null) {
- beanInstance = AccessController.doPrivileged(new PrivilegedAction<Object>() {
- public Object run() {
- return getInstantiationStrategy().instantiate(mbd, beanName, parent);
- }
- }, getAccessControlContext());
- }
- else {
- beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
- }
- BeanWrapper bw = new BeanWrapperImpl(beanInstance);
- initBeanWrapper(bw);
- return bw;
- }
- catch (Throwable ex) {
- throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
- }
- }
这里我们使用默认的实例化策略对Bean进行实例化,默认的实例化策略是
CglibSubclassingInstantiationStrategy,也就是用cglib来对bean
进行实例化。
<!--EndFragment-->
首先取得在BeanFactory中设置的property的值,这些值来自对BeanDefinition的解析。接着开始进行依赖注入的过程,先处理autowire的注入接着对属性进行注入。接下来我们看看具体对属性进行解析和注入的过程。
- protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
- if (pvs == null || pvs.isEmpty()) {
- return;
- }
- MutablePropertyValues mpvs = null;
- List<PropertyValue> original;
- if (System.getSecurityManager()!= null) {
- if (bw instanceof BeanWrapperImpl) {
- ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
- }
- }
- if (pvs instanceof MutablePropertyValues) {
- mpvs = (MutablePropertyValues) pvs;
- if (mpvs.isConverted()) {
- // Shortcut: use the pre-converted values as-is.
- try {
- bw.setPropertyValues(mpvs);
- return;
- }
- catch (BeansException ex) {
- throw new BeanCreationException(
- mbd.getResourceDescription(), beanName, "Error setting property values", ex);
- }
- }
- original = mpvs.getPropertyValueList();
- }
- else {
- original = Arrays.asList(pvs.getPropertyValues());
- }
- TypeConverter converter = getCustomTypeConverter();
- if (converter == null) {
- converter = bw;
- }
- BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);
- // Create a deep copy, resolving any references for values.
- List<PropertyValue> deepCopy = new ArrayList<PropertyValue>(original.size());
- boolean resolveNecessary = false;
- for (PropertyValue pv : original) {
- if (pv.isConverted()) {
- deepCopy.add(pv);
- }
- else {
- String propertyName = pv.getName();
- Object originalValue = pv.getValue();
- Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
- Object convertedValue = resolvedValue;
- boolean convertible = bw.isWritableProperty(propertyName) &&
- !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName);
- if (convertible) {
- convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
- }
- // Possibly store converted value in merged bean definition,
- // in order to avoid re-conversion for every created bean instance.
- if (resolvedValue == originalValue) {
- if (convertible) {
- pv.setConvertedValue(convertedValue);
- }
- deepCopy.add(pv);
- }
- else if (convertible && originalValue instanceof TypedStringValue &&
- !((TypedStringValue) originalValue).isDynamic() &&
- !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {
- pv.setConvertedValue(convertedValue);
- deepCopy.add(pv);
- }
- else {
- resolveNecessary = true;
- deepCopy.add(new PropertyValue(pv, convertedValue));
- }
- }
- }
- if (mpvs != null && !resolveNecessary) {
- mpvs.setConverted();
- }
- // Set our (possibly massaged) deep copy.
- try {
- bw.setPropertyValues(new MutablePropertyValues(deepCopy));
- }
- catch (BeansException ex) {
- throw new BeanCreationException(
- mbd.getResourceDescription(), beanName, "Error setting property values", ex);
- }
- }
首先为解析值创建一个拷贝,拷贝的数据将会被注入到Bean中,最后设置到BeanWrapper中。
- public Object resolveValueIfNecessary(Object argName, Object value) {
- // We must check each value to see whether it requires a runtime reference
- // to another bean to be resolved.
- if (value instanceof RuntimeBeanReference) {
- RuntimeBeanReference ref = (RuntimeBeanReference) value;
- return resolveReference(argName, ref);
- }
- else if (value instanceof RuntimeBeanNameReference) {
- String refName = ((RuntimeBeanNameReference) value).getBeanName();
- refName = String.valueOf(evaluate(refName));
- if (!this.beanFactory.containsBean(refName)) {
- throw new BeanDefinitionStoreException(
- "Invalid bean name '" + refName + "' in bean reference for " + argName);
- }
- return refName;
- }
- else if (value instanceof BeanDefinitionHolder) {
- // Resolve BeanDefinitionHolder: contains BeanDefinition with name and aliases.
- BeanDefinitionHolder bdHolder = (BeanDefinitionHolder) value;
- return resolveInnerBean(argName, bdHolder.getBeanName(), bdHolder.getBeanDefinition());
- }
- else if (value instanceof BeanDefinition) {
- // Resolve plain BeanDefinition, without contained name: use dummy name.
- BeanDefinition bd = (BeanDefinition) value;
- return resolveInnerBean(argName, "(inner bean)", bd);
- }
- else if (value instanceof ManagedArray) {
- // May need to resolve contained runtime references.
- ManagedArray array = (ManagedArray) value;
- Class elementType = array.resolvedElementType;
- if (elementType == null) {
- String elementTypeName = array.getElementTypeName();
- if (StringUtils.hasText(elementTypeName)) {
- try {
- elementType = ClassUtils.forName(elementTypeName, this.beanFactory.getBeanClassLoader());
- array.resolvedElementType = elementType;
- }
- catch (Throwable ex) {
- // Improve the message by showing the context.
- throw new BeanCreationException(
- this.beanDefinition.getResourceDescription(), this.beanName,
- "Error resolving array type for " + argName, ex);
- }
- }
- else {
- elementType = Object.class;
- }
- }
- return resolveManagedArray(argName, (List<?>) value, elementType);
- }
- else if (value instanceof ManagedList) {
- // May need to resolve contained runtime references.
- return resolveManagedList(argName, (List<?>) value);
- }
- else if (value instanceof ManagedSet) {
- // May need to resolve contained runtime references.
- return resolveManagedSet(argName, (Set<?>) value);
- }
- else if (value instanceof ManagedMap) {
- // May need to resolve contained runtime references.
- return resolveManagedMap(argName, (Map<?, ?>) value);
- }
- else if (value instanceof ManagedProperties) {
- Properties original = (Properties) value;
- Properties copy = new Properties();
- for (Map.Entry propEntry : original.entrySet()) {
- Object propKey = propEntry.getKey();
- Object propValue = propEntry.getValue();
- if (propKey instanceof TypedStringValue) {
- propKey = evaluate((TypedStringValue) propKey);
- }
- if (propValue instanceof TypedStringValue) {
- propValue = evaluate((TypedStringValue) propValue);
- }
- copy.put(propKey, propValue);
- }
- return copy;
- }
- else if (value instanceof TypedStringValue) {
- // Convert value to target type here.
- TypedStringValue typedStringValue = (TypedStringValue) value;
- Object valueObject = evaluate(typedStringValue);
- try {
- Class<?> resolvedTargetType = resolveTargetType(typedStringValue);
- if (resolvedTargetType != null) {
- return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType);
- }
- else {
- return valueObject;
- }
- }
- catch (Throwable ex) {
- // Improve the message by showing the context.
- throw new BeanCreationException(
- this.beanDefinition.getResourceDescription(), this.beanName,
- "Error converting typed String value for " + argName, ex);
- }
- }
- else {
- return evaluate(value);
- }
- }
这个函数里定义了对各种类型属性的解析而真正的注入是在BeanWrapperImpl类中的setPropertyValue方法中:
- private void setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv) throws BeansException {
- String propertyName = tokens.canonicalName;
- String actualName = tokens.actualName;
- if (tokens.keys != null) {
- // Apply indexes and map keys: fetch value for all keys but the last one.
- PropertyTokenHolder getterTokens = new PropertyTokenHolder();
- getterTokens.canonicalName = tokens.canonicalName;
- getterTokens.actualName = tokens.actualName;
- getterTokens.keys = new String[tokens.keys.length - 1];
- System.arraycopy(tokens.keys, 0, getterTokens.keys, 0, tokens.keys.length - 1);
- Object propValue;
- try {
- propValue = getPropertyValue(getterTokens);
- }
- catch (NotReadablePropertyException ex) {
- throw new NotWritablePropertyException(getRootClass(), this.nestedPath + propertyName,
- "Cannot access indexed value in property referenced " +
- "in indexed property path '" + propertyName + "'", ex);
- }
- // Set value for last key.
- String key = tokens.keys[tokens.keys.length - 1];
- if (propValue == null) {
- throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
- "Cannot access indexed value in property referenced " +
- "in indexed property path '" + propertyName + "': returned null");
- }
- else if (propValue.getClass().isArray()) {
- PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName);
- Class requiredType = propValue.getClass().getComponentType();
- int arrayIndex = Integer.parseInt(key);
- Object oldValue = null;
- try {
- if (isExtractOldValueForEditor()) {
- oldValue = Array.get(propValue, arrayIndex);
- }
- Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType,
- new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), requiredType));
- Array.set(propValue, arrayIndex, convertedValue);
- }
- catch (IndexOutOfBoundsException ex) {
- throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
- "Invalid array index in property path '" + propertyName + "'", ex);
- }
- }
- else if (propValue instanceof List) {
- PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName);
- Class requiredType = GenericCollectionTypeResolver.getCollectionReturnType(
- pd.getReadMethod(), tokens.keys.length);
- List list = (List) propValue;
- int index = Integer.parseInt(key);
- Object oldValue = null;
- if (isExtractOldValueForEditor() && index < list.size()) {
- oldValue = list.get(index);
- }
- Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType,
- new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), requiredType));
- if (index < list.size()) {
- list.set(index, convertedValue);
- }
- else if (index >= list.size()) {
- for (int i = list.size(); i < index; i++) {
- try {
- list.add(null);
- }
- catch (NullPointerException ex) {
- throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
- "Cannot set element with index " + index + " in List of size " +
- list.size() + ", accessed using property path '" + propertyName +
- "': List does not support filling up gaps with null elements");
- }
- }
- list.add(convertedValue);
- }
- }
- else if (propValue instanceof Map) {
- PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName);
- Class mapKeyType = GenericCollectionTypeResolver.getMapKeyReturnType(
- pd.getReadMethod(), tokens.keys.length);
- Class mapValueType = GenericCollectionTypeResolver.getMapValueReturnType(
- pd.getReadMethod(), tokens.keys.length);
- Map map = (Map) propValue;
- // IMPORTANT: Do not pass full property name in here - property editors
- // must not kick in for map keys but rather only for map values.
- Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType,
- new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), mapKeyType));
- Object oldValue = null;
- if (isExtractOldValueForEditor()) {
- oldValue = map.get(convertedMapKey);
- }
- // Pass full property name and old value in here, since we want full
- // conversion ability for map values.
- Object convertedMapValue = convertIfNecessary(
- propertyName, oldValue, pv.getValue(), mapValueType,
- new TypeDescriptor(new MethodParameter(pd.getReadMethod(), -1, tokens.keys.length + 1)));
- map.put(convertedMapKey, convertedMapValue);
- }
- else {
- throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
- "Property referenced in indexed property path '" + propertyName +
- "' is neither an array nor a List nor a Map; returned value was [" + pv.getValue() + "]");
- }
- }
- else {
- PropertyDescriptor pd = pv.resolvedDescriptor;
- if (pd == null || !pd.getWriteMethod().getDeclaringClass().isInstance(this.object)) {
- pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName);
- if (pd == null || pd.getWriteMethod() == null) {
- if (pv.isOptional()) {
- logger.debug("Ignoring optional value for property '" + actualName +
- "' - property not found on bean class [" + getRootClass().getName() + "]");
- return;
- }
- else {
- PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass());
- throw new NotWritablePropertyException(
- getRootClass(), this.nestedPath + propertyName,
- matches.buildErrorMessage(), matches.getPossibleMatches());
- }
- }
- pv.getOriginalPropertyValue().resolvedDescriptor = pd;
- }
- Object oldValue = null;
- try {
- Object originalValue = pv.getValue();
- Object valueToApply = originalValue;
- if (!Boolean.FALSE.equals(pv.conversionNecessary)) {
- if (pv.isConverted()) {
- valueToApply = pv.getConvertedValue();
- }
- else {
- if (isExtractOldValueForEditor() && pd.getReadMethod() != null) {
- final Method readMethod = pd.getReadMethod();
- if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()) &&
- !readMethod.isAccessible()) {
- if (System.getSecurityManager()!= null) {
- AccessController.doPrivileged(new PrivilegedAction<Object>() {
- public Object run() {
- readMethod.setAccessible(true);
- return null;
- }
- });
- }
- else {
- readMethod.setAccessible(true);
- }
- }
- try {
- if (System.getSecurityManager() != null) {
- oldValue = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
- public Object run() throws Exception {
- return readMethod.invoke(object);
- }
- }, acc);
- }
- else {
- oldValue = readMethod.invoke(object);
- }
- }
- catch (Exception ex) {
- if (ex instanceof PrivilegedActionException) {
- ex = ((PrivilegedActionException) ex).getException();
- }
- if (logger.isDebugEnabled()) {
- logger.debug("Could not read previous value of property '" +
- this.nestedPath + propertyName + "'", ex);
- }
- }
- }
- valueToApply = convertForProperty(propertyName, oldValue, originalValue, pd);
- }
- pv.getOriginalPropertyValue().conversionNecessary = (valueToApply != originalValue);
- }
- final Method writeMethod = (pd instanceof GenericTypeAwarePropertyDescriptor ?
- ((GenericTypeAwarePropertyDescriptor) pd).getWriteMethodForActualAccess() :
- pd.getWriteMethod());
- if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()) && !writeMethod.isAccessible()) {
- if (System.getSecurityManager()!= null) {
- AccessController.doPrivileged(new PrivilegedAction<Object>() {
- public Object run() {
- writeMethod.setAccessible(true);
- return null;
- }
- });
- }
- else {
- writeMethod.setAccessible(true);
- }
- }
- final Object value = valueToApply;
- if (System.getSecurityManager() != null) {
- try {
- AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
- public Object run() throws Exception {
- writeMethod.invoke(object, value);
- return null;
- }
- }, acc);
- }
- catch (PrivilegedActionException ex) {
- throw ex.getException();
- }
- }
- else {
- writeMethod.invoke(this.object, value);
- }
- }
- catch (TypeMismatchException ex) {
- throw ex;
- }
- catch (InvocationTargetException ex) {
- PropertyChangeEvent propertyChangeEvent =
- new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
- if (ex.getTargetException() instanceof ClassCastException) {
- throw new TypeMismatchException(propertyChangeEvent, pd.getPropertyType(), ex.getTargetException());
- }
- else {
- throw new MethodInvocationException(propertyChangeEvent, ex.getTargetException());
- }
- }
- catch (Exception ex) {
- PropertyChangeEvent pce =
- new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
- throw new MethodInvocationException(pce, ex);
- }
- }
- }