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.ArrayList;
021import java.util.List;
022
023import org.springframework.lang.Nullable;
024import org.springframework.util.Assert;
025import org.springframework.util.ObjectUtils;
026
027/**
028 * Extension of MethodOverride that represents an arbitrary
029 * override of a method by the IoC container.
030 *
031 * <p>Any non-final method can be overridden, irrespective of its
032 * parameters and return types.
033 *
034 * @author Rod Johnson
035 * @author Juergen Hoeller
036 * @since 1.1
037 */
038public class ReplaceOverride extends MethodOverride {
039
040        private final String methodReplacerBeanName;
041
042        private final List<String> typeIdentifiers = new ArrayList<>();
043
044
045        /**
046         * Construct a new ReplaceOverride.
047         * @param methodName the name of the method to override
048         * @param methodReplacerBeanName the bean name of the MethodReplacer
049         */
050        public ReplaceOverride(String methodName, String methodReplacerBeanName) {
051                super(methodName);
052                Assert.notNull(methodReplacerBeanName, "Method replacer bean name must not be null");
053                this.methodReplacerBeanName = methodReplacerBeanName;
054        }
055
056
057        /**
058         * Return the name of the bean implementing MethodReplacer.
059         */
060        public String getMethodReplacerBeanName() {
061                return this.methodReplacerBeanName;
062        }
063
064        /**
065         * Add a fragment of a class string, like "Exception"
066         * or "java.lang.Exc", to identify a parameter type.
067         * @param identifier a substring of the fully qualified class name
068         */
069        public void addTypeIdentifier(String identifier) {
070                this.typeIdentifiers.add(identifier);
071        }
072
073
074        @Override
075        public boolean matches(Method method) {
076                if (!method.getName().equals(getMethodName())) {
077                        return false;
078                }
079                if (!isOverloaded()) {
080                        // Not overloaded: don't worry about arg type matching...
081                        return true;
082                }
083                // If we get here, we need to insist on precise argument matching...
084                if (this.typeIdentifiers.size() != method.getParameterCount()) {
085                        return false;
086                }
087                Class<?>[] parameterTypes = method.getParameterTypes();
088                for (int i = 0; i < this.typeIdentifiers.size(); i++) {
089                        String identifier = this.typeIdentifiers.get(i);
090                        if (!parameterTypes[i].getName().contains(identifier)) {
091                                return false;
092                        }
093                }
094                return true;
095        }
096
097
098        @Override
099        public boolean equals(@Nullable Object other) {
100                if (!(other instanceof ReplaceOverride) || !super.equals(other)) {
101                        return false;
102                }
103                ReplaceOverride that = (ReplaceOverride) other;
104                return (ObjectUtils.nullSafeEquals(this.methodReplacerBeanName, that.methodReplacerBeanName) &&
105                                ObjectUtils.nullSafeEquals(this.typeIdentifiers, that.typeIdentifiers));
106        }
107
108        @Override
109        public int hashCode() {
110                int hashCode = super.hashCode();
111                hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.methodReplacerBeanName);
112                hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.typeIdentifiers);
113                return hashCode;
114        }
115
116        @Override
117        public String toString() {
118                return "Replace override for method '" + getMethodName() + "'";
119        }
120
121}