[Java] Annotation(内注)实例一则

为下篇[Android] 数据实体的自动存取--SQLite篇”准备
* 参考地址:http://www.blogjava.net/ericwang/archive/2005/12/13/23743.html
* http://www.blogjava.net/weidagang2046/articles/27958.html
* http://www.blogjava.net/mlh123caoer/archive/2007/09/06/143260.html
* 官方文档:
* http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html

以上链接很全面,不过多说明了。直接上代码,仅做备份

package lab.sodino.annotation;

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

@Target(ElementType.FIELD)//仅为属性名的修饰有效
@Retention(RetentionPolicy.RUNTIME)//存在RetentionPolicy.RUNTIME、RetentionPolicy.CLASS、RetentionPolicy.SOURCE
@Documented//javadoc可生成相应文档
public @interface unique {

}

package lab.sodino.annotation;

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

@Target(ElementType.TYPE)//ElementType.TYPE用于标识类、接口(包括内注自身)、枚举
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface author {
	//修饰符仅可为public, protected, private & static的组合
	public static enum AppEnum {
		Web, Client, Service, Undesignated
	};

	//public & abstract的组合或默认
	AppEnum type() default AppEnum.Undesignated;

	String name() default "unknown";

	String webSite() default "N/A";
}

package lab.sodino.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * Java Annotation<br/>
 * 
 * 参考地址:http://www.blogjava.net/ericwang/archive/2005/12/13/23743.html<br/>
 * http://www.blogjava.net/weidagang2046/articles/27958.html<br/>
 * http://www.blogjava.net/mlh123caoer/archive/2007/09/06/143260.html<br/>
 * 官方文档:
 * http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html<br/>
 * */
public class AnnotationLab {
	public static void main(String[] args) {
		testField();
		testClazz();
	}

	private static void testClazz() {
		try {
			Class<?> clz = Class.forName(GoodsBean.class.getName());
			System.out.println(clz.isAnnotationPresent(author.class));
			author au = clz.getAnnotation(author.class);
			System.out.println(au.name() + " " + au.webSite());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

	private static void testField() {
		GoodsBean bean = GoodsBean.newInstance();
		Class<? extends GoodsBean> clazz = bean.getClass();
		Field[] arrFiled = clazz.getDeclaredFields();
		for (Field f : arrFiled) {
			Annotation[] arrAnno = f.getAnnotations();
			System.out.println(arrAnno.length);
			if (f.isAnnotationPresent(unique.class)) {
				System.out.println(f.getName() + " annotation:unique");
			} else {
				System.out.println(f.getName() + " annotation:null");
			}
		}
	}
}

package lab.sodino.annotation;

@author(name="sodino",webSite="sodino.com")//以"key=value"的形式进行赋值。
public class GoodsBean {
	@unique
	public long id;
	public String name;
	public long time;
	public int price;
	public boolean isPaid;
	/** 测试用。 */
	public byte testByte;
	/** 测试用。 */
	public short testShort;
	public float cicle;
	public double testDouble;

	public String toString() {
		StringBuffer strBuffer = new StringBuffer();
		strBuffer.append("id[" + id + "]\n");
		strBuffer.append("name[" + name + "]\n");
		strBuffer.append("time[" + time + "]\n");
		strBuffer.append("price[" + price + "]\n");
		strBuffer.append("isPaid[" + isPaid + "]\n");
		strBuffer.append("cicle[" + cicle + "]\n");
		strBuffer.append("testByte[" + testByte + "]\n");
		strBuffer.append("testShort[" + testShort + "]\n");
		strBuffer.append("testDouble[" + testDouble + "]\n");
		return strBuffer.toString();
	}

	public static GoodsBean newInstance() {
		GoodsBean bean = new GoodsBean();
		bean.id = 123456789l;
		bean.name = "AutoSave";
		bean.time = 12345234252342l;
		bean.price = 1024;
		bean.isPaid = true;
		bean.cicle = 2.356f;
		bean.testByte = 8;
		bean.testShort = 128;
		bean.testDouble = 9856.2145d;
		return bean;
	}
}


你可能感兴趣的:([Java] Annotation(内注)实例一则)