android - How can I specify the type of the field that an Annotation should be applied to in Java? -
i'm creating simple annotation, me inflating settings inside android application. annotation this:
@target(elementtype.field) @retention(retentionpolicy.runtime) public @interface coresettings { string name() default ""; } i want ensure used in fields type extends custom class named basesettings. in example below, mainsettings extends abstract class basesettings.
public class mainactivity extends baseactivity { @coresettings("prefs") mainsettings settings; (...) } how can it?
yes, can cause compiler issue error message if write annotation on field of wrong type.
you cannot using @target meta-annotation. in addition, need write annotation processor. annotation processor examine each occurrence of annotation @coresettings in source code, , issues error (just other compiler error) if @coresettings applied type not extend basesettings. short, simple annotation processor write.
you compile-time warning when running annotation processor, can add -processor ... command-line argument javac in project's buildfile.
Comments
Post a Comment