001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.util;
008:
009: import java.lang.reflect.Method;
010: import java.lang.reflect.Modifier;
011: import java.security.MessageDigest;
012: import java.security.NoSuchAlgorithmException;
013: import java.util.Map;
014: import java.util.HashMap;
015:
016: /**
017: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
018: */
019: public class MethodUtils {
020: private static MessageDigest md5 = null;
021:
022: static {
023: try {
024: md5 = MessageDigest.getInstance("MD5");
025: } catch (NoSuchAlgorithmException e) {
026: throw new RuntimeException(e.getMessage());
027: }
028: }
029:
030: private static Map<Long, Method> objectMethods = new HashMap<Long, Method>();
031:
032: static {
033: try {
034: Method[] methods = Object.class.getDeclaredMethods();
035: for (Method m : methods) {
036: objectMethods.put(getMethodHash(m), m);
037: }
038: } catch (Exception e) {
039: e.printStackTrace();
040: throw new RuntimeException(e.getMessage());
041: }
042: }
043:
044: /**
045: * 是å?¦æ˜¯å®šä¹‰åœ¨ Object ä¸çš„方法
046: * @param method 方法
047: */
048: public static boolean isObjectMethod(Method method) {
049: return objectMethods.containsKey(getMethodHash(method));
050: }
051:
052: /**
053: * get the exclusive method hash code
054: *
055: * @param method method
056: */
057: public static long getMethodHash(Method method) {
058: long hash = 0;
059: Class[] parameterTypes = method.getParameterTypes();
060: String methodDesc = method.getName() + "(";
061: for (Class type : parameterTypes) {
062: methodDesc += getTypeString(type);
063: }
064: methodDesc += ")" + getTypeString(method.getReturnType());
065:
066: try {
067: byte[] bytes = md5
068: .digest(methodDesc.getBytes("ISO-8859-1"));
069: for (int j = 0; j < Math.min(8, bytes.length); j++) {
070: hash += (long) (bytes[j] & 0xff) << j * 8;
071: }
072: } catch (Exception e) {
073: e.printStackTrace();
074: }
075: return hash;
076: }
077:
078: private static String getTypeString(Class cl) {
079: if (cl == Byte.TYPE) {
080: return "B";
081: } else if (cl == Character.TYPE) {
082: return "C";
083: } else if (cl == Double.TYPE) {
084: return "D";
085: } else if (cl == Float.TYPE) {
086: return "F";
087: } else if (cl == Integer.TYPE) {
088: return "I";
089: } else if (cl == Long.TYPE) {
090: return "J";
091: } else if (cl == Short.TYPE) {
092: return "S";
093: } else if (cl == Boolean.TYPE) {
094: return "Z";
095: } else if (cl == Void.TYPE) {
096: return "V";
097: } else if (cl.isArray()) {
098: return "[" + getTypeString(cl.getComponentType());
099: } else {
100: return "L" + cl.getName().replace('.', '/') + ";";
101: }
102: }
103:
104: /**
105: * is defined in this clazz or in it's super interface or super class
106: *
107: * @param method method
108: * @param clazz class
109: */
110: public static boolean isDeclaredBy(Method method, Class clazz) {
111: Class declaredClass = method.getDeclaringClass();
112: if (declaredClass == clazz) {
113: return true;
114: } else {
115: // defined in super interface or super class
116: if (clazz.isAssignableFrom(declaredClass)) {
117: try {
118: clazz.getMethod(method.getName(), method
119: .getParameterTypes());
120: return true;
121: } catch (NoSuchMethodException e) {
122: return false;
123: }
124: } else {
125: return false;
126: }
127: }
128:
129: }
130:
131: public static boolean isGetMethod(Method method) {
132: if (((method.getName().startsWith("get") && !method.getName()
133: .equals("get")) || (method.getName().startsWith("is") && !method
134: .getName().equals("is")))
135: && method.getReturnType() != void.class
136: && method.getParameterTypes().length == 0) {
137: return true;
138: }
139: return false;
140: }
141:
142: public static boolean isSetMethod(Method method) {
143: if (method.getName().startsWith("set")
144: && !method.getName().equals("set")
145: && method.getReturnType() == void.class
146: && method.getParameterTypes().length == 1) {
147: return true;
148: }
149: return false;
150: }
151:
152: public static boolean isStatic(Method method) {
153: return Modifier.isStatic(method.getModifiers());
154: }
155:
156: public static boolean isPrivate(Method method) {
157: return Modifier.isPrivate(method.getModifiers());
158: }
159:
160: public static boolean isAbstract(Method method) {
161: return Modifier.isAbstract(method.getModifiers());
162: }
163:
164: public static void main(String[] args) {
165: MethodUtils.getTypeString(Object.class);
166: }
167: }
|