| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.tracing; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.util.Arrays; |
| 37 | | | import java.util.Iterator; |
| 38 | | | import java.util.regex.Pattern; |
| 39 | | | |
| 40 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 41 | | | import org.objectweb.asm.Opcodes; |
| 42 | | | import org.objectweb.asm.Type; |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | <p> |
| 49 | | | |
| 50 | | | <ul> |
| 51 | | | <li> |
| 52 | | | </li> |
| 53 | | | <li><code></code> |
| 54 | | | </li> |
| 55 | | | <li><a |
| 56 | | | |
| 57 | | | </a></li> |
| 58 | | | <li><code></code> |
| 59 | | | <code></code> |
| 60 | | | <code></code></li> |
| 61 | | | </ul> |
| 62 | | (1) | |
| 63 | | (2) | |
| 64 | | | |
| 65 | | | @author |
| 66 | | | |
| 67 | | | public class AspectPattern |
| 68 | | | { |
| 69 | | | private final String mPattern; |
| 70 | | | |
| 71 | | | private final String mClassRegex; |
| 72 | | | |
| 73 | | | private final String mMethodRegex; |
| 74 | | | |
| 75 | | | private String mReturnPattern; |
| 76 | | | |
| 77 | | | |
| 78 | | | private String mTracerClass; |
| 79 | | | |
| 80 | | | private int mModifiers; |
| 81 | | | |
| 82 | | | private Pattern mClassMatcher; |
| 83 | | | |
| 84 | | | private Pattern mMethodMatcher; |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | |
| 89 | | | |
| 90 | | | |
| 91 | | | @param |
| 92 | | | |
| 93 | | | public AspectPattern (String in) |
| 94 | 100 | | { |
| 95 | 100 | | mPattern = in; |
| 96 | 100 | | int pos = 0; |
| 97 | 100 | | pos = getTracerClass(0); |
| 98 | 100 | | pos = getModifiers(pos); |
| 99 | 100 | | pos = getReturnType(pos); |
| 100 | 100 | | final int paraPos = mPattern.indexOf('(', pos); |
| 101 | 100 | | final int methodPos = mPattern.substring(pos, paraPos).lastIndexOf('.') |
| 102 | | | + pos; |
| 103 | | (3) | |
| 104 | 100 | | mClassRegex = convertToPattern(mPattern.substring(pos, methodPos)); |
| 105 | 100 | | final String methodName = convertToMethod(mPattern.substring( |
| 106 | | | methodPos + 1, paraPos)); |
| 107 | 100 | | final String arguments = convertToArgumets(mPattern.substring( |
| 108 | | | paraPos + 1, mPattern.length() - 1)); |
| 109 | 100 | (4) | mMethodRegex = methodName + "\\(" + arguments + "\\)" + mReturnPattern; |
| 110 | 100 | | } |
| 111 | | | |
| 112 | | | |
| 113 | | | |
| 114 | | | {@link } |
| 115 | | | @param |
| 116 | | | @return |
| 117 | | | |
| 118 | | | private int getTracerClass (int i) |
| 119 | | | { |
| 120 | 100 | | int pos = i; |
| 121 | 100 | | int end = i; |
| 122 | | | while (pos < mPattern.length() |
| 123 | 100 | | && Character.isWhitespace(mPattern.charAt(pos))) |
| 124 | | | { |
| 125 | 0 | | pos++; |
| 126 | | | } |
| 127 | 100 | | if (pos < mPattern.length() |
| 128 | | | && mPattern.charAt(pos) == '(') |
| 129 | | | { |
| 130 | 0 | | end = mPattern.indexOf(')', pos); |
| 131 | 0 | | if (end == -1) |
| 132 | | | { |
| 133 | 0 | (5) | throw new ArgumentMalformedException( |
| 134 | | | "pattern", mPattern, |
| 135 | | | "Opening '(' for tracing class name is not closed."); |
| 136 | | | } |
| 137 | 0 | | mTracerClass = mPattern.substring(pos + 1, end); |
| 138 | 0 | | end += 1; |
| 139 | | | while (end < mPattern.length() |
| 140 | 0 | | && Character.isWhitespace(mPattern.charAt(end))) |
| 141 | | | { |
| 142 | 0 | | end++; |
| 143 | | | } |
| 144 | | | } |
| 145 | 100 | | return end; |
| 146 | | | } |
| 147 | | | |
| 148 | | | |
| 149 | | | |
| 150 | | | |
| 151 | | | |
| 152 | | | @param |
| 153 | | | @param |
| 154 | | | @param |
| 155 | | | @return |
| 156 | | | |
| 157 | | | public boolean matches (int acc, String className, String arguments) |
| 158 | | | { |
| 159 | 100 | | return ((acc & mModifiers) == mModifiers |
| 160 | | | && className.matches(mClassRegex) |
| 161 | | | && arguments.matches(mMethodRegex)); |
| 162 | | | } |
| 163 | | | |
| 164 | | | |
| 165 | | | @param |
| 166 | | | @return |
| 167 | | | |
| 168 | | | private String convertToArgumets (String substring) |
| 169 | | | { |
| 170 | 100 | | final String[] args = substring.split(","); |
| 171 | 100 | | final StringBuffer pattern = new StringBuffer(); |
| 172 | 100 | | final Iterator i = Arrays.asList(args).iterator(); |
| 173 | 100 | | while (i.hasNext()) |
| 174 | | | { |
| 175 | 100 | | final String arg = (String) i.next(); |
| 176 | 100 | | if (!org.jcoderz.commons.util.StringUtil.isEmptyOrNull(arg)) |
| 177 | | | { |
| 178 | 100 | | final Type argType = tryBasicType(arg); |
| 179 | 100 | | if (argType == null) |
| 180 | | | { |
| 181 | 100 | | if ("*".equals(arg)) |
| 182 | | | { |
| 183 | 100 | | pattern.append("\\[*([VZCBSIFJD]|L[^;]+;)"); |
| 184 | | | } |
| 185 | 100 | | else if ("..".equals(arg)) |
| 186 | | | { |
| 187 | 100 | | pattern.append(".*"); |
| 188 | | | } |
| 189 | | | else |
| 190 | | | { |
| 191 | 100 | | pattern.append(convertToArgumentPattern(arg)); |
| 192 | | | } |
| 193 | | | } |
| 194 | | | else |
| 195 | | | { |
| 196 | 100 | | pattern.append(argType.getDescriptor()); |
| 197 | | | } |
| 198 | | | } |
| 199 | 100 | | } |
| 200 | 100 | | return pattern.toString(); |
| 201 | | | } |
| 202 | | | |
| 203 | | | |
| 204 | | | @param |
| 205 | | | @return |
| 206 | | | |
| 207 | | | private String convertToMethod (String method) |
| 208 | | | { |
| 209 | | | String result; |
| 210 | | | |
| 211 | 100 | | if ("new".equals(method)) |
| 212 | | | { |
| 213 | 0 | | if ((mModifiers & Opcodes.ACC_STATIC) != 0) |
| 214 | | | { |
| 215 | 0 | | result = "<init>"; |
| 216 | | | } |
| 217 | | | else |
| 218 | | | { |
| 219 | 0 | | result = "<clinit>"; |
| 220 | | | } |
| 221 | | | } |
| 222 | | | else |
| 223 | | | { |
| 224 | 100 | | result = method.replaceAll("\\*", ".*"); |
| 225 | | | } |
| 226 | 100 | | return result; |
| 227 | | | } |
| 228 | | | |
| 229 | | | |
| 230 | | | @param |
| 231 | | | @return |
| 232 | | | |
| 233 | | | private int getReturnType (int pos) |
| 234 | | | { |
| 235 | 100 | | final int result = (mPattern + " ").indexOf(' ', pos) + 1; |
| 236 | 100 | | final String typeString = mPattern.substring(pos, result - 1); |
| 237 | 100 | | final Type returnType = tryBasicType(typeString); |
| 238 | 100 | | if (returnType == null) |
| 239 | | | { |
| 240 | 100 | | mReturnPattern = convertToArgumentPattern(typeString); |
| 241 | | | } |
| 242 | | | else |
| 243 | | | { |
| 244 | 100 | | mReturnPattern = returnType.getDescriptor(); |
| 245 | | | } |
| 246 | 100 | | return result; |
| 247 | | | } |
| 248 | | | |
| 249 | | | |
| 250 | | | @param |
| 251 | | | @return |
| 252 | | | |
| 253 | | | private String convertToPattern (String typeString) |
| 254 | | | { |
| 255 | 100 | | String resultPattern = typeString; |
| 256 | 100 | | if (org.jcoderz.commons.util.StringUtil.isNullOrEmpty(typeString)) |
| 257 | | | { |
| 258 | 0 | | resultPattern = ""; |
| 259 | | | } |
| 260 | 100 | | else if ("*".equals(typeString)) |
| 261 | | | { |
| 262 | 100 | | resultPattern = "[^/]*"; |
| 263 | | | } |
| 264 | | | else |
| 265 | | | { |
| 266 | 100 | | resultPattern = resultPattern.replaceAll("\\.", "/"); |
| 267 | 100 | | resultPattern = resultPattern.replaceAll("\\*", "[^\\/]*"); |
| 268 | 100 | | resultPattern = resultPattern.replaceAll("//", ".*"); |
| 269 | | | |
| 270 | | | |
| 271 | 100 | | if (resultPattern.indexOf('/') < 0 |
| 272 | | | && resultPattern.indexOf('*') < 0) |
| 273 | | | { |
| 274 | 100 | | final StringBuffer pattern = new StringBuffer("(java/util/"); |
| 275 | 100 | | pattern.append(resultPattern); |
| 276 | 100 | (6) | pattern.append('|'); |
| 277 | 100 | | pattern.append("java/lang/"); |
| 278 | 100 | | pattern.append(resultPattern); |
| 279 | 100 | | pattern.append('|'); |
| 280 | 100 | | pattern.append(resultPattern); |
| 281 | 100 | | pattern.append(')'); |
| 282 | 100 | | resultPattern = pattern.toString(); |
| 283 | | | } |
| 284 | | | } |
| 285 | 100 | | return resultPattern; |
| 286 | | | } |
| 287 | | | |
| 288 | | | |
| 289 | | | @param |
| 290 | | | @return |
| 291 | | | |
| 292 | | | private String convertToArgumentPattern (String typeString) |
| 293 | | | { |
| 294 | 100 | | String resultPattern = typeString; |
| 295 | 100 | | if (org.jcoderz.commons.util.StringUtil.isNullOrEmpty(typeString)) |
| 296 | | | { |
| 297 | 0 | | resultPattern = ""; |
| 298 | | | } |
| 299 | 100 | | else if ("*".equals(typeString)) |
| 300 | | | { |
| 301 | 100 | | resultPattern = "\\[*([VZCBSIFJD]|L[^;]+;)"; |
| 302 | | | } |
| 303 | | | else |
| 304 | | | { |
| 305 | 100 | | String arrayPrefix = ""; |
| 306 | | | |
| 307 | 100 | | while (resultPattern.endsWith("[]")) |
| 308 | | | { |
| 309 | 100 | (7)(8) | arrayPrefix += "\\["; |
| 310 | 100 | | resultPattern |
| 311 | | | = resultPattern.substring(0, resultPattern.length() |
| 312 | | | - "[]".length()); |
| 313 | | | } |
| 314 | 100 | | resultPattern = resultPattern.replaceAll("\\.", "/"); |
| 315 | 100 | | resultPattern = resultPattern.replaceAll("\\*", "[^\\/]*"); |
| 316 | 100 | | resultPattern = resultPattern.replaceAll("//", ".*"); |
| 317 | | | |
| 318 | | | |
| 319 | 100 | | if (resultPattern.indexOf('/') >= 0 |
| 320 | | | || resultPattern.indexOf('*') >= 0) |
| 321 | | | { |
| 322 | 0 | | final StringBuffer pattern = new StringBuffer(arrayPrefix); |
| 323 | 0 | | pattern.append('L'); |
| 324 | 0 | | pattern.append(resultPattern); |
| 325 | 0 | | pattern.append(';'); |
| 326 | 0 | | resultPattern = pattern.toString(); |
| 327 | 0 | | } |
| 328 | | | else |
| 329 | | | { |
| 330 | 100 | | final StringBuffer pattern = new StringBuffer("("); |
| 331 | 100 | | pattern.append(arrayPrefix); |
| 332 | 100 | | pattern.append("Ljava/util/"); |
| 333 | 100 | | pattern.append(resultPattern); |
| 334 | 100 | (9) | pattern.append(';'); |
| 335 | 100 | | pattern.append('|'); |
| 336 | 100 | | pattern.append(arrayPrefix); |
| 337 | 100 | | pattern.append("Ljava/lang/"); |
| 338 | 100 | | pattern.append(resultPattern); |
| 339 | 100 | (10) | pattern.append(';'); |
| 340 | 100 | | pattern.append('|'); |
| 341 | 100 | | pattern.append(arrayPrefix); |
| 342 | 100 | | pattern.append('L'); |
| 343 | 100 | | pattern.append(resultPattern); |
| 344 | 100 | | pattern.append(";)"); |
| 345 | 100 | | resultPattern = pattern.toString(); |
| 346 | | | } |
| 347 | | | } |
| 348 | 100 | | return resultPattern; |
| 349 | | | } |
| 350 | | | |
| 351 | | | |
| 352 | | | @param |
| 353 | | | @return |
| 354 | | | |
| 355 | | | private Type tryBasicType (String typeString) |
| 356 | | | { |
| 357 | 100 | | final StringBuffer type = new StringBuffer(); |
| 358 | 100 | | String in = typeString; |
| 359 | 100 | | while (in.endsWith("[]")) |
| 360 | | | { |
| 361 | 100 | | type.append('['); |
| 362 | 100 | | in = in.substring(0, in.length() - "[]".length()); |
| 363 | | | } |
| 364 | 100 | | if ("int".equals(in)) |
| 365 | | | { |
| 366 | 100 | | type.append('I'); |
| 367 | | | } |
| 368 | 100 | | else if ("void".equals(in)) |
| 369 | | | { |
| 370 | 100 | | type.append('V'); |
| 371 | | | } |
| 372 | 100 | | else if ("boolean".equals(in)) |
| 373 | | | { |
| 374 | 0 | | type.append('Z'); |
| 375 | | | } |
| 376 | 100 | | else if ("char".equals(in)) |
| 377 | | | { |
| 378 | 0 | | type.append('C'); |
| 379 | | | } |
| 380 | 100 | | else if ("short".equals(in)) |
| 381 | | | { |
| 382 | 0 | | type.append('S'); |
| 383 | | | } |
| 384 | 100 | | else if ("float".equals(in)) |
| 385 | | | { |
| 386 | 0 | | type.append('F'); |
| 387 | | | } |
| 388 | 100 | | else if ("long".equals(in)) |
| 389 | | | { |
| 390 | 0 | | type.append('J'); |
| 391 | | | } |
| 392 | 100 | | else if ("double".equals(in)) |
| 393 | | | { |
| 394 | 0 | | type.append('D'); |
| 395 | | | } |
| 396 | | | else |
| 397 | | | { |
| 398 | 100 | | type.setLength(0); |
| 399 | | | } |
| 400 | 100 | | Type resultType = null; |
| 401 | 100 | | if (type.length() != 0) |
| 402 | | | { |
| 403 | 100 | | resultType = Type.getType(type.toString()); |
| 404 | 100 | | if (resultType.getSort() == Type.OBJECT) |
| 405 | | | { |
| 406 | 0 | | resultType = null; |
| 407 | | | } |
| 408 | | | } |
| 409 | 100 | | return resultType; |
| 410 | | | } |
| 411 | | | |
| 412 | | | |
| 413 | | | @param |
| 414 | | | @return |
| 415 | | | |
| 416 | | | private int getModifiers (int pos) |
| 417 | | | { |
| 418 | 100 | | int result = pos; |
| 419 | | | int oldResult; |
| 420 | | | do |
| 421 | | | { |
| 422 | 100 | | oldResult = result; |
| 423 | 100 | | result = checkModifier("public ", Opcodes.ACC_PUBLIC, result); |
| 424 | 100 | | result = checkModifier("private ", Opcodes.ACC_PRIVATE, result); |
| 425 | 100 | | result = checkModifier("protected ", Opcodes.ACC_PROTECTED, result); |
| 426 | 100 | | result = checkModifier("static ", Opcodes.ACC_STATIC, result); |
| 427 | 100 | | result = checkModifier("final ", Opcodes.ACC_FINAL, result); |
| 428 | 100 | | result |
| 429 | | | = checkModifier( |
| 430 | | | "synchronized ", Opcodes.ACC_SYNCHRONIZED, result); |
| 431 | 100 | | result |
| 432 | | | = checkModifier( |
| 433 | | | "deprecated ", Opcodes.ACC_DEPRECATED, result); |
| 434 | | | } |
| 435 | 100 | | while (result != oldResult); |
| 436 | 100 | | return result; |
| 437 | | | } |
| 438 | | | |
| 439 | | | |
| 440 | | | @param |
| 441 | | | @param |
| 442 | | | @param |
| 443 | | | @return |
| 444 | | | |
| 445 | | | private int checkModifier (String modifier, int acc, int pos) |
| 446 | | | { |
| 447 | 100 | | int result = pos; |
| 448 | 100 | | if (mPattern.startsWith(modifier, pos)) |
| 449 | | | { |
| 450 | 100 | | mModifiers |= acc; |
| 451 | 100 | | result += modifier.length(); |
| 452 | | | } |
| 453 | 100 | | return result; |
| 454 | | | } |
| 455 | | | |
| 456 | | (11) | public int getModifiers () |
| 457 | | | { |
| 458 | 100 | | return mModifiers; |
| 459 | | | } |
| 460 | | | |
| 461 | | (12) | public String getMethodPattern () |
| 462 | | | { |
| 463 | 100 | | return mMethodRegex; |
| 464 | | | } |
| 465 | | | |
| 466 | | (13) | public String getClassRegex () |
| 467 | | | { |
| 468 | 100 | | return mClassRegex; |
| 469 | | | } |
| 470 | | | |
| 471 | | | |
| 472 | | | |
| 473 | | | @param |
| 474 | | | @return |
| 475 | | | |
| 476 | | (14)(15)(16) | public boolean matchClass (String internalClassname) |
| 477 | | | { |
| 478 | 100 | | if (mClassMatcher == null) |
| 479 | | | { |
| 480 | 100 | | mClassMatcher = Pattern.compile(mClassRegex); |
| 481 | | | } |
| 482 | 100 | | return mClassMatcher.matcher(internalClassname).matches(); |
| 483 | | | } |
| 484 | | | |
| 485 | | | |
| 486 | | | @param |
| 487 | | | @return |
| 488 | | | |
| 489 | | (17)(18)(19) | public boolean matchMethod (String methodDesc) |
| 490 | | | { |
| 491 | 100 | | if (mMethodMatcher == null) |
| 492 | | | { |
| 493 | 100 | | mMethodMatcher = Pattern.compile(mMethodRegex); |
| 494 | | | } |
| 495 | 100 | | return mMethodMatcher.matcher(methodDesc).matches(); |
| 496 | | | } |
| 497 | | | |
| 498 | | | {@inheritDoc} |
| 499 | | | public String toString () |
| 500 | | | { |
| 501 | 100 | | return mPattern + " = '" + AsmUtil.toString(mModifiers) + " " |
| 502 | | | + mClassRegex + "#" + mMethodRegex + "'"; |
| 503 | | | } |
| 504 | | | |
| 505 | | | |
| 506 | | | @param |
| 507 | | | @return |
| 508 | | | |
| 509 | | (20)(21)(22) | public boolean matchAccess (int access) |
| 510 | | | { |
| 511 | 100 | | return (access & mModifiers) == mModifiers; |
| 512 | | | } |
| 513 | | | |
| 514 | | | String getMethodRegex () |
| 515 | | | { |
| 516 | 100 | | return mMethodRegex; |
| 517 | | | } |
| 518 | | | |
| 519 | | (23) | public String getTracerClass () |
| 520 | | | { |
| 521 | 0 | | return mTracerClass; |
| 522 | | | } |
| 523 | | | } |