[JAVA] 注解学习@interface
2015-12-08 17:45
337 查看
一直都看框架级的代码中都是各种annotation,一起来看看到底怎么弄的
例子1:直接定义一个annotation,并使用之:
例子2:复杂类型的,各种注解类型
示例3:看原始注解的定义,可以看到也是使用注解,另外,使用value作为注解属性名,则可以省略注解名字不写,例如:@Rentention(RententionPolicy.RUNTIME):
参考:http://blog.csdn.net/liuwenbo0920/article/details/7290586/
例子1:直接定义一个annotation,并使用之:
package com.base.annotation.example; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * Created by guangyi on 15/12/8. */ @Retention(RetentionPolicy.RUNTIME) public @interface MyTarget { String value(); } package com.base.annotation.example; import java.lang.reflect.Method; /** * Created by guangyi on 15/12/8. */ public class MyTargetTest { @MyTarget("test") public void doSomething() { System.out.println("hello world"); } public static void main(String[] args) throws NoSuchMethodException{ Method method = MyTargetTest.class.getMethod("doSomething", null); if(method.isAnnotationPresent(MyTarget.class)) { System.out.println(method.getAnnotation(MyTarget.class)); // 打印@com.base.annotation.example.MyTarget(value=test) } } }
例子2:复杂类型的,各种注解类型
package com.base.annotation.example.example2; /** * Created by guangyi on 15/12/8. */ public class EnumTest { public static enum TrafficLamp { RED, GREEN, YELLOW } } package com.base.annotation.example.example2; import com.base.annotation.example.MyTarget; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * Created by guangyi on 15/12/8. */ @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String hello() default "hello"; String world(); int[] array() default {1, 2, 3, 4}; EnumTest.TrafficLamp lamp(); MyTarget myAnnotation() default @MyTarget("ddd"); Class style() default String.class; } package com.base.annotation.example.example2; import com.base.annotation.example.MyTarget; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * Created by guangyi on 15/12/8. */ @MyAnnotation(hello = "beijing", world = "shanghai", array = {5, 6, 7, 8}, lamp = EnumTest.TrafficLamp.GREEN, style = int.class) public class MyTest { @MyAnnotation(myAnnotation = @MyTarget("baby"), world = "shanghai", array = {10, 11, 12}, lamp = EnumTest.TrafficLamp.YELLOW) @Deprecated @SuppressWarnings("") public void output() { System.out.println("output something!"); } public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException{ MyTest myTest = new MyTest(); Class<MyTest> c = MyTest.class; Method method = c.getMethod("output", new Class[]{}); if(MyTest.class.isAnnotationPresent(MyAnnotation.class)) { System.out.println("have annotation"); } if(method.isAnnotationPresent(MyAnnotation.class)) { method.invoke(myTest, null); MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class); String hello = myAnnotation.hello(); String world = myAnnotation.world(); System.out.println(hello + ", " + world); System.out.println(myAnnotation.array().length); System.out.println(myAnnotation.myAnnotation().value()); System.out.println(myAnnotation.style()); System.out.println(myAnnotation.lamp()); } Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation.annotationType().getName()); } } }
示例3:看原始注解的定义,可以看到也是使用注解,另外,使用value作为注解属性名,则可以省略注解名字不写,例如:@Rentention(RententionPolicy.RUNTIME):
package java.lang; import java.lang.annotation.*; import static java.lang.annotation.ElementType.*; @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings { String[] value(); } package java.lang; import java.lang.annotation.*; import static java.lang.annotation.ElementType.*; @Documented @Retention(RetentionPolicy.RUNTIME) @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE}) public @interface Deprecated { } package java.lang.annotation; @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); } package java.lang.annotation; @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
参考:http://blog.csdn.net/liuwenbo0920/article/details/7290586/
相关文章推荐
- Java里List取并集方法retainAll不能用来判断是否有重复数据!
- 多线程
- MapReduce性能优化_8. 优化MapReduce的用户JAVA代码
- 序列化和反序列化,怎么保证数据的正确性
- 最全Pycharm教程(12)——Pycharm调试器之Java脚本调试
- Eclipse 中 新建maven项目 无法添加src/main/java 问题
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
- Java线程池中的异常消失
- Java编程思想学习心得(五)自动递增和递减操作符的陷阱
- java语言的特性有什么
- java开发工具eclipse中@author怎么改
- eclipse中run cofigurations 里面 Arguments 的两种参数详解
- 利用反射简单模拟Spring的控制反转(Ioc)和依赖注入(DI)
- 关于java.lang.UnsupportedClassVersionError解决方法总结
- Java GC 概念摘要
- 简述JAVA开发包的种类
- 使用JavaMail收发邮件示例备忘
- 一个JavaWeb项目开发总结
- Java编程思想学习心得(四)赋值操作符的陷阱
- 使用JAVA爬取博客里面的所有文章