1 package org.myoodb.core; 25 26 import java.util.*; 27 import java.lang.reflect.Method ; 28 29 public final class MethodHelper 30 { 31 public final static String SIGNATURE_DELIMITER =","; 32 33 public static String getSignature(Class [] args) 34 { 35 StringBuilder result = new StringBuilder (); 36 result.append("\""); 37 for (int i = 0; i < args.length; i++) 38 { 39 if (i> 0) 40 { 41 result.append(SIGNATURE_DELIMITER); 42 } 43 result.append(args[i].getName()); 44 } 45 result.append("\""); 46 return result.toString(); 47 } 48 49 public static String getSignature(String [] args) 50 { 51 StringBuilder result = new StringBuilder (); 52 result.append("\""); 53 for (int i = 0; i < args.length; i++) 54 { 55 if (i> 0) 56 { 57 result.append(SIGNATURE_DELIMITER); 58 } 59 result.append(args[i]); 60 } 61 result.append("\""); 62 return result.toString(); 63 } 64 65 public static int getMethodIndex(Method [] methods, Method method) 66 { 67 for (int i = 0; i < methods.length; i++) 68 { 69 if (method.equals(methods[i]) == true) 70 { 71 return i; 72 } 73 } 74 75 throw new org.myoodb.exception.InternalException("MethodHelper.getMethodIndex (unable to find method: " + method + " )"); 76 } 77 78 public static Method [] getMethods(Class classType, Class interfaceType) 79 { 80 Method [] implMethods = classType.getMethods(); 81 Method [] interfaceMethods = interfaceType.getMethods(); 82 83 TreeSet initSet = new TreeSet(); 84 for (int i = 0; i < implMethods.length; i++) 85 { 86 String implName = implMethods[i].getName(); 87 String implSig = MethodHelper.getSignature(implMethods[i].getParameterTypes()); 88 MethodSignature implMethodSig = new MethodSignature(interfaceType.getName(), implName, implSig, implMethods[i]); 89 initSet.add(implMethodSig); 90 } 91 92 TreeMap indexMap = new TreeMap(); 93 for (int j = 0; j < interfaceMethods.length; j++) 94 { 95 String interfaceName = interfaceMethods[j].getName(); 96 String interfaceSig = MethodHelper.getSignature(interfaceMethods[j].getParameterTypes()); 97 MethodSignature interfaceMethodSig = new MethodSignature(interfaceType.getName(), interfaceName, interfaceSig, null); 98 99 org.myoodb.MyOodbIndex index = null; 100 MethodSignature foundMethodSig = null; 101 102 for (int k = 0; k < implMethods.length; k++) 103 { 104 String implName = implMethods[k].getName(); 105 String implSig = MethodHelper.getSignature(implMethods[k].getParameterTypes()); 106 MethodSignature implMethodSig = new MethodSignature(interfaceType.getName(), implName, implSig, implMethods[k]); 107 108 if (interfaceMethodSig.equals(implMethodSig) == true) 109 { 110 foundMethodSig = implMethodSig; 111 index = interfaceMethods[j].getAnnotation(org.myoodb.MyOodbIndex.class); 112 break; 113 } 114 } 115 116 if (index != null) 117 { 118 indexMap.put(new Integer (index.value()), foundMethodSig); 119 initSet.remove(foundMethodSig); 120 } 121 } 122 123 int index = 0; 124 Iterator iter = indexMap.values().iterator(); 125 while (iter.hasNext()) 126 { 127 MethodSignature methodSig = (MethodSignature) iter.next(); 128 implMethods[index++] = methodSig.getMethod(); 129 } 130 131 iter = initSet.iterator(); 132 while (iter.hasNext()) 133 { 134 MethodSignature methodSig = (MethodSignature) iter.next(); 135 implMethods[index++] = methodSig.getMethod(); 136 } 137 138 return implMethods; 139 } 140 } 141 | Popular Tags |