Thoughts on android annotations and implementation methods
When developing web projects, we often use SSH to build our projects. Many programmers prefer to use annotations to reduce the amount of code. But have you ever thought about how this annotation is implemented. How can we write code that is as elegant as annotation. In addition to reading new things while writing code, we always keep exploring. We also need to develop a spirit of great enthusiasm, dedicated research, and meticulous treatment of code. In this way, you can make progress and write more elegant code. Today, I will show you how to write an annotation class to initialize the android control.
To put it bluntly, let's get a piece of code first.
Package com. example. volley. annotation; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target; // declare in field @ Target (ElementType. FIELD) @ Retention (RetentionPolicy. RUNTIME) public @ interface ViewInject {int value () default 0 ;}
First, let's look at this code. I believe that kids shoes that you have never previously written will surely be confused. So I want to explain something. @ Interface this is an annotation class defined by java.
@ Target and @ Retention are the meta annotations provided by JDK. See the following.
@ Target (ElementType. TYPE) // interface, class, enumeration, Annotation
@ Target (ElementType. FIELD) // constant of the FIELD and enumeration
@ Target (ElementType. METHOD) // METHOD
@ Target (ElementType. PARAMETER) // method PARAMETER
@ Target (ElementType. CONSTRUCTOR) // CONSTRUCTOR
@ Target (ElementType. LOCAL_VARIABLE) // local variable
@ Target (ElementType. ANNOTATION_TYPE) // Annotation
@ Target (ElementType. PACKAGE) // PACKAGE
@ Retention (RetentionPolicy. RUNTIME) // The annotation will exist in the class bytecode file and can be obtained through reflection during RUNTIME.
For example, ElementType. TYPE indicates a class annotation. For example, @ controller of struts2 and springMVC
This annotation.
RetentionPolicy. RUNTIME indicates that the program runs in the class file through reflection.
After the annotation class has been written, the key below is how to parse the annotation.
The old method is to add the code first.
package com.example.volley.annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;import android.app.Activity;public class ViewUtils { public static ViewUtils viewUtils; public Activity ctx; public ViewUtils(Activity ctx) { this.ctx = ctx; autoInjectAllField(); } public static ViewUtils inject(Activity ctx){ if (viewUtils!=null) { return viewUtils; }else{ viewUtils = new ViewUtils(ctx); return viewUtils; } } private void autoInjectAllField() { try { Class
clazz = ctx.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(ViewInject.class)) { ViewInject inject = field.getAnnotation(ViewInject.class); int id = inject.value(); if(id > 0){ field.setAccessible(true); field.set(ctx, ctx.findViewById(id)); } } } } catch (Exception e) { e.printStackTrace(); } }}
Use ctx. getclass () to obtain the object class. Then all the annotations in this class match the ViewInject annotation class. If the match is successful, set (ctx, ctx. findById ());
Everyone must understand this article. Today, let's see.