001/*
002 * Copyright 2002-2014 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;
020
021import org.springframework.beans.BeanMetadataElement;
022import org.springframework.util.Assert;
023import org.springframework.util.ObjectUtils;
024
025/**
026 * Object representing the override of a method on a managed object by the IoC
027 * container.
028 *
029 * <p>Note that the override mechanism is <em>not</em> intended as a generic
030 * means of inserting crosscutting code: use AOP for that.
031 *
032 * @author Rod Johnson
033 * @author Juergen Hoeller
034 * @author Sam Brannen
035 * @since 1.1
036 */
037public abstract class MethodOverride implements BeanMetadataElement {
038
039        private final String methodName;
040
041        private boolean overloaded = true;
042
043        private Object source;
044
045
046        /**
047         * Construct a new override for the given method.
048         * @param methodName the name of the method to override
049         */
050        protected MethodOverride(String methodName) {
051                Assert.notNull(methodName, "Method name must not be null");
052                this.methodName = methodName;
053        }
054
055
056        /**
057         * Return the name of the method to be overridden.
058         */
059        public String getMethodName() {
060                return this.methodName;
061        }
062
063        /**
064         * Set whether the overridden method is <em>overloaded</em> (i.e., whether argument
065         * type matching needs to occur to disambiguate methods of the same name).
066         * <p>Default is {@code true}; can be switched to {@code false} to optimize
067         * runtime performance.
068         */
069        protected void setOverloaded(boolean overloaded) {
070                this.overloaded = overloaded;
071        }
072
073        /**
074         * Return whether the overridden method is <em>overloaded</em> (i.e., whether argument
075         * type matching needs to occur to disambiguate methods of the same name).
076         */
077        protected boolean isOverloaded() {
078                return this.overloaded;
079        }
080
081        /**
082         * Set the configuration source {@code Object} for this metadata element.
083         * <p>The exact type of the object will depend on the configuration mechanism used.
084         */
085        public void setSource(Object source) {
086                this.source = source;
087        }
088
089        @Override
090        public Object getSource() {
091                return this.source;
092        }
093
094        /**
095         * Subclasses must override this to indicate whether they <em>match</em> the
096         * given method. This allows for argument list checking as well as method
097         * name checking.
098         * @param method the method to check
099         * @return whether this override matches the given method
100         */
101        public abstract boolean matches(Method method);
102
103
104        @Override
105        public boolean equals(Object other) {
106                if (this == other) {
107                        return true;
108                }
109                if (!(other instanceof MethodOverride)) {
110                        return false;
111                }
112                MethodOverride that = (MethodOverride) other;
113                return (ObjectUtils.nullSafeEquals(this.methodName, that.methodName) &&
114                                ObjectUtils.nullSafeEquals(this.source, that.source));
115        }
116
117        @Override
118        public int hashCode() {
119                int hashCode = ObjectUtils.nullSafeHashCode(this.methodName);
120                hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.source);
121                return hashCode;
122        }
123
124}