以下のようなアノテーションが存在したときに、アノテーションを
スーパー・インタフェースにするには、
Eclipse [設定]→[Java]→[コンパイラ]→[エラー/警告]
▼注釈(N)
注釈をスーパー・インタフェースとして使用 → 無視
を設定
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.google.inject.BindingAnnotation;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.METHOD})
@BindingAnnotation
public @interface Fruit{
int value();
}
----------------------
自動生成は、、、
import java.lang.annotation.Annotation;
public class FruitImpl implements Fruit{
@Override
public int value(){
// TODO 自動生成されたメソッド・スタブ
return 0;
}
@Override
public Class<? extends Annotation> annotationType(){
// TODO 自動生成されたメソッド・スタブ
return null;
}
}
ここまで。
このアノテーションをスーパー・インタフェースにしたクラスの使い道の例は、
以下を追記して、完成させて Google guice の @Named の int 型版とすることである。
public class FruitImpl implements Fruit{
private Integer value;
public FruitImpl(int v){
this.value = v;
}
@Override
public int value(){
return this.value;
}
@Override
public Class<? extends Annotation> annotationType(){
return Fruit.class;
}
@Override
public boolean equals(Object obj){
if (!(obj instanceof Fruit)){
return false;
}
Fruit other = (Fruit)obj;
return this.value.equals(other.value());
}
@Override
public int hashCode() {
return (127 * "value".hashCode()) ^ this.value.hashCode();
}
@Override
public String toString() {
return "@" + Fruit.class.getName() + "(value=" + this.value + ")";
}
}
// FruitImpl の用意、→実践は、FruitImplをインナークラス化した方がよい?
public class Fruits{
public static Annotation set(int v){
return new FruitImpl(v);
}
}
// バインドの定義に、ループを記述できる。
Injector injector = Guice.createInjector(new AbstractModule(){
@Override
protected void configure(){
for(int i=10;i < 16;i++){
binder().bind(String.class).annotatedWith(Fruits.set(i)).toInstance("Field Inject Test! "+i);
}
:
----------------------------
もしも、Eclipse自動生成は、、
public @interface Fruit{
String a();
int b();
}
であれば、
@Override
public String a(){
// TODO 自動生成されたメソッド・スタブ
return null;
}
@Override
public int b(){
// TODO 自動生成されたメソッド・スタブ
return 0;
}
が生成される。