`
YANFAN0916
  • 浏览: 30131 次
文章分类
社区版块
存档分类
最新评论

重复造轮子:注释的使用

 
阅读更多
package com.metarnet.model;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 注释测试
 * @author yanfan
 * 不使用已指定的数据类型,而使用注释来标注其数据类型
 * 其中有四个类:MainTest(主函数,用于测试)
 * 		TestNote(MODEL,用于存放数据)
 * 		NoteEnum(定义注释中的属性可以存放的值)
 * 		Note(注释)
 * 使用@Note(Name=NoteEnum.String)来标注一个数据
 */
public class MainTest {

	public static void main(String[] args) {
		TestNote note = new TestNote();
		try {
			note.setTestInt(123);
			note.setTestDate(new Date());
			note.setTestString("Test");
			//note.setTestString("Test");
			System.out.println("TestDate:"+note.getTestDate());
			System.out.println("TestInt:"+note.getTestInt());
			System.out.println("TestString:"+note.getTestString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

/**
 * 在此声明三个参数,全部使用String类型,但使用注释标注它的类型
 * @author yanfan
 *
 */
class TestNote {
	@Note(Name=NoteEnum.String)
	protected String TestString;
	@Note(Name=NoteEnum.Int)
	protected String TestInt;
	@Note(Name=NoteEnum.Date)
	protected String testDate;
	public Object getTestString() throws Exception {
		return GET("TestString");
	}
	public void setTestString(Object testString) throws Exception{
		SET("TestString", testString);
	}
	public Object getTestInt() throws Exception {
		return GET("TestInt");
	}
	public void setTestInt(Object testInt) throws Exception{
		SET("TestInt", testInt);
	}
	public Object getTestDate() throws Exception {
		return GET("testDate");
	}
	public void setTestDate(Object testDate) throws Exception{
		SET("testDate", testDate);
	}
	private void SET(String Name,Object value) throws Exception
	{
		Field field = TestNote.class.getDeclaredField(Name);
		if(field.getAnnotation(Note.class).Name() == NoteEnum.Date)
		{
			field.set(this,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value));
		}else if(field.getAnnotation(Note.class).Name() == NoteEnum.Int){
			field.set(this,value.toString());
		}else {
			field.set(this,value);
		}
	}
	private Object GET(String Name)throws Exception
	{
		Field field = TestNote.class.getDeclaredField(Name);
		if(field.getAnnotation(Note.class).Name() == NoteEnum.Date)
		{
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(field.get(this).toString());
		}else if(field.getAnnotation(Note.class).Name() == NoteEnum.Int){
			return Integer.parseInt(field.get(this).toString());
		}else {
			return field.get(this).toString();
		}
	}
}


/**
 * 定义可以在注释中选择的参数
 * @author yanfan
 *
 */
enum NoteEnum {
	String,
	Date,
	Int
}

/**
 * 定义一个可以使用在参数上的注释
 * @author yanfan
 *
 */
	/**
	 * CONSTRUCTOR : 构造器的声明
	 * FIELD : 域声明(包括enum实例)
	 * LOCAL_VARIABLE : 局部变量声明
	 * METHOD : 方法声明
	 * PACKAGE : 包声明
	 * PARAMETER : 参数声明
	 * TYPE : 类、接口 (包括注解类型) 或enum声明
	 */
@Target(ElementType.FIELD)
	/**
	 * SOURCE : 注释将被编译器丢掉
	 * CLASS : 注释在class文件中可用,但会被VM丢弃
	 * RUNTIME : VM将在运行时也保留注释,因此可以通过反射机制读取注释的信息。
	 */
@Retention(RetentionPolicy.RUNTIME) 
@interface Note {
	public NoteEnum Name();//返回为枚举类型
}

(这是执行正确返回的值,可以明显的看出来其数据类型)

当我将测试类型设置为这个之后,


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics