1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveutils.util; |
16 | |
17 | import java.lang.reflect.Method; |
18 | import java.util.HashMap; |
19 | import java.util.Map; |
20 | |
21 | /** |
22 | * Default implementation to generate a mapping between public methods of a |
23 | * source class (or interface) into equivalent methods (same name, same |
24 | * arguments, compatible returned type, but possibly different thrown |
25 | * exceptions). |
26 | * |
27 | * @author Jean-Francois Poilpret |
28 | */ |
29 | public class DefaultMethodMappingBuilder implements MethodMappingBuilder |
30 | { |
31 | /** |
32 | * The method analyses all public methods of source class. For each method: |
33 | * <ul> |
34 | * <li>search a public method with the name and arguments list in target</li> |
35 | * <li>check the returned type of the target method is assignable to the |
36 | * the returned type of the source method</li> |
37 | * </ul> |
38 | * <p> |
39 | * If a <code>source</code> method has no <code>target</code> method with |
40 | * matches for name and arguments, then a warning is generated and that |
41 | * method will not appear in the returned <code>Map</code>. |
42 | */ |
43 | public Map<Method, Method> buildMapping( Class source, |
44 | Class target, |
45 | MethodMappingErrorHandler handler) |
46 | { |
47 | Map<Method, Method> methodsMap = new HashMap<Method, Method>(); |
48 | for (Method src: source.getMethods()) |
49 | { |
50 | Method tgt = mapMethod(src, target, handler); |
51 | if (tgt != null) |
52 | { |
53 | methodsMap.put(src, tgt); |
54 | } |
55 | } |
56 | return methodsMap; |
57 | } |
58 | |
59 | protected Method mapMethod(Method source, |
60 | Class targetClass, |
61 | MethodMappingErrorHandler handler) |
62 | { |
63 | try |
64 | { |
65 | // Find equivalent method in implementation |
66 | Method target = targetClass.getMethod( source.getName(), |
67 | source.getParameterTypes()); |
68 | // Check return types are compatible |
69 | Class<?> srcReturn = source.getReturnType(); |
70 | Class<?> tgtReturn = target.getReturnType(); |
71 | if ( srcReturn.isAssignableFrom(tgtReturn) |
72 | || handler.handleIncompatibleMethods(source, target)) |
73 | { |
74 | return target; |
75 | } |
76 | return null; |
77 | } |
78 | catch (NoSuchMethodException e) |
79 | { |
80 | handler.handleNonExistingMethod(source, targetClass); |
81 | return null; |
82 | } |
83 | } |
84 | } |