Actually there are a few issues with your code (I just reformatted it so it is at least readable):
- In Java class names are usually in camel case, i.e.
MyClass or SomeAspect instead of myclass or someAspect.
- Method names in Java begin with lower case characters, i.e.
request rather than Request.
- You are mentioning two annotations
@Intercept and @Interceptor. From now on I will assume that actually we are dealing with just one called @Intercept, okay?
- You also annotate your aspect's advice with
@Intercept, but I guess this is not intended, is it? It might lead to infinite recursion in another pointcut if the intercepting advice is intercepting itself...
As for your actual question, then answer is: sort of, but not statically within a pointcut (because parameters are only determined during runime) but dynamically, also during runtime. You have two options:
Option A: if-else within advice code
package de.scrum_master.app;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Intercept {}
package de.scrum_master.app;
public class Application {
public static void one(@Intercept String string) {}
public static void two(String string, String string2) {}
public static void three(@Intercept String string, int i, int j) {}
public static void main(String[] args) {
one("foo");
two("foo", "bar");
three("foo", 11, 22);
one("bingo");
two("bingo", "bongo");
three("bingo", 33, 44);
}
}
package de.scrum_master.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class InterceptorAspect {
@Pointcut("execution(public * *(@de.scrum_master.app.Intercept (*), ..)) && args(text, ..)")
public static void normalPointcut(String text) {}
@Around("normalPointcut(text)")
public Object capture(ProceedingJoinPoint thisJoinPoint, String text) {
if (text != "bingo")
return thisJoinPoint.proceed();
System.out.println(thisJoinPoint);
for (Object arg : thisJoinPoint.getArgs())
System.out.println(" " + arg);
// Do something before calling the original method
Object result = thisJoinPoint.proceed();
// Do something after calling the original method
return result;
}
}
Option B: use if() pointcut
The if() pointcut is a static method returning a boolean value based on the dynamic condition you want to test. The refactored aspect would look like this:
package de.scrum_master.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class InterceptorAspect {
@Pointcut("if() && execution(public * *(@de.scrum_master.app.Intercept (*), ..)) && args(text, ..)")
public static boolean conditionalPointcut(String text) {
return text != "bingo";
}
@Around("conditionalPointcut(text)")
public Object capture(ProceedingJoinPoint thisJoinPoint, String text) {
System.out.println(thisJoinPoint);
for (Object arg : thisJoinPoint.getArgs())
System.out.println(" " + arg);
// Do something before calling the original method
Object result = thisJoinPoint.proceed();
// Do something after calling the original method
return result;
}
}
As you can see, the dynamic, parameter-based condition has moved into the pointcut which is no longer a void method with an empty body but a boolean method returning the result of the parameter check. Furthermore, the pointcut expressen got prepended by an if() && expression.
Console log:
The console output for both aspect variants is exactly the same:
execution(void de.scrum_master.app.Application.one(String))
foo
execution(void de.scrum_master.app.Application.three(String, int, int))
foo
11
22