1 22 package org.jboss.reflect.spi; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.DataOutputStream ; 26 import java.security.DigestOutputStream ; 27 import java.security.MessageDigest ; 28 29 35 public class MethodInfoHashing 36 { 37 public static long methodHash(MethodInfo method) 38 throws Exception 39 { 40 TypeInfo[] parameterTypes = method.getParameterTypes(); 41 String methodDesc = method.getName() + "("; 42 for (int j = 0; j < parameterTypes.length; j++) 43 { 44 methodDesc += getTypeString(parameterTypes[j]); 45 } 46 methodDesc += ")" + getTypeString(method.getReturnType()); 47 return createHash(methodDesc); 48 } 49 50 public static long createHash(String methodDesc) 51 throws Exception 52 { 53 long hash = 0; 54 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream (512); 55 MessageDigest messagedigest = MessageDigest.getInstance("SHA"); 56 DataOutputStream dataoutputstream = new DataOutputStream (new DigestOutputStream (bytearrayoutputstream, messagedigest)); 57 dataoutputstream.writeUTF(methodDesc); 58 dataoutputstream.flush(); 59 byte abyte0[] = messagedigest.digest(); 60 for (int j = 0; j < Math.min(8, abyte0.length); j++) 61 hash += (long) (abyte0[j] & 0xff) << j * 8; 62 return hash; 63 64 } 65 66 static String getTypeString(TypeInfo cl) 67 { 68 69 if (cl == PrimitiveInfo.BYTE) 70 { 71 return "B"; 72 } 73 else if (cl == PrimitiveInfo.CHAR) 74 { 75 return "C"; 76 } 77 else if (cl == PrimitiveInfo.DOUBLE) 78 { 79 return "D"; 80 } 81 else if (cl == PrimitiveInfo.FLOAT) 82 { 83 return "F"; 84 } 85 else if (cl == PrimitiveInfo.INT) 86 { 87 return "I"; 88 } 89 else if (cl == PrimitiveInfo.LONG) 90 { 91 return "J"; 92 } 93 else if (cl == PrimitiveInfo.SHORT) 94 { 95 return "S"; 96 } 97 else if (cl == PrimitiveInfo.BOOLEAN) 98 { 99 return "Z"; 100 } 101 else if (cl == PrimitiveInfo.VOID) 102 { 103 return "V"; 104 } 105 else if (cl instanceof ArrayInfo) 106 { 107 ArrayInfo ai = (ArrayInfo) cl; 108 return "[" + getTypeString(ai.getComponentType()); 109 } 110 else 111 { 112 return "L" + cl.getName().replace('.', '/') + ";"; 113 } 114 } 115 } 116 | Popular Tags |