KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > support > ArgumentConvertingMethodInvoker


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.support;
18
19 import java.beans.PropertyEditor JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
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 /**
30  * Subclass of MethodInvoker that tries to convert the given arguments
31  * for the actual target method via BeanWrapperImpl.
32  *
33  * <p>Supports flexible argument conversions, in particular for
34  * invoking a specific overloaded method.
35  *
36  * @author Juergen Hoeller
37  * @since 1.1
38  * @see org.springframework.beans.BeanWrapperImpl#convertIfNecessary
39  */

40 public class ArgumentConvertingMethodInvoker extends MethodInvoker {
41
42     private TypeConverter typeConverter;
43
44     private boolean useDefaultConverter = true;
45
46
47     /**
48      * Set a TypeConverter to use for argument type conversion.
49      * <p>Default is a {@link org.springframework.beans.SimpleTypeConverter}.
50      * Can be overridden with any TypeConverter implementation, typically
51      * a pre-configured SimpleTypeConverter or a BeanWrapperImpl instance.
52      * @see org.springframework.beans.SimpleTypeConverter
53      * @see org.springframework.beans.BeanWrapperImpl
54      */

55     public void setTypeConverter(TypeConverter typeConverter) {
56         this.typeConverter = typeConverter;
57         this.useDefaultConverter = false;
58     }
59
60     /**
61      * Return the TypeConverter used for argument type conversion.
62      * <p>Can be cast to {@link org.springframework.beans.PropertyEditorRegistry}
63      * if direct access to the underlying PropertyEditors is desired
64      * (provided that the present TypeConverter actually implements the
65      * PropertyEditorRegistry interface).
66      */

67     public TypeConverter getTypeConverter() {
68         if (this.typeConverter == null && this.useDefaultConverter) {
69             this.typeConverter = getDefaultTypeConverter();
70         }
71         return this.typeConverter;
72     }
73
74     /**
75      * Obtain the default TypeConverter for this method invoker.
76      * <p>Called if no explicit TypeConverter has been specified.
77      * The default implementation builds a
78      * {@link org.springframework.beans.SimpleTypeConverter}.
79      * Can be overridden in subclasses.
80      */

81     protected TypeConverter getDefaultTypeConverter() {
82         return new SimpleTypeConverter();
83     }
84
85     /**
86      * Register the given custom property editor for all properties of the given type.
87      * <p>Typically used in conjunction with the default
88      * {@link org.springframework.beans.SimpleTypeConverter}; will work with any
89      * TypeConverter that implements the PropertyEditorRegistry interface as well.
90      * @param requiredType type of the property
91      * @param propertyEditor editor to register
92      * @see #setTypeConverter
93      * @see org.springframework.beans.PropertyEditorRegistry#registerCustomEditor
94      */

95     public void registerCustomEditor(Class JavaDoc requiredType, PropertyEditor JavaDoc propertyEditor) {
96         TypeConverter converter = getTypeConverter();
97         if (!(converter instanceof PropertyEditorRegistry)) {
98             throw new IllegalStateException JavaDoc(
99                     "TypeConverter does not implement PropertyEditorRegistry interface: " + converter);
100         }
101         ((PropertyEditorRegistry) converter).registerCustomEditor(requiredType, propertyEditor);
102     }
103
104
105     /**
106      * This implementation looks for a method with matching parameter types:
107      * that is, where each argument value is assignable to the corresponding parameter type.
108      */

109     protected Method JavaDoc findMatchingMethod() {
110         Method JavaDoc[] candidates = getTargetClass().getMethods();
111         Object JavaDoc[] arguments = getArguments();
112         int argCount = arguments.length;
113
114         // First pass: look for method with directly assignable arguments.
115
for (int i = 0; i < candidates.length; i++) {
116             if (candidates[i].getName().equals(getTargetMethod())) {
117                 // Check if the inspected method has the correct number of parameters.
118
Class JavaDoc[] paramTypes = candidates[i].getParameterTypes();
119                 if (paramTypes.length == argCount) {
120                     int numberOfCorrectArguments = 0;
121                     for (int j = 0; j < argCount; j++) {
122                         // Verify that the supplied argument is assignable to the method parameter.
123
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         // Second pass: look for method where arguments can be converted to parameter types.
135
TypeConverter converter = getTypeConverter();
136         if (converter != null) {
137             for (int i = 0; i < candidates.length; i++) {
138                 if (candidates[i].getName().equals(getTargetMethod())) {
139                     // Check if the inspected method has the correct number of parameters.
140
Class JavaDoc[] paramTypes = candidates[i].getParameterTypes();
141                     if (paramTypes.length == argCount) {
142                         Object JavaDoc[] argumentsToUse = new Object JavaDoc[argCount];
143                         int numberOfCorrectArguments = 0;
144                         for (int j = 0; j < argCount; j++) {
145                             // Verify that the supplied argument is assignable to the method parameter.
146
try {
147                                 argumentsToUse[j] = converter.convertIfNecessary(arguments[j], paramTypes[j]);
148                                 numberOfCorrectArguments++;
149                             }
150                             catch (TypeMismatchException ex) {
151                                 // Ignore -> simply doesn't match.
152
}
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