I would like to have something like this:
public void doSomething(@ReplaceFooBar String myString) {
//...
}
ReplaceFooBar
is my custom annotation which should take the value of myString
and do a replaceAll
of "foo" with "bar" on it before the method starts executing so that it executes with the new string value. So, if the method was invoked with the parameter "I'm at the foo." it should actually execute with "I'm at the bar."
I don't know how to make this work. I've been fiddling with this for some time now. Let's say I last ended up at this point:
@Documented
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ReplaceFooBar {
}
And...
@Aspect
public aspect ReplaceFooBarAspect {
@Before("@annotation(ReplaceFooBar)")
public String replaceFooBar(String value) {
if (value == null) {
return null;
}
return value.replaceAll("foo", "bar");
}
}
What am I doing wrong?
My code isn't compiling, I'm getting errors like these.
Error:(6, 0) ajc: Duplicate annotation @Aspect
Error:(7, 0) ajc: aspects cannot have @Aspect annotation
Error:(10, 0) ajc: This advice must return void
Error:(10, 0) ajc: formal unbound in pointcut
I don't know how these aspects work exactly, how to get this working the way I want it.