01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.beans.factory.support;
18:
19: import java.lang.reflect.Method;
20:
21: import org.springframework.util.Assert;
22: import org.springframework.util.ObjectUtils;
23:
24: /**
25: * Represents an override of a method that looks up an object in the same IoC context.
26: *
27: * <p>Methods eligible for lookup override must not have arguments.
28: *
29: * @author Rod Johnson
30: * @author Juergen Hoeller
31: * @since 1.1
32: */
33: public class LookupOverride extends MethodOverride {
34:
35: private final String beanName;
36:
37: /**
38: * Construct a new LookupOverride.
39: * @param methodName the name of the method to override.
40: * This method must have no arguments.
41: * @param beanName name of the bean in the current BeanFactory
42: * that the overriden method should return
43: */
44: public LookupOverride(String methodName, String beanName) {
45: super (methodName);
46: Assert.notNull(beanName, "Bean name must not be null");
47: this .beanName = beanName;
48: }
49:
50: /**
51: * Return the name of the bean that should be returned by this method.
52: */
53: public String getBeanName() {
54: return this .beanName;
55: }
56:
57: /**
58: * Match method of the given name, with no parameters.
59: */
60: public boolean matches(Method method) {
61: return (method.getName().equals(getMethodName()) && method
62: .getParameterTypes().length == 0);
63: }
64:
65: public String toString() {
66: return "LookupOverride for method '" + getMethodName()
67: + "'; will return bean '" + this .beanName + "'";
68: }
69:
70: public boolean equals(Object other) {
71: return (other instanceof LookupOverride && super .equals(other) && ObjectUtils
72: .nullSafeEquals(this .beanName,
73: ((LookupOverride) other).beanName));
74: }
75:
76: public int hashCode() {
77: return (29 * super.hashCode() + ObjectUtils
78: .nullSafeHashCode(this.beanName));
79: }
80:
81: }
|