I am using Aspectj for project-wide policy enforcement.
One thing I am trying to implement now is that there should be no logic in any setter methods except simple validation with Guava's Preconditions.check*
methods.
public pointcut withinSetter() :
withincode(public void set*(*));
public pointcut inputValidation() :
call(public void Preconditions.check*(*));
public pointcut setFieldValue() : set(* *);
public pointcut entity() : within(com.mycompany.BaseEntity+);
declare warning :
entity() && withinSetter() && !setFieldValue() && !inputValidation():
"Please don't use Logic in Setters";
This works as expected, generating warnings for any non-setter code. However, it fails for constructs like this:
public void setFoo(final String newFoo) {
Preconditions.checkNotNull(newFoo); // this is OK
Preconditions.checkArgument(
newFoo.matches("\\p{Alpha}{3}"), // this generates a warning
// because String.matches()
// is called
"Foo must have exactly 3 characters!");
this.foo = newFoo;
}
So what I am looking for is a construct that would allow any code, as long as it happens inside the parameters to a Preconditions.check*
call. Is there such a pointcut?