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;
020import java.lang.reflect.Modifier;
021
022import org.springframework.util.ObjectUtils;
023
024/**
025 * Represents an override of a method that looks up an object in the same IoC context.
026 *
027 * <p>Methods eligible for lookup override must not have arguments.
028 *
029 * @author Rod Johnson
030 * @author Juergen Hoeller
031 * @since 1.1
032 */
033public class LookupOverride extends MethodOverride {
034
035        private final String beanName;
036
037        private Method method;
038
039
040        /**
041         * Construct a new LookupOverride.
042         * @param methodName the name of the method to override
043         * @param beanName the name of the bean in the current {@code BeanFactory}
044         * that the overridden method should return (may be {@code null})
045         */
046        public LookupOverride(String methodName, String beanName) {
047                super(methodName);
048                this.beanName = beanName;
049        }
050
051        /**
052         * Construct a new LookupOverride.
053         * @param method the method to override
054         * @param beanName the name of the bean in the current {@code BeanFactory}
055         * that the overridden method should return (may be {@code null})
056         */
057        public LookupOverride(Method method, String beanName) {
058                super(method.getName());
059                this.method = method;
060                this.beanName = beanName;
061        }
062
063
064        /**
065         * Return the name of the bean that should be returned by this method.
066         */
067        public String getBeanName() {
068                return this.beanName;
069        }
070
071        /**
072         * Match the specified method by {@link Method} reference or method name.
073         * <p>For backwards compatibility reasons, in a scenario with overloaded
074         * non-abstract methods of the given name, only the no-arg variant of a
075         * method will be turned into a container-driven lookup method.
076         * <p>In case of a provided {@link Method}, only straight matches will
077         * be considered, usually demarcated by the {@code @Lookup} annotation.
078         */
079        @Override
080        public boolean matches(Method method) {
081                if (this.method != null) {
082                        return method.equals(this.method);
083                }
084                else {
085                        return (method.getName().equals(getMethodName()) && (!isOverloaded() ||
086                                        Modifier.isAbstract(method.getModifiers()) || method.getParameterTypes().length == 0));
087                }
088        }
089
090
091        @Override
092        public boolean equals(Object other) {
093                if (!(other instanceof LookupOverride) || !super.equals(other)) {
094                        return false;
095                }
096                LookupOverride that = (LookupOverride) other;
097                return (ObjectUtils.nullSafeEquals(this.method, that.method) &&
098                                ObjectUtils.nullSafeEquals(this.beanName, that.beanName));
099        }
100
101        @Override
102        public int hashCode() {
103                return (29 * super.hashCode() + ObjectUtils.nullSafeHashCode(this.beanName));
104        }
105
106        @Override
107        public String toString() {
108                return "LookupOverride for method '" + getMethodName() + "'";
109        }
110
111}