1 22 package org.jboss.aop.util; 23 24 import javassist.CtClass; 25 import javassist.CtConstructor; 26 import javassist.CtMethod; 27 import javassist.NotFoundException; 28 29 30 import java.io.ByteArrayOutputStream ; 31 import java.io.DataOutputStream ; 32 import java.io.IOException ; 33 import java.security.DigestOutputStream ; 34 import java.security.MessageDigest ; 35 import java.security.NoSuchAlgorithmException ; 36 import java.util.HashMap ; 37 38 48 public class JavassistMethodHashing 49 { 50 public static long methodHash(CtMethod method) 51 { 52 try 53 { 54 CtClass[] parameterTypes = method.getParameterTypes(); 55 String methodDesc = method.getName()+"("; 56 for(int j = 0; j < parameterTypes.length; j++) 57 { 58 methodDesc += getTypeString(parameterTypes[j]); 59 } 60 methodDesc += ")"+getTypeString(method.getReturnType()); 61 62 long hash = 0; 63 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream (512); 64 MessageDigest messagedigest = MessageDigest.getInstance("SHA"); 65 DataOutputStream dataoutputstream = new DataOutputStream (new DigestOutputStream (bytearrayoutputstream, messagedigest)); 66 dataoutputstream.writeUTF(methodDesc); 67 dataoutputstream.flush(); 68 byte abyte0[] = messagedigest.digest(); 69 for(int j = 0; j < Math.min(8, abyte0.length); j++) 70 hash += (long)(abyte0[j] & 0xff) << j * 8; 71 return hash; 72 } 73 catch (Exception e) 74 { 75 throw new RuntimeException (e); 76 } 77 } 78 79 public static long constructorHash(CtConstructor method) 80 { 81 try 82 { 83 CtClass[] parameterTypes = method.getParameterTypes(); 84 String methodDesc = method.getDeclaringClass().getName()+"("; 85 for(int j = 0; j < parameterTypes.length; j++) 86 { 87 methodDesc += getTypeString(parameterTypes[j]); 88 } 89 methodDesc += ")"; 90 91 long hash = 0; 92 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream (512); 93 MessageDigest messagedigest = MessageDigest.getInstance("SHA"); 94 DataOutputStream dataoutputstream = new DataOutputStream (new DigestOutputStream (bytearrayoutputstream, messagedigest)); 95 dataoutputstream.writeUTF(methodDesc); 96 dataoutputstream.flush(); 97 byte abyte0[] = messagedigest.digest(); 98 for(int j = 0; j < Math.min(8, abyte0.length); j++) 99 hash += (long)(abyte0[j] & 0xff) << j * 8; 100 return hash; 101 } 102 catch (Exception e) 103 { 104 throw new RuntimeException (e); 105 } 106 } 107 108 static String getTypeString(CtClass cl) 109 throws Exception 110 { 111 if (cl.equals(CtClass.byteType)) 112 { 113 return "B"; 114 } else if (cl.equals(CtClass.charType)) 115 { 116 return "C"; 117 } else if (cl.equals(CtClass.doubleType)) 118 { 119 return "D"; 120 } else if (cl.equals(CtClass.floatType)) 121 { 122 return "F"; 123 } else if (cl.equals(CtClass.intType)) 124 { 125 return "I"; 126 } else if (cl.equals(CtClass.longType)) 127 { 128 return "J"; 129 } else if (cl.equals(CtClass.shortType)) 130 { 131 return "S"; 132 } else if (cl.equals(CtClass.booleanType)) 133 { 134 return "Z"; 135 } else if (cl.equals(CtClass.voidType)) 136 { 137 return "V"; 138 } else if (cl.isArray()) 139 { 140 return "["+getTypeString(cl.getComponentType()); 141 } else 142 { 143 return "L"+cl.getName().replace('.','/')+";"; 144 } 145 } 146 147 private static void addDeclaredMethods(HashMap advised, CtClass superclass) throws Exception 148 { 149 CtMethod[] declaredMethods = superclass.getDeclaredMethods(); 150 for (int i = 0; i < declaredMethods.length; i++) 151 { 152 if (superclass.isInterface() || Advisable.isAdvisable(declaredMethods[i])) 153 { 154 long hash = methodHash(declaredMethods[i]); 155 advised.put(new Long (hash), declaredMethods[i]); 156 } 157 } 158 } 159 private static void populateMethodTables(HashMap advised, CtClass superclass) 160 throws Exception 161 { 162 if (superclass == null) return; 163 if (superclass.getName().equals("java.lang.Object")) return; 164 165 populateMethodTables(advised, superclass.getSuperclass()); 166 addDeclaredMethods(advised, superclass); 167 } 168 169 public static HashMap getMethodMap(CtClass clazz) throws Exception 170 { 171 HashMap map = new HashMap (); 172 populateMethodTables(map, clazz); 173 return map; 174 } 175 176 public static HashMap getDeclaredMethodMap(CtClass clazz) throws Exception 177 { 178 HashMap map = new HashMap (); 179 addDeclaredMethods(map, clazz); 180 return map; 181 } 182 183 } 184 | Popular Tags |