论坛首页 Java企业应用论坛

annotation初探

浏览 2379 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-03-05  
OO

今天研究Annotation分析JavaBean信息,通过在JavaBean中添加的注解来获得信息真是非常方便。

Annotation是在已经发布的JDK1.5(tiger)中增加新的特色。它提供一种机制,将程序的元素如:类,方法,属性,参数,本地变量,包和元数据联系起来。这样编译器可以将元数据存储在Class文件中。这样虚拟机和其它对象可以根据这些元数据来决定如何使用这些程序元素或改变它们的行为。

定义Annotation

/**
 * @(#) Id.java 1.0, 2008-3-5
 * Copyright 徐国智. All rights reserved.
 * QQ:13629995    MSN:xgzcc@hotmail.com
 * Email:lidxgz@163.com,lidxgz@gmail.com
 */

package com.xugz.core.annotation;

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

/**
 * @author 徐国智
 *
 * @version 1.0,2008-3-5 上午10:45:57
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Id {
	
}

 

 

/**
 * @(#) Column.java 1.0, 2008-3-5
 * Copyright 徐国智. All rights reserved.
 * QQ:13629995    MSN:xgzcc@hotmail.com
 * Email:lidxgz@163.com,lidxgz@gmail.com
 */

package com.xugz.core.annotation;

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

/**
 * @author 徐国智
 *
 * @version 1.0,2008-3-5 上午10:50:42
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Column {
	String name() default "";
}

 

 

说明:

Retention指示注释类型的注释要保留多久。

RetentionPolicy注释保留策略。枚举类型
CLASS
          编译器将把注释记录在类文件中,但在运行时 VM 不需要保留注释。
RUNTIME
          编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。
SOURCE
          编译器要丢弃的注释。
Target指示注释类型所适用的程序元素的种类。
ElementType程序元素类型。枚举类型
ANNOTATION_TYPE
          注释类型声明
CONSTRUCTOR
          构造方法声明
FIELD
          字段声明(包括枚举常量)
LOCAL_VARIABLE
          局部变量声明
METHOD
          方法声明
PACKAGE
          包声明
PARAMETER
          参数声明
TYPE
          类、接口(包括注释类型)或枚举声明

在JavaBean上添加注解

/**
 * @(#) Word.java 1.0, 2008-3-4
 * Copyright 2007 徐国智. All rights reserved.
 * QQ:13629995;MSN:xgzcc@hotmail.com;
 * Email:lidxgz@163.com
 */

package com.xugz.bean;

import com.xugz.core.annotation.Column;
import com.xugz.core.annotation.Id;

/**
 * @author Xugz
 *
 * @version 1.0,2008-3-4
 */
public class Word {
/**
 *为属性wordId添加注解,指定Id属性和字段名
*/
	@Id @Column(name="id")
	private Integer wordId;
	private String word;
	private String explain;
	private String example;
	private Integer state;
	private Integer learnCount;
	private Integer newOrder;
/**
*getter和setter方法省略
*/
}

 

 

利用反射机制分析JavaBean

/**
 * @(#) Reflect.java 1.0, 2008-3-5
 * Copyright 徐国智. All rights reserved.
 * QQ:13629995    MSN:xgzcc@hotmail.com
 * Email:lidxgz@163.com,lidxgz@gmail.com
 */

package com.xugz.core.reflect;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.xugz.core.annotation.Column;
import com.xugz.core.annotation.Id;

/**
 * @author 徐国智
 * 
 * @version 1.0,2008-3-5 上午10:54:32
 */
public class Reflect {
	private String className;

	private Class cl;

	public Reflect(String className) {
		this.className = className;
		try {
			cl = Class.forName(className);
		} catch (Exception e) {
		}
	}

	public String getId() {
		try {
			if (cl != null && !cl.isInstance(Object.class)) {
				Field[] fields = cl.getDeclaredFields();
				for (Field field : fields) {
					Id id = field.getAnnotation(Id.class);
					if (id == null)
						continue;
					return getColumnName(field.getName());
				}
			}
		} catch (Exception e) {

		}
		return null;
	}

	public String getColumnName(String name) {
		try {
			if (cl != null && !cl.isInstance(Object.class)) {
				Field field = cl.getDeclaredField(name);
				Column column = field.getAnnotation(Column.class);
				if (column == null)
					return field.getName();
				else
					return column.name();

			}
		} catch (Exception e) {

		}
		return null;
	}
	public List<String> getFieldNames(){
		List<String> list = new ArrayList<String>();
		try {
			if (cl != null && !cl.isInstance(Object.class)) {
				Field[] fields = cl.getDeclaredFields();
				for(Field field:fields){
				Column column = field.getAnnotation(Column.class);
				if (column == null)
					list.add(field.getName());
				else
					list.add(column.name());
				}
				return list;
			}
		} catch (Exception e) {

		}
		return null;
	}
}

 

 

测试

/**
 * @(#) TestAnnotation.java 1.0, 2008-3-5
 * Copyright 徐国智. All rights reserved.
 * QQ:13629995    MSN:xgzcc@hotmail.com
 * Email:lidxgz@163.com,lidxgz@gmail.com
 */

package test;

import java.util.List;

import com.xugz.core.reflect.Reflect;

/**
 * @author 徐国智
 *
 * @version 1.0,2008-3-5 上午11:08:41
 */
public class TestAnnotation {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成方法存根
		Reflect reflect = new Reflect("com.xugz.bean.Word");
		System.out.println("id===="+reflect.getId());
		List<String> list = reflect.getFieldNames();
		for(String name:list){
			System.out.println(name);
		}
	}

}

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics