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.

BindingTest.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*******************************************************************************
  2. * Copyright (c) Jonas Bonér, Alexandre Vasseur
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Common Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/cpl-v10.html
  7. *******************************************************************************/
  8. package ataspectj;
  9. import junit.framework.TestCase;
  10. import org.aspectj.lang.annotation.Aspect;
  11. import org.aspectj.lang.annotation.Pointcut;
  12. import org.aspectj.lang.annotation.Around;
  13. import org.aspectj.lang.JoinPoint;
  14. import org.aspectj.lang.ProceedingJoinPoint;
  15. import org.aspectj.lang.annotation.Around;
  16. import org.aspectj.lang.annotation.Aspect;
  17. import org.aspectj.lang.annotation.Pointcut;
  18. /**
  19. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  20. */
  21. public class BindingTest extends TestCase {
  22. public static void main(String[] args) {
  23. TestHelper.runAndThrowOnFailure(suite());
  24. }
  25. public static junit.framework.Test suite() {
  26. return new junit.framework.TestSuite(BindingTest.class);
  27. }
  28. public int substract(int i, int j) {
  29. int res = i - j;
  30. return res+1;//see +1 here
  31. }
  32. public static int dup(int i) {
  33. return i*3;//see x3 here
  34. }
  35. public void testAroundArgs() {
  36. int res = substract(3, 2);// should be 2 without around advice
  37. assertEquals(1, res);
  38. }
  39. public void testProceedArgs() {
  40. int res = dup((3+1));// advice will change arg with arg-1
  41. assertEquals(6, res);
  42. callWithinStatic();
  43. }
  44. private static void callWithinStatic() {
  45. int res = dup((3+1));
  46. assertEquals(6, res);
  47. }
  48. @Aspect
  49. public static class TestAspect_1 {
  50. @Pointcut("call(int substract(int, int)) && within(ataspectj.BindingTest) && args(arg1, arg2)")
  51. void pc(int arg2, int arg1) {}// see rather fancy ordering here..
  52. @Around("pc(argAdvice2, argAdvice1) && target(t)")//see here ordering remade consistent
  53. public Object aaround(ProceedingJoinPoint jp, BindingTest t, int argAdvice1, int argAdvice2) throws Throwable {
  54. int res = ((Integer)jp.proceed()).intValue();
  55. return new Integer(res-1);
  56. }
  57. @Pointcut("call(int dup(int)) && within(ataspectj.BindingTest) && args(arg1)")
  58. void pc2(int arg1) {}
  59. @Around("pc2(argAdvice1)")
  60. public Object aaround2(int argAdvice1, ProceedingJoinPoint jp) throws Throwable {
  61. int res = ((Integer)jp.proceed(new Object[]{new Integer(argAdvice1-1)})).intValue();
  62. return new Integer(res/3*2);
  63. }
  64. }
  65. }