You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SynchronizedAspect.aj 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package foo;
  2. import org.aspectj.lang.annotation.SuppressAjWarnings;
  3. public aspect SynchronizedAspect perthis(@this(Synchronized)) {
  4. // private static final Logger log = LogManager.getLogger(SynchronizedAspect.class);
  5. // private Lock objectLock = new ReentrantLock();
  6. // private static long timeout = 1800;
  7. // private static TimeUnit unit = TimeUnit.SECONDS;
  8. // private static boolean timeoutInitialized = false;
  9. // @SuppressWarnings({"rawtypes"})
  10. // private final static synchronized void initTimeout(Class cl) {
  11. // timeout = SynchronizedStaticAspect.aspectOf(cl).timeout;
  12. // unit = SynchronizedStaticAspect.aspectOf(cl).unit;
  13. // timeoutInitialized = true;
  14. // log.trace("timeout inialized with " + timeout + " " + unit);
  15. // }
  16. pointcut synchronizedMethods() :
  17. execution(@Synchronized !synchronized !static * *..*.*(..))
  18. ;
  19. pointcut synchronizedStaticMethods() :
  20. execution(@Synchronized !synchronized static * *..*.*(..))
  21. ;
  22. pointcut ignoredSynchronized() :
  23. execution(@Synchronized * *..*.*(..))
  24. && !synchronizedMethods()
  25. && !synchronizedStaticMethods()
  26. ;
  27. declare warning : ignoredSynchronized() :
  28. "@Synchronized is ignored here";
  29. declare warning :
  30. (synchronizedStaticMethods() || synchronizedMethods())
  31. && !@within(Synchronized) :
  32. "@Synchronized is ignored here because @Synchronized for class is missing";
  33. /**
  34. * Uses the Lock class of Java 5 to put a synchronization wrapper around
  35. * a method. Advantage of this Lock class is the posibility to use a
  36. * timeout to avoid dead locks.
  37. *
  38. * @return the return value of the wrapped method
  39. */
  40. @SuppressAjWarnings({"adviceDidNotMatch"})
  41. Object around() : synchronizedMethods() && @within(Synchronized) {
  42. return proceed();
  43. /*
  44. if (!timeoutInitialized) {
  45. initTimeout(thisJoinPointStaticPart.getSignature().getDeclaringType());
  46. }
  47. if (log.isTraceEnabled()) {
  48. log.trace("synchronizing " + thisJoinPoint.getSignature().toShortString() + "...");
  49. }
  50. try {
  51. if (objectLock.tryLock(timeout, unit)) {
  52. if (log.isTraceEnabled()) {
  53. log.trace("lock granted for "
  54. + thisJoinPoint.getSignature().toShortString());
  55. }
  56. try {
  57. return proceed();
  58. } finally {
  59. objectLock.unlock();
  60. if (log.isTraceEnabled()) {
  61. log.trace("lock released for "
  62. + thisJoinPoint.getSignature().toShortString());
  63. }
  64. }
  65. } else {
  66. String msg = "can't get " + objectLock + " for "
  67. + thisJoinPoint.getSignature().toShortString()
  68. + " after " + timeout + " " + unit;
  69. log.error(msg);
  70. throw new RuntimeException(msg);
  71. }
  72. } catch (InterruptedException ie) {
  73. String msg = "interrupted: "
  74. + thisJoinPoint.getSignature().toShortString();
  75. log.warn(msg, ie);
  76. throw new RuntimeException(msg, ie);
  77. }
  78. */
  79. }
  80. }