java annotation讲解 :http://danwind.iteye.com/blog/1140831/
java annotation实例:
java">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";
}
class DataBase{
java"> @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;
}
}
java">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("参数报错");
}
}
}