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