は以下のclass
考えてみましょう:私のアプリケーションの反射位相の間、Methodインスタンスからクラス注釈を取得するにはどうすればよいですか?
@ClassAnnotation1
@ClassAnnotation2
class MyClass {
// ...
@MethodAnnotation1
@MethodAnnotation2
private void myMethod(@ParamAnnotation1 @ParamAnnotation2 int i) {
// ...
}
}
を、私はMethod
インスタンス与えられた、様々なコードの側面を分析する必要があります。
public void analyze(final Method method) {
// do the analysis...
// for example here, method is an instance of myMethod in MyClass
}
私は簡単に
for (Parameter p : method.getParameters()) {
if (p.getAnnotation(ParamAnnotation1.class) != null) {
// ...
}
}
を行うことにより、パラメータ「Annotation
を分析し、私が期待する結果を得ることができます。
方法のAnnotation
年代を簡単残念ながら、私はクラス「Annotation
年代の予想結果を得るために失敗し
method.getAnnotation(MethodAnnotation1.class)
で処理することができます。実際に
MyClass
が明確に@ClassAnnotation1
で注釈されているのに対し、
method.getClass().getAnnotation(ClassAnnotation1.class)`
への呼び出しは、null
を返します。
例Method
インスタンスからMyClass
注釈を取得するにはどうすればよいですか?