Auro
Auro

Reputation: 1638

Android annotation not understood properly

I have been given a project that I have to study & understand. There's a class that has the following: -

public abstract class AbstractPayment extends GodelCheckout {
    public static final String AUTH_USING_GET = "GET";
    public static final String AUTH_USING_POST = "POST";

    @Retention(RetentionPolicy.SOURCE)
    @StringDef({AUTH_USING_GET, AUTH_USING_POST})
    public @interface AuthMethod {}

I tried understanding annotation & why they are important from Google's Developer site, but I couldn't understand much apart from the fact that they are used for improving code.

  1. But how are they improving code? Any example?

  2. Why is @Retention(RetentionPolicy.SOURCE) required here?

  3. What does @StringDef() actually do?

  4. what is public @interface AuthMthod{}?

  5. Please explain what Retention.SOURCE, .CLASS & .RUNTIME really are? I tried understanding them from Google's Developer's site, but it didn't help much?

Upvotes: 1

Views: 56

Answers (1)

R. Zagórski
R. Zagórski

Reputation: 20278

  1. By marking a function argument or result of it, you can define an expected behaviour. For example it marking a function argument with that annotation, no other argument than AUTH_USING_GET or AUTH_USING_POST can be passed.
  2. With source retention, the code won't compile, when there are annotation errors. After successful compilation, they are removed from code. With retention policy RUNTIME the code is inspected during running.
  3. @StringDef allows you to pass only String id's, for example R.string.login. @IntDef allows you to pass only R.id.sample_int to the function
  4. This is the annotation, that desired parameters or function results or class fields must be annotated to obtain desired result.
  5. Look at second question.

For more questions refer to Java documentation.

Upvotes: 2

Related Questions