This is the third day of my participation in Gwen Challenge

What are annotations

Sync code address github.com/xyz0z0/Andr…

What are annotations

It can be said that it is an auxiliary annotation, which can combine various functional effects with APT, bytecode modification and reflection technology.

First, take a look at annotations, such as Override, which are system-defined annotations that apply to methods that subclasses inherit from their parents.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
Copy the code

You can see that the first and second lines define two annotations. These are meta-annotations, which are the annotations that are placed on top of the annotations. The first annotation is where @target indicates the annotation modifier, such as elementType. METHOD, which indicates that the annotation can be placed on a METHOD. The second annotation @retention indicates which lifetime the annotation is retained for. The three values are SOURCE (retained in SOURCE), CLASS (retained in bytecode), and RUNTIME (retained at RUNTIME). If we set Retention in SOURCE, The annotation is not available through reflection at run time, so the determination of this field needs to be created according to the actual usage scenario. Public @interface Override {} public @interface Override {}

What are annotations for

Next, take a look at the @BindView annotation in ButterKnife, a third-party open source framework.

@Retention(RUNTIME) @Target(FIELD)
public @interface BindView {
  /** View ID to which the field will be bound. */
  @IdRes int value(a);
}
Copy the code

You can see that this annotation is retained at runtime and can be placed on member properties. It also defines a method that returns an int, so that we can append values to annotations when we use them, such as @bindView (R.i.D.first).

Define a custom annotation

This is the end of annotations, so let’s use Kotlin to create a custom annotation. By the way, see what the difference is in Kotlin.

@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class KotlinFirst(
    val name: String
)
Copy the code

Here, we define a Kotlin annotation, which you can see is very similar to Java’s. Our annotation can be placed on member properties and retained until runtime. So let’s try using this annotation with reflection.

public class KotlinAnnotationTestActivity extends AppCompatActivity {

    @KotlinFirst(name = "HAHA")
    public String mName;

    @BindView(R.id.tvFirst)
    TextView tvFirst;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter02);
        ButterKnife.bind(this); Class<? > clazz =this.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(KotlinFirst.class)) {
                KotlinFirst annotation = field.getAnnotation(KotlinFirst.class);
                Log.d("sss", annotation.name());// HAHA}}}}Copy the code

Here we get the value on the annotation by reflection, and then we can do something else with it.

Initially we talked about annotations doing things in conjunction with other technical points, and just now we showed that the coordination reflection can read the value of an annotation at runtime. You can also do things like compiler checking and bytecode modification. And that’s what we need to learn together.

This article is a personal learning record, can not avoid mistakes or not enough in-depth understanding of technical points, but also hope for criticism and advice.