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

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.factory.support;
  17. import java.lang.reflect.Method;
  18. import java.util.LinkedList;
  19. import java.util.List;
  20. /**
  21.  * Extension of MethodOverride that represents an arbitrary
  22.  * override of a method by the IoC container.
  23.  *
  24.  * <p>Any non-final method can be overridden, irrespective of its
  25.  * parameters and return types.
  26.  *
  27.  * @author Rod Johnson
  28.  */
  29. public class ReplaceOverride extends MethodOverride {
  30. private final String methodReplacerBeanName;
  31. /** 
  32.  * List of String. Identifying signatures.
  33.  */
  34. private List typeIdentifiers = new LinkedList();
  35. public ReplaceOverride(String methodName, String methodReplacerBeanName) {
  36. super(methodName);
  37. this.methodReplacerBeanName = methodReplacerBeanName;
  38. }
  39. /**
  40.  * Add a fragment of a class string, like "Exception"
  41.  * or "java.lang.Exc", to identify a parameter type
  42.  * @param s substring of class FQN
  43.  */
  44. public void addTypeIdentifier(String s) {
  45. this.typeIdentifiers.add(s);
  46. }
  47. public boolean matches(Method method, MethodOverrides overrides) {
  48. // TODO could cache result for efficiency
  49. if (!method.getName().equals(getMethodName())) {
  50. // it can't match
  51. return false;
  52. }
  53. if (!overrides.isOverloadedMethodName(method.getName())) {
  54. // No overloaded: don't worry about arg type matching.
  55. return true;
  56. }
  57. // If we get to here, we need to insist on precise argument matching.
  58. if (this.typeIdentifiers.size() != method.getParameterTypes().length) {
  59. return false;
  60. }
  61. for (int i = 0; i < this.typeIdentifiers.size(); i++) {
  62. String identifier = (String) this.typeIdentifiers.get(i);
  63. if (method.getParameterTypes()[i].getName().indexOf(identifier) == -1) {
  64. // this parameter can't match
  65. return false;
  66. }
  67. }
  68. return true;
  69. }
  70. /**
  71.  * Return the name of the bean implementing MethodReplacer.
  72.  */
  73. public String getMethodReplacerBeanName() {
  74. return methodReplacerBeanName;
  75. }
  76. public String toString() {
  77. return "Replace override for method '" + getMethodName() + "; will call bean '" +
  78. this.methodReplacerBeanName + "'";
  79. }
  80. }