samedi 13 juin 2015

How can I specify the type of the field that an Annotation should be applied to in Java?

I'm creating a simple annotation, to help me inflating settings inside my Android application. The annotation is this:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CoreSettings {
    String name() default "";
}

I want to ensure it will only be used in fields which type extends a custom class named BaseSettings. In the example below, MainSettings extends the abstract class BaseSettings.

public class MainActivity extends BaseActivity {

    @CoreSettings("prefs")
    MainSettings settings;


    (...)

}

How can I do it?





Aucun commentaire:

Enregistrer un commentaire