ArgumentConvertingMethodInvoker.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:2k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.beans.support;
  17. import java.beans.PropertyEditor;
  18. import org.springframework.beans.BeanWrapperImpl;
  19. import org.springframework.util.MethodInvoker;
  20. /**
  21.  * Subclass of MethodInvoker that tries to convert the given arguments
  22.  * for the actual target method via BeanWrapperImpl.
  23.  * @author Juergen Hoeller
  24.  * @since 09.06.2004
  25.  * @see org.springframework.beans.BeanWrapperImpl#doTypeConversionIfNecessary
  26.  */
  27. public class ArgumentConvertingMethodInvoker extends MethodInvoker {
  28. private final BeanWrapperImpl beanWrapper = new BeanWrapperImpl();
  29. /**
  30.  * Register the given custom property editor for all properties
  31.  * of the given type.
  32.  * @param requiredType type of the property
  33.  * @param propertyEditor editor to register
  34.  * @see org.springframework.beans.BeanWrapper#registerCustomEditor
  35.  */
  36. public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) {
  37. this.beanWrapper.registerCustomEditor(requiredType, propertyEditor);
  38. }
  39. public void prepare() throws ClassNotFoundException, NoSuchMethodException {
  40. super.prepare();
  41. // try to convert the arguments for the chosen method
  42. Class[] requiredTypes = getPreparedMethod().getParameterTypes();
  43. Object[] arguments = getArguments();
  44. Object[] convertedArguments = new Object[arguments.length];
  45. for (int i = 0; i < arguments.length; i++) {
  46. convertedArguments[i] = this.beanWrapper.doTypeConversionIfNecessary(arguments[i], requiredTypes[i]);
  47. }
  48. setArguments(convertedArguments);
  49. }
  50. }