1 16 17 package org.springframework.beans.support; 18 19 import java.beans.PropertyEditor ; 20 import java.lang.reflect.Method ; 21 22 import org.springframework.beans.PropertyEditorRegistry; 23 import org.springframework.beans.SimpleTypeConverter; 24 import org.springframework.beans.TypeConverter; 25 import org.springframework.beans.TypeMismatchException; 26 import org.springframework.util.ClassUtils; 27 import org.springframework.util.MethodInvoker; 28 29 40 public class ArgumentConvertingMethodInvoker extends MethodInvoker { 41 42 private TypeConverter typeConverter; 43 44 private boolean useDefaultConverter = true; 45 46 47 55 public void setTypeConverter(TypeConverter typeConverter) { 56 this.typeConverter = typeConverter; 57 this.useDefaultConverter = false; 58 } 59 60 67 public TypeConverter getTypeConverter() { 68 if (this.typeConverter == null && this.useDefaultConverter) { 69 this.typeConverter = getDefaultTypeConverter(); 70 } 71 return this.typeConverter; 72 } 73 74 81 protected TypeConverter getDefaultTypeConverter() { 82 return new SimpleTypeConverter(); 83 } 84 85 95 public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) { 96 TypeConverter converter = getTypeConverter(); 97 if (!(converter instanceof PropertyEditorRegistry)) { 98 throw new IllegalStateException ( 99 "TypeConverter does not implement PropertyEditorRegistry interface: " + converter); 100 } 101 ((PropertyEditorRegistry) converter).registerCustomEditor(requiredType, propertyEditor); 102 } 103 104 105 109 protected Method findMatchingMethod() { 110 Method [] candidates = getTargetClass().getMethods(); 111 Object [] arguments = getArguments(); 112 int argCount = arguments.length; 113 114 for (int i = 0; i < candidates.length; i++) { 116 if (candidates[i].getName().equals(getTargetMethod())) { 117 Class [] paramTypes = candidates[i].getParameterTypes(); 119 if (paramTypes.length == argCount) { 120 int numberOfCorrectArguments = 0; 121 for (int j = 0; j < argCount; j++) { 122 if (ClassUtils.isAssignableValue(paramTypes[j], arguments[j])) { 124 numberOfCorrectArguments++; 125 } 126 } 127 if (numberOfCorrectArguments == argCount) { 128 return candidates[i]; 129 } 130 } 131 } 132 } 133 134 TypeConverter converter = getTypeConverter(); 136 if (converter != null) { 137 for (int i = 0; i < candidates.length; i++) { 138 if (candidates[i].getName().equals(getTargetMethod())) { 139 Class [] paramTypes = candidates[i].getParameterTypes(); 141 if (paramTypes.length == argCount) { 142 Object [] argumentsToUse = new Object [argCount]; 143 int numberOfCorrectArguments = 0; 144 for (int j = 0; j < argCount; j++) { 145 try { 147 argumentsToUse[j] = converter.convertIfNecessary(arguments[j], paramTypes[j]); 148 numberOfCorrectArguments++; 149 } 150 catch (TypeMismatchException ex) { 151 } 153 } 154 if (numberOfCorrectArguments == argumentsToUse.length) { 155 setArguments(argumentsToUse); 156 return candidates[i]; 157 } 158 } 159 } 160 } 161 } 162 163 return null; 164 } 165 166 } 167 | Popular Tags |