EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hiveutils.util]

COVERAGE SUMMARY FOR SOURCE FILE [DefaultMethodMappingBuilder.java]

nameclass, %method, %block, %line, %
DefaultMethodMappingBuilder.java100% (1/1)100% (3/3)100% (72/72)100% (16/16)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DefaultMethodMappingBuilder100% (1/1)100% (3/3)100% (72/72)100% (16/16)
DefaultMethodMappingBuilder (): void 100% (1/1)100% (3/3)100% (1/1)
buildMapping (Class, Class, MethodMappingErrorHandler): Map 100% (1/1)100% (36/36)100% (6/6)
mapMethod (Method, Class, MethodMappingErrorHandler): Method 100% (1/1)100% (33/33)100% (9/9)

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 
15package net.sourceforge.hiveutils.util;
16 
17import java.lang.reflect.Method;
18import java.util.HashMap;
19import 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 */
29public 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}

[all classes][net.sourceforge.hiveutils.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov