KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > util > Methods


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.util;
8
9 import java.lang.reflect.Method JavaDoc;
10 import java.lang.reflect.Modifier JavaDoc;
11 import java.security.MessageDigest JavaDoc;
12 import java.security.NoSuchAlgorithmException JavaDoc;
13
14 /**
15  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
16  */

17
18 public class Methods {
19
20     public final static Method JavaDoc ToString;
21     public final static Method JavaDoc HashCode;
22     public final static Method JavaDoc Equals;
23
24     static {
25         try {
26             Class JavaDoc ejbObjectClass = Object JavaDoc.class;
27             ToString = ejbObjectClass.getMethod("toString", Classes.EMPTY_CLASS_ARRAY);
28             HashCode = ejbObjectClass.getMethod("hashCode", Classes.EMPTY_CLASS_ARRAY);
29             Equals = ejbObjectClass.getMethod("equals", new Class JavaDoc[]{Object JavaDoc.class});
30         }
31         catch(Exception JavaDoc e) {
32             e.printStackTrace();
33             throw new RuntimeException JavaDoc(e.getMessage());
34         }
35     };
36
37
38     private static MessageDigest JavaDoc md5 = null;
39
40     static {
41         try {
42             md5 = MessageDigest.getInstance("MD5");
43         }
44         catch(NoSuchAlgorithmException JavaDoc e) {
45             throw new RuntimeException JavaDoc(e.getMessage());
46         }
47     }
48
49     /**
50      * get the exclusive method hash code
51      *
52      * @param method
53      * @return
54      */

55     public static long getMethodHash(Method JavaDoc method) {
56         long hash = 0;
57         Class JavaDoc[] parameterTypes = method.getParameterTypes();
58         String JavaDoc methodDesc = method.getName() + "(";
59         for(int j = 0; j < parameterTypes.length; j++) {
60             methodDesc += getTypeString(parameterTypes[j]);
61         }
62         methodDesc += ")" + getTypeString(method.getReturnType());
63
64         try {
65             byte[] bytes = md5.digest(methodDesc.getBytes("ISO-8859-1"));
66             for(int j = 0; j < Math.min(8, bytes.length); j++) {
67                 hash += (long) (bytes[j] & 0xff) << j * 8;
68             }
69         }
70         catch(Exception JavaDoc e) {
71             e.printStackTrace();
72         }
73         return hash;
74     }
75
76     private static String JavaDoc getTypeString(Class JavaDoc cl) {
77         if(cl == Byte.TYPE) {
78             return "B";
79         }
80         else if(cl == Character.TYPE) {
81             return "C";
82         }
83         else if(cl == Double.TYPE) {
84             return "D";
85         }
86         else if(cl == Float.TYPE) {
87             return "F";
88         }
89         else if(cl == Integer.TYPE) {
90             return "I";
91         }
92         else if(cl == Long.TYPE) {
93             return "J";
94         }
95         else if(cl == Short.TYPE) {
96             return "S";
97         }
98         else if(cl == Boolean.TYPE) {
99             return "Z";
100         }
101         else if(cl == Void.TYPE) {
102             return "V";
103         }
104         else if(cl.isArray()) {
105             return "[" + getTypeString(cl.getComponentType());
106         }
107         else {
108             return "L" + cl.getName().replace('.', '/') + ";";
109         }
110     }
111
112     /**
113      * * is defined in this clz
114      * or in it's super interface or super class
115      *
116      * @param method
117      * @param clz
118      * @return
119      */

120     public static boolean isDeclaredBy(Method JavaDoc method, Class JavaDoc clz) {
121         Class JavaDoc declaredClass = method.getDeclaringClass();
122         if(declaredClass == clz) {
123             return true;
124         }
125         else {
126             // defined in super interface or super class
127
if(clz.isAssignableFrom(declaredClass)) {
128                 try {
129                     clz.getMethod(method.getName(), method.getParameterTypes());
130                     return true;
131                 }
132                 catch(NoSuchMethodException JavaDoc e) {
133                 }
134             }
135         }
136         return false;
137     }
138
139     public static boolean isGetMethod(Method JavaDoc method) {
140         if(((method.getName().startsWith("get")
141                 && !method.getName().equals("get"))
142                 || (method.getName().startsWith("is")
143                 && !method.getName().equals("is")))
144                 && method.getReturnType() != void.class
145                 && method.getParameterTypes().length == 0) {
146             return true;
147         }
148         return false;
149     }
150
151     public static boolean isSetMethod(Method JavaDoc method) {
152         if(method.getName().startsWith("set")
153                 && !method.getName().equals("set")
154                 && method.getReturnType() == void.class
155                 && method.getParameterTypes().length == 1) {
156             return true;
157         }
158         return false;
159     }
160
161     public static boolean isStatic(Method JavaDoc method) {
162         return Modifier.isStatic(method.getModifiers());
163     }
164
165     public static boolean isPrivate(Method JavaDoc method) {
166         return Modifier.isPrivate(method.getModifiers());
167     }
168
169     public static boolean isAbstract(Method JavaDoc method) {
170         return Modifier.isAbstract(method.getModifiers());
171     }
172
173     public static void main(String JavaDoc[] args) {
174
175     }
176 }
177
178
Popular Tags