AbstractMatcher の実装

Google guice bindInterceptor に渡す2番目の引数、メソッドに対する条件の記述、
アノテーションが付いているかの判定が多いので、
よく、Matchers.annotatedWith(annotation.class) を使うことが多い。

Matchers にはメソッド戻り値タイプなどを判定するものはあるが、
メソッド名を判定させるものはない。
メソッド名に制約をあてるコーディング規定にするという考え方ではないからだ。
でもどうしても、メソッド名を bindInterceptor インタセプトの条件にするなら、
以下のように書くことになる。
この書き方は、他の判定条件を追加する時の参考になる。


Injector injector = Guice.createInjector(new AbstractModule(){
   @Override
   protected void configure(){
      try{
      binder().bindInterceptor(Matchers.any()
         ,new AbstractMatcher<Method>(){
             @Override
             public boolean matches(Method m){
                return m.getName()
.equals("exec") && m.getParameterTypes().length==0;
             }
          }

         ,new FooIntercepter()
      );
      }catch(Exception e){
         throw new RuntimeException(e);
      }
   }
});
このようにメソッド名がマッチするかなんて、
絶対に書かないつもりである。