java annotation注释

java annotation讲解 :http://danwind.iteye.com/blog/1140831/

 

java annotation实例:

 

package com.annotation;

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.METHOD})
public @interface DataBaseBean
{
	public int i = 0;
	public String value() default "mysql";
}

 

利用java反射,和注释的测试用例

   class DataBase{

	@DataBaseBean
	private String name;
	@OracleAnnotation("oracle")
	private Object driver;
	private int length;
	@DataBaseBean
	public String getName(String j,String i, int ii,java.util.Date d)
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Object getDriver()
	{
		return driver;
	}
	public void setDriver(Object driver)
	{
		this.driver = driver;
	}
	public int getLength()
	{
		return length;
	}
	public void setLength(int length)
	{
		this.length = length;
	}
	
}
 

 

 

public class ReflectAnnotation
{
	public static void main(String[] args)
	{
		try
		{
			Class pojo = Class.forName("com.annotation.DataBase");
			Field[] filedList = pojo.getDeclaredFields();
			Method[] methodList = pojo.getDeclaredMethods();
			System.out.println("getClassEle----------------");
			for (int i = 0; i < filedList.length; i++)
			{
				Field file = filedList[i];
				System.out.println(Modifier.toString(file.getModifiers())+"    "+file.getType()+"    "+file.getName());
				Annotation[] annotations = file.getAnnotations();
				
				for (int j = 0; j < annotations.length; j++)
				{
					if(annotations[j].annotationType().equals(DataBaseBean.class))
					{
						DataBaseBean anno = (DataBaseBean)annotations[j];
						System.out.println("value值为"+anno.value());
					}
					else if(annotations[j].annotationType().equals(OracleAnnotation.class))
					{
						OracleAnnotation anno = (OracleAnnotation)annotations[j];
						System.out.println("value值为"+anno.value());
					}
					System.out.println("filed上的注释为"+annotations[j].annotationType());
				}
			}
			for (int i = 0; i < methodList.length; i++)
			{
				Method method = methodList[i];
				boolean MethodAnnotation = method.isAnnotationPresent(DataBaseBean.class);
			
				if(MethodAnnotation)
				{
					Class parameters[] = method.getParameterTypes();
				    System.out.print(Modifier.toString(method.getModifiers()) + " " + method.getReturnType() + " " + method.getName() + " (");
				    for(int k = 0;k < parameters.length;k++)
				    {
				     System.out.print("参数列表:"+parameters[k].toString());
				    }
				    DataBaseBean databaseBean = method.getAnnotation(DataBaseBean.class);
				    System.out.println(databaseBean.value());
				}
			}
		}
		catch (Exception e)
		{
			System.out.println("参数报错");
		}
	}
	
}
 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值