Java 注解

原创
2016/09/05 14:33
阅读数 22

1、运行时注解:

http://www.yiibai.com/java/lang/package_getannotation.html

ViewInject .java:

package com.flx.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 
 * @Description: 自定义view注解类
 * @date 2016-9-5 上午11:52:04
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewInject {
	int value();
}

InjectUtil.java: 

package com.flx.annotation;

import java.lang.reflect.Field;

import android.app.Activity;
import android.view.View;

/**
 * 
 * @Description:
 * @date 2016-9-5 上午11:59:06
 */
public class InjectUtil {

	public static void inject(View view, boolean isSuperClass) {
		injectObj(view, isSuperClass);
	}

	public static void inject(Activity activity, boolean isSuperClass) {
		injectObj(activity, isSuperClass);
	}

	/**
	 * 
	 * @Description: 获取object上的所有注解字段,并获取注解上的值,设置字段的值为查找到的view
	 * @param object
	 */
	private static void injectObj(Object object, boolean isSuperClass) {
		Class<?> clazz = object.getClass();

		Field[] fields = null;
		if (isSuperClass) {
			Class<?> superclass = clazz.getSuperclass();
			fields = superclass.getDeclaredFields();
		} else {
			fields = clazz.getDeclaredFields();
		}

		/*Class<?> superclass = clazz.getSuperclass();
		Field[] fieldsParent = superclass.getDeclaredFields();
		Field[] fieldsChild = clazz.getDeclaredFields();
		Field[] fields = new Field[fieldsParent.length + fieldsChild.length];
		System.arraycopy(fieldsParent, 0, fields, 0, fieldsParent.length);
		System.arraycopy(fieldsChild, 0, fields, fieldsParent.length, fieldsChild.length);*/

		if (fields != null && fields.length > 0) {
			for (Field field : fields) {
				// get the annotation for class ViewInject
				if (field.isAnnotationPresent(ViewInject.class)) {
					ViewInject viewInject = field.getAnnotation(ViewInject.class);
					if (viewInject != null) {
						View view = findViewById(object, viewInject.value());
						try {
							if (view != null) {
								field.setAccessible(true);
								field.set(object, view);
							}
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
	}

	/**
	 * 
	 * @Description: 根据id查找view
	 * @param object
	 *            view or activity
	 * @param id
	 *            the id attribute from the XML
	 * @return
	 */
	private static View findViewById(Object object, int id) {
		View view = null;
		if (object instanceof View) {
			view = ((View) object).findViewById(id);
		} else if (object instanceof Activity) {
			view = ((Activity) object).findViewById(id);
		}
		return view;
	}
}

使用:

   @ViewInject(R.id.title_tv)
    private TextView title_tv;

       // view注解
        InjectUtil.inject(this, false);

2、编译时注解:

https://lizhaoxuan.github.io/2016/07/17/apt-Grammar-explanation/           好!

Android 如何编写基于编译时注解的项目:

http://www.open-open.com/lib/view/open1469021419529.html   最好!

 

 

 

 

 

 

展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部