KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > spi > MethodInfoHashing


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.reflect.spi;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.DataOutputStream JavaDoc;
26 import java.security.DigestOutputStream JavaDoc;
27 import java.security.MessageDigest JavaDoc;
28
29 /**
30  * Create a unique hash for MethodInfo
31  *
32  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
33  * @version $Revision: 37459 $
34  */

35 public class MethodInfoHashing
36 {
37    public static long methodHash(MethodInfo method)
38            throws Exception JavaDoc
39    {
40       TypeInfo[] parameterTypes = method.getParameterTypes();
41       String JavaDoc 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 JavaDoc methodDesc)
51            throws Exception JavaDoc
52    {
53       long hash = 0;
54       ByteArrayOutputStream JavaDoc bytearrayoutputstream = new ByteArrayOutputStream JavaDoc(512);
55       MessageDigest JavaDoc messagedigest = MessageDigest.getInstance("SHA");
56       DataOutputStream JavaDoc dataoutputstream = new DataOutputStream JavaDoc(new DigestOutputStream JavaDoc(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 JavaDoc 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