1

I am using AspectJ with annotation and trying to find how to disable all AspectJ's advices to stop advicing method from user's input (e.g. Boolean tracked = false).

Here is my code for main class.

package testMaven;


public class MainApp {

    public static void main(String[] args) {
        testing test = new testing();
        test.aa(1000);
        test.setDd(3);
    }

}

Here is the Aspect annotated class.

package testMaven;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;

@Aspect
public class aspecter {

    public aspecter(){

    }

    boolean tracked = false;

    @Before("execution(*  testMaven.testing.aa(..)) && if(tracked)")
    public void testBefore(){
        System.out.println("yooi");
    }

    @Before("execution(*  testMaven.testing.setDd(..)) && if(tracked) ")
    public void testBefore2(){
        System.out.println("yooi2");
    }
}

if(tracked) will give an error of "Syntax error on token "execution(* testMaven.testing.aa(..)) && if(tracked) ", "in annotation style, if(...) pointcuts cannot contain code. Use if() and put the code in the annotated method" expected".

Is there anyway that I could specify the if() method based on my specification?

Thanks

Ihsan Haikal
  • 1,085
  • 4
  • 16
  • 42
  • What version of AspectJ and Java are you using? Are you using compile time weaving or load time weaving? Did you read the docs at https://eclipse.org/aspectj/doc/released/adk15notebook/ataspectj-pcadvice.html, especially the section "if() pointcut expressions"? – Wim Deblauwe Jun 16 '16 at 15:43
  • @WimDeblauwe I am using 1.8.9 of Maven and Java8. Yes I am following [this](https://www.eclipse.org/aspectj/doc/next/adk15notebook/ataspectj-pcadvice.html#d0e3697) and also other questions in Stackoverflow but those answers really complicated. I am following the eclipse's tutorial for my question – Ihsan Haikal Jun 16 '16 at 15:52

1 Answers1

5

If using annotation style you have to do things a little differently, as the documentation describes ( https://eclipse.org/aspectj/doc/released/adk15notebook/ataspectj-pcadvice.html ). In your case your aspect needs to be something like this:

static boolean tracked = false;

@Pointcut("if()")
public static boolean tracked() {
  return tracked;
}

@Before("execution(*  testMaven.testing.aa(..)) && tracked()")
public void testBefore(){
    System.out.println("yooi");
}

@Before("execution(*  testMaven.testing.setDd(..)) && tracked() ")
public void testBefore2(){
    System.out.println("yooi2");
}

Notice the code that would normally go into the if(...) clause in a code style aspect is now in a method body, which is tagged with @Pointcut using if(). I did have to make the field static. You could probably modify the code in the tracked() method to use Aspects.aspectOf() to keep it non static.

Andy Clement
  • 2,510
  • 16
  • 11
  • Thank you it works! I am still confused how to implement Aspects.aspectOf() but i think it is ok to use static. – Ihsan Haikal Jun 17 '16 at 09:11
  • See https://eclipse.org/aspectj/doc/next/adk15notebook/ataspectj-aspectof.html - for a singleton aspect you probably need something like: Aspects.aspectOf(aspecter.class).tracked in your pointcut method ('tracked()' in my sample code) - that will access the singleton instance of the aspect and then the non-static tracked field upon that instance. – Andy Clement Jun 17 '16 at 21:05