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.

PointcutReferenceTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * initial implementation Alexandre Vasseur
  11. *******************************************************************************/
  12. package ataspectj;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.aspectj.lang.annotation.Before;
  15. import org.aspectj.lang.annotation.Pointcut;
  16. import org.aspectj.lang.annotation.Aspect;
  17. import org.aspectj.lang.annotation.Before;
  18. import org.aspectj.lang.annotation.Before;
  19. import org.aspectj.lang.annotation.Pointcut;
  20. import org.aspectj.lang.annotation.SuppressAjWarnings;
  21. import org.aspectj.lang.JoinPoint;
  22. import org.aspectj.runtime.internal.CFlowCounter;
  23. import junit.framework.TestCase;
  24. /**
  25. * Test pointcut reference without binding
  26. *
  27. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  28. */
  29. public class PointcutReferenceTest extends TestCase {
  30. static StringBuffer s_log = new StringBuffer();
  31. static void log(String s) {
  32. s_log.append(s).append(" ");
  33. }
  34. public static void main(String[] args) {
  35. TestHelper.runAndThrowOnFailure(suite());
  36. }
  37. public static junit.framework.Test suite() {
  38. return new junit.framework.TestSuite(PointcutReferenceTest.class);
  39. }
  40. public void hello() {
  41. log("hello");
  42. }
  43. public void helloWithRef() {
  44. log("helloWithRef");
  45. }
  46. public void testPointcutReferenceNoBinding() {
  47. PointcutReferenceTest me = new PointcutReferenceTest();
  48. s_log = new StringBuffer();
  49. me.hello();
  50. assertEquals("before hello ", s_log.toString());
  51. }
  52. public void testPointcutReferenceBinding() {
  53. PointcutReferenceTest me = new PointcutReferenceTest();
  54. s_log = new StringBuffer();
  55. me.helloWithRef();
  56. assertEquals("beforeWithRef helloWithRef ", s_log.toString());
  57. }
  58. @Aspect
  59. public static class TestAspect {
  60. // order of pointcut in source code does not matter for pc refs
  61. @Pointcut("execution(* ataspectj.PointcutReferenceTest.hello(..))")
  62. void pcRef() {}
  63. @Pointcut("pcRef()")
  64. void pcRef2() {}
  65. @SuppressAjWarnings
  66. @Before("pcRef2()")
  67. public void before(JoinPoint jp) {
  68. log("before");
  69. }
  70. // see here outer aspect reference
  71. @Pointcut("execution(* ataspectj.PointcutReferenceTest.helloWithRef(..))" +
  72. " && ataspectj.PointcutReferenceTest.RefAspect.pcRefObjectBinding(t)")
  73. void pcRefBinding(Object t) {}
  74. @SuppressAjWarnings
  75. @Before("pcRefBinding(ttt)")
  76. public void before(Object ttt, JoinPoint jp) {
  77. log("beforeWithRef");
  78. }
  79. }
  80. @Aspect
  81. public static class RefAspect {
  82. @Pointcut("this(obj)")
  83. public void pcRefObjectBinding(PointcutReferenceTest obj) {}
  84. }
  85. }