简体   繁体   中英

Android Reflection Fails for Annotation

having difficulty getting information about a fields annotation

here is my annotation:

public @interface DbField {}

here is my class:

public class AccountData extends Data {

    @DbField
    public String uidnext;

here is my reflection

Field f = AccountData.class.getField("uidnext");
Log.d("fcrow", String.format("%s:%b", f.getName(),
                f.getAnnotation(DbField.class) != null));

output is:

D/fcrow: uidnext:false

I'm confused most of what I can find online points to this code working.

Modify your annotation declaration to include a runtime retention policy:

@Retention(RetentionPolicy.RUNTIME)
public @interface DbField {}

If you do not do this, you will get the default retention policy CLASS . Its documentation states:

Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. This is the default behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM