001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.beans.factory.support; 018 019import java.lang.reflect.Method; 020import java.util.LinkedList; 021import java.util.List; 022 023import org.springframework.util.Assert; 024import org.springframework.util.ObjectUtils; 025 026/** 027 * Extension of MethodOverride that represents an arbitrary 028 * override of a method by the IoC container. 029 * 030 * <p>Any non-final method can be overridden, irrespective of its 031 * parameters and return types. 032 * 033 * @author Rod Johnson 034 * @author Juergen Hoeller 035 * @since 1.1 036 */ 037public class ReplaceOverride extends MethodOverride { 038 039 private final String methodReplacerBeanName; 040 041 private final List<String> typeIdentifiers = new LinkedList<String>(); 042 043 044 /** 045 * Construct a new ReplaceOverride. 046 * @param methodName the name of the method to override 047 * @param methodReplacerBeanName the bean name of the MethodReplacer 048 */ 049 public ReplaceOverride(String methodName, String methodReplacerBeanName) { 050 super(methodName); 051 Assert.notNull(methodReplacerBeanName, "Method replacer bean name must not be null"); 052 this.methodReplacerBeanName = methodReplacerBeanName; 053 } 054 055 056 /** 057 * Return the name of the bean implementing MethodReplacer. 058 */ 059 public String getMethodReplacerBeanName() { 060 return this.methodReplacerBeanName; 061 } 062 063 /** 064 * Add a fragment of a class string, like "Exception" 065 * or "java.lang.Exc", to identify a parameter type. 066 * @param identifier a substring of the fully qualified class name 067 */ 068 public void addTypeIdentifier(String identifier) { 069 this.typeIdentifiers.add(identifier); 070 } 071 072 073 @Override 074 public boolean matches(Method method) { 075 if (!method.getName().equals(getMethodName())) { 076 return false; 077 } 078 if (!isOverloaded()) { 079 // Not overloaded: don't worry about arg type matching... 080 return true; 081 } 082 // If we get here, we need to insist on precise argument matching... 083 Class<?>[] parameterTypes = method.getParameterTypes(); 084 if (this.typeIdentifiers.size() != parameterTypes.length) { 085 return false; 086 } 087 for (int i = 0; i < this.typeIdentifiers.size(); i++) { 088 String identifier = this.typeIdentifiers.get(i); 089 if (!parameterTypes[i].getName().contains(identifier)) { 090 return false; 091 } 092 } 093 return true; 094 } 095 096 097 @Override 098 public boolean equals(Object other) { 099 if (!(other instanceof ReplaceOverride) || !super.equals(other)) { 100 return false; 101 } 102 ReplaceOverride that = (ReplaceOverride) other; 103 return (ObjectUtils.nullSafeEquals(this.methodReplacerBeanName, that.methodReplacerBeanName) && 104 ObjectUtils.nullSafeEquals(this.typeIdentifiers, that.typeIdentifiers)); 105 } 106 107 @Override 108 public int hashCode() { 109 int hashCode = super.hashCode(); 110 hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.methodReplacerBeanName); 111 hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.typeIdentifiers); 112 return hashCode; 113 } 114 115 @Override 116 public String toString() { 117 return "Replace override for method '" + getMethodName() + "'"; 118 } 119 120}