KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > eireneh > eliza > util > OverloadUtil


1 package com.eireneh.eliza.util;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.util.Arrays JavaDoc;
5
6 /**
7  * @author Rob Sanheim
8  *
9  * A utility to check if one method is overloading another. Note that this
10  * utility does not take into account generics at all, but it should work
11  * correctly for covariant return types.
12  * @link <a * HREF="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#227768">the
13  * JLS</a> for information on overloading
14  *
15  * TODO change to allow parameters to isOverloaded be ordered any way, and make this
16  * class figure out which is the "higher level" method
17  *
18  * TODO change to support generics, particularily the nasty case where an
19  * overloaded or overridden method uses generics and the base method does not -
20  * see <a * HREF="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.3">JLS
21  * again</a>
22  */

23 public class OverloadUtil
24 {
25     /**
26      * Check two methods reflectively to see if one is overloading the other
27      * Note that the parameter ordering is important if one method is higher in
28      * the class hiearchy then the other. If this is the case, make sure the
29      * method higher up in the chain is the first parameter. Otherwise, the
30      * ordering does not matter.
31      *
32      * @param higher
33      * method
34      * @param lower
35      * method
36      * @return
37      */

38     public static boolean isOverloaded(Method JavaDoc higher, Method JavaDoc lower)
39     {
40         if (namesAreEqual(higher, lower) && returnTypesAreEqualOrCovariant(higher, lower) && isNotInterfaceImplementation(higher, lower)
41             && isNotOverridden(higher, lower))
42         {
43             return true;
44
45         }
46         else
47         {
48             return false;
49         }
50     }
51
52     private static boolean isNotOverridden(Method JavaDoc higher, Method JavaDoc lower)
53     {
54         if (isOverridden(higher, lower))
55         {
56             return false;
57         }
58         else
59         {
60             return true;
61         }
62     }
63
64     /**
65      * @param higher
66      * @param lower
67      * @return true if lower overrides higher
68      */

69     private static boolean isOverridden(Method JavaDoc higher, Method JavaDoc lower)
70     {
71         return declaringClassIsAssignableFrom(higher, lower) && declaringClassIsNotAnInterface(higher) && parametersAreEqual(higher, lower);
72     }
73
74     /**
75      * @param first
76      * @param second
77      * @return true if the first method's declaring class is assignable from the
78      * second
79      */

80     private static boolean declaringClassIsAssignableFrom(Method JavaDoc first, Method JavaDoc second)
81     {
82         return first.getDeclaringClass().isAssignableFrom(second.getDeclaringClass());
83
84     }
85
86     /**
87      * We have to make sure we don't mistake standard interface implementation
88      * (where first method is on an interface and the params are equal) for
89      * overloading.
90      *
91      * @param higher
92      * @param lower
93      * @return
94      */

95     private static boolean isNotInterfaceImplementation(Method JavaDoc higher, Method JavaDoc lower)
96     {
97         return !(declaringClassIsAnInterface(higher) && parametersAreEqual(higher, lower));
98     }
99
100     /**
101      * check deep equality on parameters of two methods
102      *
103      * @param first
104      * @param second
105      * @return
106      */

107     private static boolean parametersAreEqual(Method JavaDoc first, Method JavaDoc second)
108     {
109         return Arrays.deepEquals(first.getParameterTypes(), second.getParameterTypes());
110     }
111
112     /**
113      * @param higher
114      * @param lower
115      * @return true if return types are equal or covariants
116      */

117     private static boolean returnTypesAreEqualOrCovariant(Method JavaDoc higher, Method JavaDoc lower)
118     {
119         return (declaringClassIsAssignableFrom(higher, lower) || higher.getReturnType().equals(lower.getReturnType()));
120     }
121
122     /**
123      * @param first
124      * @param second
125      * @return true if the names of the two methods are equal
126      */

127     private static boolean namesAreEqual(Method JavaDoc first, Method JavaDoc second)
128     {
129         return first.getName().equals(second.getName());
130     }
131
132     private static boolean declaringClassIsAnInterface(Method JavaDoc method)
133     {
134         return method.getDeclaringClass().isInterface();
135     }
136
137     private static boolean declaringClassIsNotAnInterface(Method JavaDoc method)
138     {
139         return !declaringClassIsAnInterface(method);
140     }
141
142 }
143
Popular Tags