1 package org.ozoneDB.core.helper; 2 3 import org.ozoneDB.core.MethodKey; 4 5 import java.lang.reflect.Method ; 6 import java.util.Set ; 7 import java.util.Iterator ; 8 import java.util.TreeSet ; 9 10 17 public class ReflectionHelper 18 { 19 public final static String SIGNATURE_DELIMITER ="|"; 20 21 public static String signature(Class [] args) { 22 String result = new String (); 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 signature(String [] args) { 31 String result = new String (); 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 43 public static int methodArrayIndex(Method [] methods, Method m) { 44 String mName = m.getName(); 45 String mSig = ReflectionHelper.signature(m.getParameterTypes()); 46 47 for (int i = 0; i < methods.length; i++) { 48 String cName = methods[i].getName(); 49 String cSig = ReflectionHelper.signature(methods[i].getParameterTypes()); 50 if (mName.equals(cName) && mSig.equals(cSig)) { 51 return i; 52 } 53 } 54 throw new RuntimeException (m + ": Unable to find method in class."); 55 } 56 57 public static Method [] methodsOfClass(Class cl) { 58 Method [] methods = cl.getMethods(); 60 61 Set set = new TreeSet (); 62 63 for (int i = 0; i < methods.length; i++) { 64 String name = methods[i].getName(); 65 String sig = ReflectionHelper.signature(methods[i].getParameterTypes()); 66 set.add(new MethodKey(cl.getName(), name, sig, methods[i])); 67 } 68 69 Iterator 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 |