1 19 package org.objectweb.carol.cmi.compiler; 20 21 import java.lang.reflect.Method ; 22 import java.util.ArrayList ; 23 24 28 public class MethodProto { 29 private String methodName; 30 private String returnType; 31 private String [] paramTypes; 32 private int hash; 33 34 public MethodProto(String sign) throws CompilerException { 35 int obr = sign.indexOf('('); 36 if (obr < 0) 37 badSignature(sign); 38 int mns = sign.lastIndexOf(' ', obr); 39 if (mns < 0) 40 badSignature(sign); 41 methodName = sign.substring(mns + 1, obr); 42 int mnd = methodName.lastIndexOf('.'); 43 if (mnd >= 0) { 44 methodName = methodName.substring(mnd + 1); 45 } 46 while (sign.charAt(mns - 1) == ' ') 47 mns--; 48 int rts = sign.lastIndexOf(' ', mns - 1); 49 returnType = sign.substring(rts + 1, mns); 51 int cbr = sign.indexOf(')'); 52 String params = sign.substring(obr + 1, cbr); 53 ArrayList p = new ArrayList (); 54 params = params.trim(); 55 while (!"".equals(params)) { 56 int com = params.indexOf(','); 57 String param; 58 if (com < 0) { 59 param = params; 60 params = ""; 61 } else { 62 param = params.substring(0, com); 63 params = params.substring(com + 1); 64 } 65 int te = param.indexOf(' '); 66 if (te > 0) { 67 param = param.substring(0, te); 68 p.add(param); 69 } else if (te < 0) { 70 p.add(param); 71 } 72 params = params.trim(); 73 } 74 paramTypes = new String [p.size()]; 75 p.toArray(paramTypes); 76 doHash(); 77 } 78 79 public MethodProto(Method m) { 80 returnType = getName(m.getReturnType()); 81 methodName = m.getName(); 82 Class [] params = m.getParameterTypes(); 83 paramTypes = new String [params.length]; 84 for (int i=0; i<params.length; i++) { 85 paramTypes[i] = getName(params[i]); 86 } 87 doHash(); 88 } 89 90 public static String getName(Class c) { 91 String name; 92 if (c.isArray()) { 93 return getName(c.getComponentType()) + "[]"; 94 } else { 95 return c.getName().replace('$', '.'); 96 } 97 } 98 99 102 private void doHash() { 103 hash = returnType.hashCode() + methodName.hashCode(); 104 for (int i = 0; i < paramTypes.length; i++) { 105 hash += paramTypes[i].hashCode(); 106 } 107 } 108 109 112 private void badSignature(String sign) throws CompilerException { 113 throw new CompilerException("Bad method signature : " + sign); 114 } 115 116 public int hashCode() { 117 return hash; 118 } 119 120 public boolean equals(Object obj) { 121 if (obj instanceof MethodProto) { 122 MethodProto mp = (MethodProto) obj; 123 String [] pt = mp.paramTypes; 124 if (methodName.equals(mp.methodName) 125 && (paramTypes.length == pt.length) 126 && returnType.equals(mp.returnType)) { 127 for (int i = 0; i < paramTypes.length; i++) { 128 if (!paramTypes[i].equals(pt[i])) { 129 return false; 130 } 131 } 132 return true; 133 } 134 } 135 return false; 136 } 137 138 public String toString() { 139 String s = returnType + " " + methodName + "("; 140 for (int i = 0; i < paramTypes.length; i++) { 141 if (i != 0) 142 s += ","; 143 s += paramTypes[i]; 144 } 145 return s + ")"; 146 } 147 } 148 | Popular Tags |