KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > plugins > proxygen > MethodHelper


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: MethodHelper.java,v 1.2 2005/04/02 13:33:44 tanderson Exp $
44  */

45 package org.exolab.jms.plugins.proxygen;
46
47 import java.lang.reflect.Method JavaDoc;
48 import java.util.ArrayList JavaDoc;
49
50
51 /**
52  * Helper class for performing reflection.
53  * <strongWarning</strong> - this is a direct copy of
54  * org.exolab.jms.net.util.MethodHelper to avoid cyclic dependencies in the build.
55  *
56  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
57  * @version $Revision: 1.2 $ $Date: 2005/04/02 13:33:44 $
58  */

59 public final class MethodHelper {
60
61     /**
62      * Prevent construction of helper class.
63      */

64     private MethodHelper() {
65     }
66
67     /**
68      * Returns all the interface-declared methods of a class.
69      * This includes those implemented by superclasses.
70      *
71      * @param clazz the class
72      * @return a list of the interface-declared methods for <code>clazz</code>
73      */

74     public static Method JavaDoc[] getAllInterfaceMethods(Class JavaDoc clazz) {
75         final int size = 10;
76         ArrayList JavaDoc result = new ArrayList JavaDoc(size);
77         getInterfaceMethods(getAllInterfaces(clazz), result);
78         return (Method JavaDoc[]) result.toArray(new Method JavaDoc[0]);
79     }
80
81     /**
82      * Returns the interface-declared methods of a class.
83      *
84      * @param clazz the class
85      * @return a list of the interface-declared methods for <code>clazz</code>
86      */

87     public static Method JavaDoc[] getInterfaceMethods(Class JavaDoc clazz) {
88         final int size = 10;
89         ArrayList JavaDoc result = new ArrayList JavaDoc(size);
90         getInterfaceMethods(clazz.getInterfaces(), result);
91         return (Method JavaDoc[]) result.toArray(new Method JavaDoc[0]);
92     }
93
94     /**
95      * Calculates a unique identifier for a method. The identifier is unique
96      * within the declaring class.
97      *
98      * @param method the method
99      * @return a unique identifier for <code>method</code>
100      */

101     public static long getMethodID(Method JavaDoc method) {
102         final int shift = 32;
103         long hash = method.getDeclaringClass().getName().hashCode();
104         hash ^= method.getName().hashCode();
105         hash ^= method.getReturnType().getName().hashCode();
106         Class JavaDoc[] args = method.getParameterTypes();
107         for (int i = 0; i < args.length; ++i) {
108             hash ^= ((long) args[i].getName().hashCode()) << shift;
109         }
110         return hash;
111     }
112
113     /**
114      * Determines all of the interface-declared methods from a set of interfaces
115      * and their super-interfaces.
116      *
117      * @param interfaces the interfaces
118      * @param result the array to contain the result
119      */

120     private static void getInterfaceMethods(Class JavaDoc[] interfaces,
121                                             ArrayList JavaDoc result) {
122         for (int i = 0; i < interfaces.length; ++i) {
123             Class JavaDoc iface = interfaces[i];
124             getInterfaceMethods(iface.getInterfaces(), result);
125             Method JavaDoc[] methods = iface.getMethods();
126             for (int j = 0; j < methods.length; ++j) {
127                 if (methods[j].getDeclaringClass() == interfaces[i]) {
128                     result.add(methods[j]);
129                 }
130             }
131         }
132     }
133
134     /**
135      * Returns all of the interfaces implemented by a class.
136      *
137      * @param clazz the class
138      * @return a list of the interfaces implemented by the class.
139      */

140     public static Class JavaDoc[] getAllInterfaces(Class JavaDoc clazz) {
141         ArrayList JavaDoc result = new ArrayList JavaDoc();
142         getAllInterfaces(clazz, result);
143         return (Class JavaDoc[]) result.toArray(new Class JavaDoc[0]);
144     }
145
146     /**
147      * Returns all of the interfaces inplemented by a class.
148
149      * @param clazz the class
150      * @param result the array to contain the result
151      */

152     private static void getAllInterfaces(Class JavaDoc clazz, ArrayList JavaDoc result) {
153         Class JavaDoc[] interfaces = clazz.getInterfaces();
154         for (int i = 0; i < interfaces.length; ++i) {
155             if (!result.contains(interfaces[i])) {
156                 result.add(interfaces[i]);
157             }
158         }
159         Class JavaDoc superClass = clazz.getSuperclass();
160         if (superClass != null) {
161             getAllInterfaces(superClass, result);
162         }
163     }
164 }
165
Popular Tags