1 6 7 package org.jfox.ioc.util; 8 9 import java.lang.reflect.Method ; 10 import java.lang.reflect.Modifier ; 11 import java.security.MessageDigest ; 12 import java.security.NoSuchAlgorithmException ; 13 14 17 18 public class Methods { 19 20 public final static Method ToString; 21 public final static Method HashCode; 22 public final static Method Equals; 23 24 static { 25 try { 26 Class ejbObjectClass = Object .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 []{Object .class}); 30 } 31 catch(Exception e) { 32 e.printStackTrace(); 33 throw new RuntimeException (e.getMessage()); 34 } 35 }; 36 37 38 private static MessageDigest md5 = null; 39 40 static { 41 try { 42 md5 = MessageDigest.getInstance("MD5"); 43 } 44 catch(NoSuchAlgorithmException e) { 45 throw new RuntimeException (e.getMessage()); 46 } 47 } 48 49 55 public static long getMethodHash(Method method) { 56 long hash = 0; 57 Class [] parameterTypes = method.getParameterTypes(); 58 String 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 e) { 71 e.printStackTrace(); 72 } 73 return hash; 74 } 75 76 private static String getTypeString(Class 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 120 public static boolean isDeclaredBy(Method method, Class clz) { 121 Class declaredClass = method.getDeclaringClass(); 122 if(declaredClass == clz) { 123 return true; 124 } 125 else { 126 if(clz.isAssignableFrom(declaredClass)) { 128 try { 129 clz.getMethod(method.getName(), method.getParameterTypes()); 130 return true; 131 } 132 catch(NoSuchMethodException e) { 133 } 134 } 135 } 136 return false; 137 } 138 139 public static boolean isGetMethod(Method 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 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 method) { 162 return Modifier.isStatic(method.getModifiers()); 163 } 164 165 public static boolean isPrivate(Method method) { 166 return Modifier.isPrivate(method.getModifiers()); 167 } 168 169 public static boolean isAbstract(Method method) { 170 return Modifier.isAbstract(method.getModifiers()); 171 } 172 173 public static void main(String [] args) { 174 175 } 176 } 177 178 | Popular Tags |