プロジェクト全体のポリシー適用に Aspectj を使用しています。
私が今実装しようとしているのは、グアバのPreconditions.check*
メソッドを使用した単純な検証を除いて、どのセッター メソッドにもロジックがあってはならないということです。
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";
これは期待どおりに機能し、セッター以外のコードに対して警告を生成します。ただし、次のような構成では失敗します。
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;
}
Preconditions.check*
したがって、私が探しているのは、呼び出しのパラメーター内で発生する限り、任意のコードを許可する構造です。そのようなポイントカットはありますか?