Apt wrapper

某所でご馳走になってきました。

眠くなる前にアイデアをまとめておきます。

親クラスの誰かが "com.example.Required" の名前を持つ注釈を持つ注釈を持っている場合、そのクラスで宣言された全てのメソッドに対して処理を行う。その際、メソッド m と 注釈 a (annotated by "com.example.Required") を引数に取る。

// filtering rule
method m {
	declared in c
}

class c {
	extends s
}

class s {
	annotated a
}

annotation a {
	annotated b
}

annotation b {
	name "com.example.Required"
}

apply(m, a)

指定された注釈の値 "foo" が指すクラスを取得し、メソッドがそのクラスの型を持つ注釈を持たなければエラー。

// processor
public void process(MethodSymbol method, AnnotationMirror mirror) {
	Class<?> a = mirror.getValueOf("foo")
	if (! method.isAnnotationPresent(a)) {
		throw new Error();
	}
}

例えば、下記は m2 が @Foo を持たないのでエラー。

// example
class A extends B {
	@Foo
	void m1() {}
	
	void m2() {}
}

class B extends C {
}

@Hoge(foo=Foo.class)
class C {
}

@Required
@interface Hoge {
	Class<?> foo();
}

filtering rule の書き方をかなり練らないと俺様ウェアの恐れ…