KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > support > ReplaceOverride


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.springframework.util.Assert;
24 import org.springframework.util.ObjectUtils;
25
26 /**
27  * Extension of MethodOverride that represents an arbitrary
28  * override of a method by the IoC container.
29  *
30  * <p>Any non-final method can be overridden, irrespective of its
31  * parameters and return types.
32  *
33  * @author Rod Johnson
34  * @author Juergen Hoeller
35  * @since 1.1
36  */

37 public class ReplaceOverride extends MethodOverride {
38     
39     private final String JavaDoc methodReplacerBeanName;
40     
41     /**
42      * List of String. Identifying signatures.
43      */

44     private List JavaDoc typeIdentifiers = new LinkedList JavaDoc();
45
46
47     /**
48      * Construct a new ReplaceOverride.
49      * @param methodName the name of the method to override
50      * @param methodReplacerBeanName the bean name of the MethodReplacer
51      */

52     public ReplaceOverride(String JavaDoc methodName, String JavaDoc methodReplacerBeanName) {
53         super(methodName);
54         Assert.notNull(methodName, "Method replacer bean name must not be null");
55         this.methodReplacerBeanName = methodReplacerBeanName;
56     }
57
58     /**
59      * Return the name of the bean implementing MethodReplacer.
60      */

61     public String JavaDoc getMethodReplacerBeanName() {
62         return this.methodReplacerBeanName;
63     }
64
65     /**
66      * Add a fragment of a class string, like "Exception"
67      * or "java.lang.Exc", to identify a parameter type.
68      * @param identifier a substring of the fully qualified class name
69      */

70     public void addTypeIdentifier(String JavaDoc identifier) {
71         this.typeIdentifiers.add(identifier);
72     }
73
74
75     public boolean matches(Method JavaDoc method) {
76         // TODO could cache result for efficiency
77
if (!method.getName().equals(getMethodName())) {
78             // It can't match.
79
return false;
80         }
81         
82         if (!isOverloaded()) {
83             // No overloaded: don't worry about arg type matching.
84
return true;
85         }
86         
87         // If we get to here, we need to insist on precise argument matching.
88
if (this.typeIdentifiers.size() != method.getParameterTypes().length) {
89             return false;
90         }
91         for (int i = 0; i < this.typeIdentifiers.size(); i++) {
92             String JavaDoc identifier = (String JavaDoc) this.typeIdentifiers.get(i);
93             if (method.getParameterTypes()[i].getName().indexOf(identifier) == -1) {
94                 // This parameter cannot match.
95
return false;
96             }
97         }
98         return true;
99     }
100
101
102     public String JavaDoc toString() {
103         return "Replace override for method '" + getMethodName() + "; will call bean '" +
104                 this.methodReplacerBeanName + "'";
105     }
106
107     public boolean equals(Object JavaDoc other) {
108         if (!(other instanceof ReplaceOverride) || !super.equals(other)) {
109             return false;
110         }
111         ReplaceOverride that = (ReplaceOverride) other;
112         return (ObjectUtils.nullSafeEquals(this.methodReplacerBeanName, that.methodReplacerBeanName) &&
113                 ObjectUtils.nullSafeEquals(this.typeIdentifiers, that.typeIdentifiers));
114     }
115
116     public int hashCode() {
117         int hashCode = super.hashCode();
118         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.methodReplacerBeanName);
119         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.typeIdentifiers);
120         return hashCode;
121     }
122
123 }
124
Popular Tags