java - Can't get @override annotation from method -
when method class
instance, , want @override
annotation. method has not annotation. impossible @override
annotation?
code below.
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 method = childhoge.getclass().getmethod("init"); (annotation s : method.getannotations()) { system.out.println(s); } method method2 = childhoge.getclass().getmethod("a"); (annotation : method2.getannotations()) { system.out.println(a); // =>@javax.annotation.resource(mappedname=, shareable=true, type=class java.lang.object, authenticationtype=container, lookup=, description=, name=) } } } class superhoge { public void init() { } } class childhoge extends superhoge { @override public void init() { super.init(); } @resource public void a() { } }
@retention(retentionpolicy.source) public @interface override { }
it has retentionpolicy.source
discarded compiler, means cannot obtained @ runtime. can see described in jls 9.6.4.2.
if annotation corresponds type t, , t has (meta-)annotation m corresponds
java.lang.annotation.retention
, then:
- if m has element value
java.lang.annotation.retentionpolicy.source
, java compiler must ensure not present in binary representation of class or interface in appears.
and javadoc retentionpolicy
describes this:
public enum retentionpolicy { /** * annotations discarded compiler. */ source, ...
Comments
Post a Comment