Annotation Type Preference


  • @Target({})
    @Retention(CLASS)
    public @interface Preference
    Defines a preference. The preference key is the value of the string resource
    R.string.${preference_group_prefix}${name}${preference_group_suffix}
    with ${preference_group_prefix} and ${preference_group_suffix} being the prefix and suffix defined in the enclosing PreferenceGroup-Annotation.
    For each preference with a type() other than void.class a getter
    public ${type} ${name}()
    and a setter
    public void ${name}(${type} value)
    will be generated in the preference group. Additionally, a key accessor
    public String ${name}()
    is generated in the Keys class of the preference group for each preference (including void).
    See Also:
    Preferences, PreferenceGroup
    • Field Summary

      Fields 
      Modifier and Type Fields Description
      static java.lang.String NO_DEFAULT_VALUE  
    • Required Element Summary

      Required Elements 
      Modifier and Type Required Element Description
      java.lang.String name
      The name of the preference.
      java.lang.Class<?> type
      The type of the preference.
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      java.lang.String defaultValue
      The default value for the preference.
      java.lang.String description
      A description that will be used as documentation for the preference accessors in the generated class.
      java.lang.Class<? extends PreferenceSerializer> serializer
      A serializer used for converting the preference type to a type supported by SharedPreferences and back.
    • Field Detail

      • NO_DEFAULT_VALUE

        static final java.lang.String NO_DEFAULT_VALUE
      • type

        java.lang.Class<?> type
        The type of the preference.

        If no serializer is provided this must be one of byte.class, char.class, short.class, int.class, long.class, float.class, double.class, boolean.class, String.class, void.class, Set.class or any subtype of Enum.class.

        If a serializer is provided the type declared here will serve as type-argument and/or constructor argument for the serializer. The actual preference type will be the serializers source type and may differ.

        Since SharedPreferences only support int, long, float, boolean, String and Set<String> the other natively supported types must be converted into one of those types:

        • byte, char and short are stored as an int
        • double is stored as a long via Double.doubleToRawLongBits(double) and Double.longBitsToDouble(long)
        • enum is stored as a String via Enum.name() and Enum.valueOf(Class, String)
        • Set is interpreted as Set<String>
        • void is not stored

        See Also:
        serializer()
      • defaultValue

        java.lang.String defaultValue
        The default value for the preference. If no default value is provided the default value will be
        • false for boolean preferences,
        • 0 for byte, short, char, int, long, float and double preferences and
        • null for String, enum and Set<String> preferences
        This field does not have an effect for void preferences. If the type() is String, then the default value is automatically escaped and quoted, otherwise it will be copied into the generated class source code as is. If a serializer() is used, the default value must be provided in serialized form, i.e. it must be a valid argument to the serializers PreferenceSerializer.deserialize(Object) method.
        Implementation Note:
        it is possibly to inject code into the generated classes by misusing this field. Just don't.
        Default:
        "__NO_DEFAULT_VALUE__"
      • description

        java.lang.String description
        A description that will be used as documentation for the preference accessors in the generated class.
        Default:
        ""
      • serializer

        java.lang.Class<? extends PreferenceSerializer> serializer

        A serializer used for converting the preference type to a type supported by SharedPreferences and back.

        A serializer must have at most one type argument T - the declared preference type - and must either have a default constructor or a constructor taking exactly one argument of type Class<? extends T>. If both constructors are present it is not defined which one will be used. Furthermore the serializers target type must be a primitive wrapper or String. If the target type is a primitive wrapper the argument of PreferenceSerializer.deserialize(Object) is guaranteed to be non-null and the return value of PreferenceSerializer.serialize(Object) must be non-null.

        The actual preference type will be the serializers source type. Therefore a preference

        {@code
        Default:
        eu.jonahbauer.android.preference.annotations.serializer.PreferenceSerializer.class