1 16 17 package org.springframework.beans.factory.config; 18 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import org.springframework.beans.MutablePropertyValues; 27 import org.springframework.beans.PropertyValue; 28 import org.springframework.util.ObjectUtils; 29 30 48 public abstract class BeanDefinitionVisitor { 49 50 56 public void visitBeanDefinition(BeanDefinition beanDefinition) { 57 visitBeanClassName(beanDefinition); 58 visitScope(beanDefinition); 59 visitPropertyValues(beanDefinition.getPropertyValues()); 60 ConstructorArgumentValues cas = beanDefinition.getConstructorArgumentValues(); 61 visitIndexedArgumentValues(cas.getIndexedArgumentValues()); 62 visitGenericArgumentValues(cas.getGenericArgumentValues()); 63 } 64 65 protected void visitBeanClassName(BeanDefinition beanDefinition) { 66 String beanClassName = beanDefinition.getBeanClassName(); 67 if (beanClassName != null) { 68 String resolvedName = resolveStringValue(beanClassName); 69 if (!beanClassName.equals(resolvedName)) { 70 beanDefinition.setBeanClassName(resolvedName); 71 } 72 } 73 } 74 75 protected void visitScope(BeanDefinition beanDefinition) { 76 String scope = beanDefinition.getScope(); 77 if (scope != null) { 78 String resolvedScope = resolveStringValue(scope); 79 if (!scope.equals(resolvedScope)) { 80 beanDefinition.setScope(resolvedScope); 81 } 82 } 83 } 84 85 protected void visitPropertyValues(MutablePropertyValues pvs) { 86 PropertyValue[] pvArray = pvs.getPropertyValues(); 87 for (int i = 0; i < pvArray.length; i++) { 88 PropertyValue pv = pvArray[i]; 89 Object newVal = resolveValue(pv.getValue()); 90 if (!ObjectUtils.nullSafeEquals(newVal, pv.getValue())) { 91 pvs.addPropertyValue(pv.getName(), newVal); 92 } 93 } 94 } 95 96 protected void visitIndexedArgumentValues(Map ias) { 97 for (Iterator it = ias.values().iterator(); it.hasNext();) { 98 ConstructorArgumentValues.ValueHolder valueHolder = 99 (ConstructorArgumentValues.ValueHolder) it.next(); 100 Object newVal = resolveValue(valueHolder.getValue()); 101 if (!ObjectUtils.nullSafeEquals(newVal, valueHolder.getValue())) { 102 valueHolder.setValue(newVal); 103 } 104 } 105 } 106 107 protected void visitGenericArgumentValues(List gas) { 108 for (Iterator it = gas.iterator(); it.hasNext();) { 109 ConstructorArgumentValues.ValueHolder valueHolder = 110 (ConstructorArgumentValues.ValueHolder) it.next(); 111 Object newVal = resolveValue(valueHolder.getValue()); 112 if (!ObjectUtils.nullSafeEquals(newVal, valueHolder.getValue())) { 113 valueHolder.setValue(newVal); 114 } 115 } 116 } 117 118 protected Object resolveValue(Object value) { 119 if (value instanceof BeanDefinition) { 120 visitBeanDefinition((BeanDefinition) value); 121 } 122 else if (value instanceof BeanDefinitionHolder) { 123 visitBeanDefinition(((BeanDefinitionHolder) value).getBeanDefinition()); 124 } 125 else if (value instanceof RuntimeBeanReference) { 126 RuntimeBeanReference ref = (RuntimeBeanReference) value; 127 String newBeanName = resolveStringValue(ref.getBeanName()); 128 if (!newBeanName.equals(ref.getBeanName())) { 129 return new RuntimeBeanReference(newBeanName); 130 } 131 } 132 else if (value instanceof List ) { 133 visitList((List ) value); 134 } 135 else if (value instanceof Set ) { 136 visitSet((Set ) value); 137 } 138 else if (value instanceof Map ) { 139 visitMap((Map ) value); 140 } 141 else if (value instanceof TypedStringValue) { 142 TypedStringValue typedStringValue = (TypedStringValue) value; 143 String stringValue = typedStringValue.getValue(); 144 if (stringValue != null) { 145 String visitedString = resolveStringValue(stringValue); 146 typedStringValue.setValue(visitedString); 147 } 148 } 149 else if (value instanceof String ) { 150 return resolveStringValue((String ) value); 151 } 152 return value; 153 } 154 155 protected void visitList(List listVal) { 156 for (int i = 0; i < listVal.size(); i++) { 157 Object elem = listVal.get(i); 158 Object newVal = resolveValue(elem); 159 if (!ObjectUtils.nullSafeEquals(newVal, elem)) { 160 listVal.set(i, newVal); 161 } 162 } 163 } 164 165 protected void visitSet(Set setVal) { 166 for (Iterator it = new HashSet (setVal).iterator(); it.hasNext();) { 167 Object elem = it.next(); 168 Object newVal = resolveValue(elem); 169 if (!ObjectUtils.nullSafeEquals(newVal, elem)) { 170 setVal.remove(elem); 171 setVal.add(newVal); 172 } 173 } 174 } 175 176 protected void visitMap(Map mapVal) { 177 for (Iterator it = new HashMap (mapVal).entrySet().iterator(); it.hasNext();) { 178 Map.Entry entry = (Map.Entry ) it.next(); 179 Object key = entry.getKey(); 180 Object newKey = resolveValue(key); 181 boolean isNewKey = !ObjectUtils.nullSafeEquals(key, newKey); 182 Object val = entry.getValue(); 183 Object newVal = resolveValue(val); 184 if (isNewKey) { 185 mapVal.remove(key); 186 } 187 if (isNewKey || !ObjectUtils.nullSafeEquals(newVal, val)) { 188 mapVal.put(newKey, newVal); 189 } 190 } 191 } 192 193 198 protected abstract String resolveStringValue(String strVal); 199 200 } 201 | Popular Tags |