KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.DataOutputStream JavaDoc;
26 import java.security.DigestOutputStream JavaDoc;
27 import java.security.MessageDigest JavaDoc;
28 import java.util.HashMap JavaDoc;
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 /**
38  * Experimental version of method hashing for the container Class-/Method-/ConstructorInfos
39  *
40  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
41  * @version $Revision: 46051 $
42  */

43 public class ClassInfoMethodHashing
44 {
45    public static long methodHash(MethodInfo methodInfo)
46    {
47       try
48       {
49          TypeInfo[] parameterTypes = methodInfo.getParameterTypes();
50          String JavaDoc 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 JavaDoc bytearrayoutputstream = new ByteArrayOutputStream JavaDoc(512);
59          MessageDigest JavaDoc messagedigest = MessageDigest.getInstance("SHA");
60          DataOutputStream JavaDoc dataoutputstream = new DataOutputStream JavaDoc(new DigestOutputStream JavaDoc(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 JavaDoc e)
69       {
70          throw new RuntimeException JavaDoc(e);
71       }
72    }
73
74    public static long constructorHash(ConstructorInfo constructorInfo)
75    {
76       try
77       {
78          TypeInfo[] parameterTypes = constructorInfo.getParameterTypes();
79          String JavaDoc 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 JavaDoc bytearrayoutputstream = new ByteArrayOutputStream JavaDoc(512);
88          MessageDigest JavaDoc messagedigest = MessageDigest.getInstance("SHA");
89          DataOutputStream JavaDoc dataoutputstream = new DataOutputStream JavaDoc(new DigestOutputStream JavaDoc(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 JavaDoc e)
98       {
99          throw new RuntimeException JavaDoc(e);
100       }
101    }
102
103    static String JavaDoc getTypeString(TypeInfo cl)
104       throws Exception JavaDoc
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 JavaDoc("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 JavaDoc advised, ClassInfo superclass) throws Exception JavaDoc
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 JavaDoc(hash), declaredMethods[i]);
171             }
172          }
173       }
174    }
175    private static void populateMethodTables(HashMap JavaDoc advised, ClassInfo superclass)
176       throws Exception JavaDoc
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 JavaDoc getMethodMap(ClassInfo clazz) throws Exception JavaDoc
186    {
187       HashMap JavaDoc map = new HashMap JavaDoc();
188       populateMethodTables(map, clazz);
189       return map;
190    }
191
192    public static HashMap JavaDoc getDeclaredMethodMap(ClassInfo clazz) throws Exception JavaDoc
193    {
194       HashMap JavaDoc map = new HashMap JavaDoc();
195       addDeclaredMethods(map, clazz);
196       return map;
197    }
198
199 }
200
Popular Tags