KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > helper > ReflectionHelper


1 package org.ozoneDB.core.helper;
2
3 import org.ozoneDB.core.MethodKey;
4
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.Set JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.TreeSet JavaDoc;
9
10 /**
11  * Created by IntelliJ IDEA.
12  * User: administrator
13  * Date: 2003-sep-13
14  * Time: 20:18:05
15  * To change this template use Options | File Templates.
16  */

17 public class ReflectionHelper
18 {
19   public final static String JavaDoc SIGNATURE_DELIMITER ="|";
20
21   public static String JavaDoc signature(Class JavaDoc[] args) {
22       String JavaDoc result = new String JavaDoc();
23       for (int i = 0; i < args.length; i++) {
24           result = i > 0 ? result + SIGNATURE_DELIMITER : result;
25           result = result + args[i].getName();
26       }
27       return "\"" + result + "\"";
28   }
29
30   public static String JavaDoc signature(String JavaDoc[] args) {
31       String JavaDoc result = new String JavaDoc();
32       for (int i = 0; i < args.length; i++) {
33           result = i > 0 ? result + SIGNATURE_DELIMITER : result;
34           result = result + args[i];
35       }
36       return "\"" + result + "\"";
37   }
38
39   /**
40    * Returns the array index of the specified method within the array
41    * of methods of this class.
42    */

43   public static int methodArrayIndex(Method JavaDoc[] methods, Method JavaDoc m) {
44       String JavaDoc mName = m.getName();
45       String JavaDoc mSig = ReflectionHelper.signature(m.getParameterTypes());
46
47       for (int i = 0; i < methods.length; i++) {
48           String JavaDoc cName = methods[i].getName();
49           String JavaDoc cSig = ReflectionHelper.signature(methods[i].getParameterTypes());
50           if (mName.equals(cName) && mSig.equals(cSig)) {
51               return i;
52           }
53       }
54       throw new RuntimeException JavaDoc(m + ": Unable to find method in class.");
55   }
56
57   public static Method JavaDoc[] methodsOfClass(Class JavaDoc cl) {
58         //System.out.println("Methods of class: "+cl);
59
Method JavaDoc[] methods = cl.getMethods();
60
61         Set JavaDoc set = new TreeSet JavaDoc();
62
63         for (int i = 0; i < methods.length; i++) {
64             String JavaDoc name = methods[i].getName();
65             String JavaDoc sig = ReflectionHelper.signature(methods[i].getParameterTypes());
66             set.add(new MethodKey(cl.getName(), name, sig, methods[i]));
67         }
68
69         Iterator JavaDoc it = set.iterator();
70         for (int i = 0; it.hasNext(); i++) {
71             MethodKey key = (MethodKey) it.next();
72             methods[i] = key.method();
73         }
74         return methods;
75     }
76 }
77
Popular Tags