1 18 19 package alt.jiapi.reflect; 20 21 import java.lang.reflect.Method ; 22 23 30 public class SignatureUtil { 31 41 public static String [] toSignature(String [] simpleNames) { 42 String [] signatures = new String [simpleNames.length]; 43 44 for (int i = 0; i < signatures.length; i++) { 45 signatures[i] = toSignature(simpleNames[i]); 46 } 47 48 return signatures; 49 } 50 51 52 58 public static String getSignature(Method m) { 59 Class parameterClasses[] = m.getParameterTypes(); 60 Class returnTypeClass = m.getReturnType(); 61 62 String parameterNames[] = new String [parameterClasses.length]; 63 64 for (int i = 0; i < parameterClasses.length; i++) { 65 parameterNames[i] = parameterClasses[i].getName(); 66 } 67 68 try { 69 return null; 70 } catch (ClassFormatError cfe) { 71 } 73 74 return null; 75 } 76 77 78 86 public static boolean isPrimitive(String simpleName) { 87 if (simpleName.indexOf('.') != -1) { 89 return false; 90 } 91 92 if (simpleName.startsWith("int")) { 93 return true; 94 } 95 else if (simpleName.startsWith("long")) { 96 return true; 97 } 98 else if (simpleName.startsWith("char")) { 99 return true; 100 } 101 else if (simpleName.startsWith("boolean")) { 102 return true; 103 } 104 else if (simpleName.startsWith("byte")) { 105 return true; 106 } 107 else if (simpleName.startsWith("float")) { 108 return true; 109 } 110 else if (simpleName.startsWith("double")) { 111 return true; 112 } 113 114 return false; 115 } 116 117 120 public static String toSignature(String simpleName) { 121 StringBuffer signature = new StringBuffer (); 122 boolean isArray = false; 123 124 for (int i = 0; i < simpleName.length(); i++) { 126 if (simpleName.charAt(i) == '[') { 127 signature.append("["); 128 isArray = true; 129 } 130 } 131 132 if (simpleName.startsWith("int")) { 133 signature.append("I"); 134 } 135 else if (simpleName.startsWith("long")) { 136 signature.append("J"); 137 } 138 else if (simpleName.startsWith("char")) { 139 signature.append("C"); 140 } 141 else if (simpleName.startsWith("boolean")) { 142 signature.append("Z"); 143 } 144 else if (simpleName.startsWith("byte")) { 145 signature.append("B"); 146 } 147 else if (simpleName.startsWith("float")) { 148 signature.append("F"); 149 } 150 else if (simpleName.startsWith("double")) { 151 signature.append("D"); 152 } 153 else if (simpleName.startsWith("void")) { 154 signature.append("V"); 155 } 156 else { 157 signature.append('L'); 159 if (isArray) { 160 simpleName = 161 simpleName.substring(0, simpleName.indexOf('[')).trim(); 162 } 163 164 signature.append(simpleName.replace('.', '/')); 165 signature.append(';'); 166 } 167 168 return signature.toString(); 169 } 170 171 181 public static String [] toSimpleName(String [] signatures) { 182 String [] simpleNames = new String [signatures.length]; 183 184 for (int i = 0; i < simpleNames.length; i++) { 185 simpleNames[i] = toSimpleName(signatures[i]); 186 } 187 188 return simpleNames; 189 } 190 191 194 public static String toSimpleName(String signature) { 195 int index = signature.lastIndexOf('['); 196 String typeName = null; 197 198 if (index != -1) { 199 if (signature.lastIndexOf(';') != -1) { 200 typeName = signature.substring(index + 1, 201 signature.length() - 1); 202 } 203 else { 204 typeName = signature.substring(index + 1, signature.length()); 205 } 206 } 207 else { 208 if (signature.lastIndexOf(';') != -1) { 209 typeName = signature.substring(0, signature.length() - 1); 210 } 211 else { 212 typeName = signature; 213 } 214 } 215 216 char ch = typeName.charAt(0); 217 218 switch (ch) { 219 case 'L': 220 typeName = typeName.substring(1); 221 typeName = typeName.replace('/', '.'); 222 break; 223 case 'I': 224 typeName = "int"; 225 break; 226 case 'J': 227 typeName = "long"; 228 break; 229 case 'C': 230 typeName = "char"; 231 break; 232 case 'Z': 233 typeName = "boolean"; 234 break; 235 case 'B': 236 typeName = "byte"; 237 break; 238 case 'F': 239 typeName = "float"; 240 break; 241 case 'D': 242 typeName = "double"; 243 break; 244 case 'V': 245 typeName = "void"; 246 break; 247 default: 248 System.out.println("error: " + typeName); 249 break; 250 } 251 252 if (index != -1) { 253 StringBuffer sb = new StringBuffer (typeName); 254 255 for (int i = 0; i <= index; i++) { 256 sb.append("[]"); 257 } 258 259 return sb.toString(); 260 } 261 262 return typeName; 263 } 264 265 273 public static String toMethodSignature(String returnType, 274 String [] paramTypes) { 275 276 String signatureReturnType = toSignature(returnType); 277 String [] signatureParamTypes = toSignature(paramTypes); 278 279 StringBuffer signature = new StringBuffer ("("); 280 for (int i = 0; i < signatureParamTypes.length; i++) { 281 signature.append(signatureParamTypes[i]); 282 } 283 284 signature.append(")"); 285 signature.append(signatureReturnType); 286 287 return signature.toString(); 288 } 289 } 290 291 | Popular Tags |