1 22 package org.jboss.aop.util; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.DataOutputStream ; 26 import java.security.DigestOutputStream ; 27 import java.security.MessageDigest ; 28 import java.util.HashMap ; 29 30 import org.jboss.reflect.spi.ArrayInfo; 31 import org.jboss.reflect.spi.ClassInfo; 32 import org.jboss.reflect.spi.ConstructorInfo; 33 import org.jboss.reflect.spi.MethodInfo; 34 import org.jboss.reflect.spi.PrimitiveInfo; 35 import org.jboss.reflect.spi.TypeInfo; 36 37 43 public class ClassInfoMethodHashing 44 { 45 public static long methodHash(MethodInfo methodInfo) 46 { 47 try 48 { 49 TypeInfo[] parameterTypes = methodInfo.getParameterTypes(); 50 String methodDesc = methodInfo.getName()+"("; 51 for(int j = 0; j < parameterTypes.length; j++) 52 { 53 methodDesc += getTypeString(parameterTypes[j]); 54 } 55 methodDesc += ")"+getTypeString(methodInfo.getReturnType()); 56 57 long hash = 0; 58 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream (512); 59 MessageDigest messagedigest = MessageDigest.getInstance("SHA"); 60 DataOutputStream dataoutputstream = new DataOutputStream (new DigestOutputStream (bytearrayoutputstream, messagedigest)); 61 dataoutputstream.writeUTF(methodDesc); 62 dataoutputstream.flush(); 63 byte abyte0[] = messagedigest.digest(); 64 for(int j = 0; j < Math.min(8, abyte0.length); j++) 65 hash += (long)(abyte0[j] & 0xff) << j * 8; 66 return hash; 67 } 68 catch (Exception e) 69 { 70 throw new RuntimeException (e); 71 } 72 } 73 74 public static long constructorHash(ConstructorInfo constructorInfo) 75 { 76 try 77 { 78 TypeInfo[] parameterTypes = constructorInfo.getParameterTypes(); 79 String methodDesc = constructorInfo.getDeclaringClass().getName()+"("; 80 for(int j = 0; j < parameterTypes.length; j++) 81 { 82 methodDesc += getTypeString(parameterTypes[j]); 83 } 84 methodDesc += ")"; 85 86 long hash = 0; 87 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream (512); 88 MessageDigest messagedigest = MessageDigest.getInstance("SHA"); 89 DataOutputStream dataoutputstream = new DataOutputStream (new DigestOutputStream (bytearrayoutputstream, messagedigest)); 90 dataoutputstream.writeUTF(methodDesc); 91 dataoutputstream.flush(); 92 byte abyte0[] = messagedigest.digest(); 93 for(int j = 0; j < Math.min(8, abyte0.length); j++) 94 hash += (long)(abyte0[j] & 0xff) << j * 8; 95 return hash; 96 } 97 catch (Exception e) 98 { 99 throw new RuntimeException (e); 100 } 101 } 102 103 static String getTypeString(TypeInfo cl) 104 throws Exception 105 { 106 if (cl instanceof PrimitiveInfo) 107 { 108 if (cl.equals(PrimitiveInfo.BYTE)) 109 { 110 return "B"; 111 } 112 else if (cl.equals(PrimitiveInfo.CHAR)) 113 { 114 return "C"; 115 } 116 else if (cl.equals(PrimitiveInfo.DOUBLE)) 117 { 118 return "D"; 119 } 120 else if (cl.equals(PrimitiveInfo.FLOAT)) 121 { 122 return "F"; 123 } 124 else if (cl.equals(PrimitiveInfo.INT)) 125 { 126 return "I"; 127 } 128 else if (cl.equals(PrimitiveInfo.LONG)) 129 { 130 return "J"; 131 } 132 else if (cl.equals(PrimitiveInfo.SHORT)) 133 { 134 return "S"; 135 } 136 else if (cl.equals(PrimitiveInfo.BOOLEAN)) 137 { 138 return "Z"; 139 } 140 else if (cl.equals(PrimitiveInfo.VOID)) 141 { 142 return "V"; 143 } 144 else 145 { 146 throw new RuntimeException ("Invalid primitive info " + cl); 147 } 148 } 149 else if (cl.isArray()) 150 { 151 TypeInfo arrayType = ((ArrayInfo)cl).getComponentType(); 152 return "["+getTypeString(arrayType); 153 } 154 else 155 { 156 return "L"+cl.getName().replace('.','/')+";"; 157 } 158 } 159 160 private static void addDeclaredMethods(HashMap advised, ClassInfo superclass) throws Exception 161 { 162 MethodInfo[] declaredMethods = superclass.getDeclaredMethods(); 163 if (declaredMethods != null) 164 { 165 for (int i = 0; i < declaredMethods.length; i++) 166 { 167 if (superclass.isInterface() || Advisable.isAdvisableMethod(declaredMethods[i].getModifiers(), declaredMethods[i].getName())) 168 { 169 long hash = methodHash(declaredMethods[i]); 170 advised.put(new Long (hash), declaredMethods[i]); 171 } 172 } 173 } 174 } 175 private static void populateMethodTables(HashMap advised, ClassInfo superclass) 176 throws Exception 177 { 178 if (superclass == null) return; 179 if (superclass.getName().equals("java.lang.Object")) return; 180 181 populateMethodTables(advised, superclass.getSuperclass()); 182 addDeclaredMethods(advised, superclass); 183 } 184 185 public static HashMap getMethodMap(ClassInfo clazz) throws Exception 186 { 187 HashMap map = new HashMap (); 188 populateMethodTables(map, clazz); 189 return map; 190 } 191 192 public static HashMap getDeclaredMethodMap(ClassInfo clazz) throws Exception 193 { 194 HashMap map = new HashMap (); 195 addDeclaredMethods(map, clazz); 196 return map; 197 } 198 199 } 200
| Popular Tags
|