guice のAOPで使う Matchers のアノテーション一致を自作する

@Named 付与の判定は、Google guice の Module 定義をする時に、
Names.named スタティックメソッドを良く使います。
guice AOP でインターセプターを定義するときも、クラスやメソッドの Matcher には、
Matchers という マッチ検証をする static メソッドの集まりのクラスがあります。
public static Matcher annotatedWith(final Annotation annotation)

@Named だけではなく自作アノテーションにマッチするものを専用に、
Names.named を参考に作ります。

サンプル:String を持つアノテーションです。
メソッドに付与する専用のアノテーションで、String を指定できます。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * Example
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Example {
   String value();
}

このアノテーションを implement したクラスを用意してそれとマッチングを行うのです。
Eclipse では、注釈型を implementしようとすると警告が出ますが無視します。
隠したいクラスので、パッケージ内だけが使用できるように class はスコープ宣言なしです。

import java.io.Serializable;
import java.lang.annotation.Annotation;
/**
 * ExampleImpl
 */
class ExampleImpl implements Example, Serializable{
   private final String value;

   public ExampleImpl(String value) {
      this.value = value;
   }
   @Override
   public Class<? extends Annotation> annotationType(){
      return Example.class;
   }
   @Override
   public String value(){
      return this.value;
   }
   @Override
   public int hashCode(){
      final int prime = 31;
      int result = 1;
      result = prime * result + ((value == null) ? 0 : value.hashCode());
      return result;
   }
   @Override
   public boolean equals(Object obj){
      if (!(obj instanceof Example)) {
         return false;
      }
      Example other = (Example) obj;
      return value.equals(other.value());
   }
   @Override
   public String toString(){
      return "@" + Example.class.getName() + "(" + value + ")";
   }
}

アノテーションを検査するためのアノテーション実装クラスインスタンスを生成するメソッドです。
(Names.named 同様です)

public final class Examples{
   private Examples(){}

   public static Example load(String str) {
      return new ExampleImpl(str);
   }
}

インターセプターバインド定義のメソッドの判定、Matchers.annotatedWith で
この staticメソッドでアノテーションを渡します。

binder().bindInterceptor(Matchers.any(),
   Matchers.annotatedWith(Examples.load("regist")),
   new MyIntercept() );

サンプル:int を持つアノテーションです。

int 型でも同様です。
要は equals メソッドです。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * Point
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Point {
   int value();
}
import java.io.Serializable;
import java.lang.annotation.Annotation;
/**
 * PointImpl
 */
class PointImpl implements Point, Serializable{
   private int value;

   public PointImpl(int value) {
      this.value = value;
   }
   @Override
   public Class<? extends Annotation> annotationType(){
      return Point.class;
   }
   @Override
   public int value(){
      return this.value;
   }
   @Override
   public int hashCode(){
      final int prime = 31;
      int result = 1;
      result = prime * result + value;
      return result;
   }
   @Override
   public boolean equals(Object obj){
      if (!(obj instanceof Point)) {
         return false;
      }
      Point other = (Point)obj;
      return value == other.value();
   }
}
/**
 * Points
 */
public final class Points{
   private Points(){}
   public static Point load(int v){
      return new PointImpl(v);
   }
}

バインド定義定義で使用例

binder().bindInterceptor(Matchers.any(),
   Matchers.annotatedWith(Points.load(20)),
   new MyIntercept() );