您的位置:首页 > 编程语言 > Java开发

JAVA注解

2013-01-03 13:57 253 查看
定义两个注解:

package annotation;

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

/*
* 元注解@Target,@Retention,@Documented,@Inherited
*
* @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:
* ElemenetType.CONSTRUCTOR 构造器声明
* ElemenetType.FIELD 域声明(包括 enum 实例)
* ElemenetType.LOCAL_VARIABLE 局部变量声明
* ElemenetType.METHOD 方法声明
* ElemenetType.PACKAGE 包声明
* ElemenetType.PARAMETER 参数声明
* ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
*
* @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
* RetentionPolicy.SOURCE 注解将被编译器丢弃
* RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
* RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
*
* @Documented 将此注解包含在 javadoc 中
*
* @Inherited 允许子类继承父类中的注解
*
*/

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited

public @interface Test {
public static final String defaultDesc = "no description";
public int id();
public String desc() default defaultDesc;
}

package annotation;

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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited

public @interface Test2 {
public String desc() default "i am test2Annotation";
}


使用注解:
package annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class AnnotationTest {

@Test(id =1,desc="hello method_1")
public void method_1(){

}
@Test(id =2)
@Test2(desc="aaaaaaaaa")
public void method_2(){

}
@Test(id =3,desc="hello method_3")
public void method_3(String name1,String name2){
System.out.println(name1 + " " + name2 + " , method_3 is executed");
}

public static String getDesc(Test a1,Test2 a2){
String desc = "" ;
if(a1.desc().equals(Test.defaultDesc)){
desc = a2.desc();
}else{
desc = a1.desc();
}
return desc;
}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

Method [] methods = AnnotationTest.class.getDeclaredMethods();
for(Method m:methods){
if(m.isAnnotationPresent(Test.class)){
Test annotation = m.getAnnotation(Test.class);
Test2 annotation2 = m.getAnnotation(Test2.class);

System.out.println("Test( Method = "+ m.getName() + ",id = " + annotation.id()
+ ",desc = "+ getDesc(annotation,annotation2)+ ")");
Annotation [] annos = m.getAnnotations();
for(Annotation anno:annos){
System.out.println(anno.annotationType());
}
if(annotation.id()==3){
m.invoke(new AnnotationTest(), new Object[]{"cche","ngmanwa"});
}

}
}

}

}


运行结果:
Test( Method = method_1,id = 1,desc = hello method_1)
interface annotation.Test
Test( Method = method_2,id = 2,desc = aaaaaaaaa)
interface annotation.Test
interface annotation.Test2
Test( Method = method_3,id = 3,desc = hello method_3)
interface annotation.Test
cche ngmanwa , method_3 is executed
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA 注解