org.springframework.beans.support
public class: ArgumentConvertingMethodInvoker [javadoc |
source]
java.lang.Object
org.springframework.util.MethodInvoker
org.springframework.beans.support.ArgumentConvertingMethodInvoker
Direct Known Subclasses:
MethodInvokingTimerTaskFactoryBean, MethodInvokingRunnable, MethodInvokingFactoryBean, MethodInvokingJobDetailFactoryBean
Subclass of
MethodInvoker that tries to convert the given
arguments for the actual target method via a
TypeConverter .
Supports flexible argument conversions, in particular for
invoking a specific overloaded method.
Also see:
- org.springframework.beans.BeanWrapperImpl#convertIfNecessary
- author:
Juergen - Hoeller
- since:
1.1 -
| Methods from org.springframework.util.MethodInvoker: |
|---|
|
findMatchingMethod, getArguments, getPreparedMethod, getTargetClass, getTargetMethod, getTargetObject, getTypeDifferenceWeight, invoke, isPrepared, prepare, resolveClassName, setArguments, setStaticMethod, setTargetClass, setTargetMethod, setTargetObject |
| Method from org.springframework.beans.support.ArgumentConvertingMethodInvoker Detail: |
protected Method doFindMatchingMethod(Object[] arguments) {
TypeConverter converter = getTypeConverter();
if (converter != null) {
String targetMethod = getTargetMethod();
Method matchingMethod = null;
int argCount = arguments.length;
Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
int minTypeDiffWeight = Integer.MAX_VALUE;
Object[] argumentsToUse = null;
for (int i = 0; i < candidates.length; i++) {
Method candidate = candidates[i];
if (candidate.getName().equals(targetMethod)) {
// Check if the inspected method has the correct number of parameters.
Class[] paramTypes = candidate.getParameterTypes();
if (paramTypes.length == argCount) {
Object[] convertedArguments = new Object[argCount];
boolean match = true;
for (int j = 0; j < argCount && match; j++) {
// Verify that the supplied argument is assignable to the method parameter.
try {
convertedArguments[j] = converter.convertIfNecessary(arguments[j], paramTypes[j]);
}
catch (TypeMismatchException ex) {
// Ignore - > simply doesn't match.
match = false;
}
}
if (match) {
int typeDiffWeight = getTypeDifferenceWeight(paramTypes, convertedArguments);
if (typeDiffWeight < minTypeDiffWeight) {
minTypeDiffWeight = typeDiffWeight;
matchingMethod = candidate;
argumentsToUse = convertedArguments;
}
}
}
}
}
if (matchingMethod != null) {
setArguments(argumentsToUse);
return matchingMethod;
}
}
return null;
}
Actually find a method with matching parameter type, i.e. where each
argument value is assignable to the corresponding parameter type. |
protected Method findMatchingMethod() {
Method matchingMethod = super.findMatchingMethod();
// Second pass: look for method where arguments can be converted to parameter types.
if (matchingMethod == null) {
// Interpret argument array as individual method arguments.
matchingMethod = doFindMatchingMethod(getArguments());
}
if (matchingMethod == null) {
// Interpret argument array as single method argument of array type.
matchingMethod = doFindMatchingMethod(new Object[] {getArguments()});
}
return matchingMethod;
}
This implementation looks for a method with matching parameter types. |
protected TypeConverter getDefaultTypeConverter() {
return new SimpleTypeConverter();
}
Obtain the default TypeConverter for this method invoker.
Called if no explicit TypeConverter has been specified.
The default implementation builds a
org.springframework.beans.SimpleTypeConverter .
Can be overridden in subclasses. |
public TypeConverter getTypeConverter() {
if (this.typeConverter == null && this.useDefaultConverter) {
this.typeConverter = getDefaultTypeConverter();
}
return this.typeConverter;
}
Return the TypeConverter used for argument type conversion.
Can be cast to org.springframework.beans.PropertyEditorRegistry
if direct access to the underlying PropertyEditors is desired
(provided that the present TypeConverter actually implements the
PropertyEditorRegistry interface). |
public void registerCustomEditor(Class requiredType,
PropertyEditor propertyEditor) {
TypeConverter converter = getTypeConverter();
if (!(converter instanceof PropertyEditorRegistry)) {
throw new IllegalStateException(
"TypeConverter does not implement PropertyEditorRegistry interface: " + converter);
}
((PropertyEditorRegistry) converter).registerCustomEditor(requiredType, propertyEditor);
}
Register the given custom property editor for all properties of the given type.
Typically used in conjunction with the default
org.springframework.beans.SimpleTypeConverter ; will work with any
TypeConverter that implements the PropertyEditorRegistry interface as well. |
public void setTypeConverter(TypeConverter typeConverter) {
this.typeConverter = typeConverter;
this.useDefaultConverter = false;
}
Set a TypeConverter to use for argument type conversion.
Default is a org.springframework.beans.SimpleTypeConverter .
Can be overridden with any TypeConverter implementation, typically
a pre-configured SimpleTypeConverter or a BeanWrapperImpl instance. |