@Override アノテーションは取得できない?
classからメソッドを取り出して、そのメソッドに@overrideなアノテーションがついてるか調べたかった。
が、取得できない。消える? それとも親のメソッドが取得されてるからoverrideがついてないメソッドが取得されてるってことなんだろうか。
package com.test; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import javax.annotation.Resource; public class ReflectionTest { public static void main(String[] args) throws Exception { ChildHoge childHoge = new ChildHoge(); Method declaredMethod = childHoge.getClass().getMethod("init"); for (Annotation s : declaredMethod.getAnnotations()) { System.out.println(s); } Method method = childHoge.getClass().getMethod("a"); for (Annotation a : method.getAnnotations()) { System.out.println(a); } } } class SuperHoge { public void init() { } } class ChildHoge extends SuperHoge { @Override public void init() { super.init(); } @Resource public void a() { } }
実行結果
@javax.annotation.Resource(mappedName=, shareable=true, type=class java.lang.Object, authenticationType=CONTAINER, lookup=, description=, name=)
※追記 @overrideのソースを見て分かった。
@Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) public @interface Override { }
@Retention(RetentionPolicy.SOURCE)
なのでコンパイル時には既に消えてる....