KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > util > JavassistMethodHashing


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.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 JavaDoc;
31 import java.io.DataOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.security.DigestOutputStream JavaDoc;
34 import java.security.MessageDigest JavaDoc;
35 import java.security.NoSuchAlgorithmException JavaDoc;
36 import java.util.HashMap JavaDoc;
37
38 /**
39  * Create a unique hash for method. This is the same as
40  * common: org.jboss.util.MethodHashing except that
41  * it's using Javasssit constructs rather than java.lang.reflect.
42  *
43  *
44  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
45  * @author <a HREF="mailto:marc@jboss.org">Marc Fleury</a>
46  * @version $Revision: 45806 $
47  */

48 public class JavassistMethodHashing
49 {
50    public static long methodHash(CtMethod method)
51    {
52       try
53       {
54          CtClass[] parameterTypes = method.getParameterTypes();
55          String JavaDoc 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 JavaDoc bytearrayoutputstream = new ByteArrayOutputStream JavaDoc(512);
64          MessageDigest JavaDoc messagedigest = MessageDigest.getInstance("SHA");
65          DataOutputStream JavaDoc dataoutputstream = new DataOutputStream JavaDoc(new DigestOutputStream JavaDoc(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 JavaDoc e)
74       {
75          throw new RuntimeException JavaDoc(e);
76       }
77    }
78
79    public static long constructorHash(CtConstructor method)
80    {
81       try
82       {
83          CtClass[] parameterTypes = method.getParameterTypes();
84          String JavaDoc 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 JavaDoc bytearrayoutputstream = new ByteArrayOutputStream JavaDoc(512);
93          MessageDigest JavaDoc messagedigest = MessageDigest.getInstance("SHA");
94          DataOutputStream JavaDoc dataoutputstream = new DataOutputStream JavaDoc(new DigestOutputStream JavaDoc(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 JavaDoc e)
103       {
104          throw new RuntimeException JavaDoc(e);
105       }
106    }
107
108    static String JavaDoc getTypeString(CtClass cl)
109       throws Exception JavaDoc
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 JavaDoc advised, CtClass superclass) throws Exception JavaDoc
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 JavaDoc(hash), declaredMethods[i]);
156          }
157       }
158    }
159    private static void populateMethodTables(HashMap JavaDoc advised, CtClass superclass)
160       throws Exception JavaDoc
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 JavaDoc getMethodMap(CtClass clazz) throws Exception JavaDoc
170    {
171       HashMap JavaDoc map = new HashMap JavaDoc();
172       populateMethodTables(map, clazz);
173       return map;
174    }
175
176    public static HashMap JavaDoc getDeclaredMethodMap(CtClass clazz) throws Exception JavaDoc
177    {
178       HashMap JavaDoc map = new HashMap JavaDoc();
179       addDeclaredMethods(map, clazz);
180       return map;
181    }
182
183 }
184
Popular Tags