Java Annotation
来源:互联网 发布:java log4j 配置文件 编辑:程序博客网 时间:2024/05/21 10:58
1. java.lang 内置注解
@Override/*表示当前方法是覆盖父类的方法*/@Deprecated /*表示当前元素是不赞成使用的*/@SuppressWarnings/*表示关闭一些不当的编译器警告信息*/2. 自定义注解
元注解
@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 /*允许子类继承父类中的注解*/e.g.
package Test_annotation; import java.lang.annotation.Documented; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.ElementType; import java.lang.annotation.RetentionPolicy; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited /* * 定义注解 Test * 注解中含有两个元素 id 和 description * description 元素 有默认值 "no description" */ public @interface Test { public int id(); public String description() default "no description"; }
package Test_annotation; import java.lang.reflect.Method; public class Test_1 { /* * 被注解的三个方法 */ @Test(id = 1, description = "hello method_1") public void method_1() { } @Test(id = 2) public void method_2() { } @Test(id = 3, description = "last method") public void method_3() { } /* * 解析注解,将Test_1类 所有被注解方法 的信息打印出来 */ public static void main(String[] args) { Method[] methods = Test_1.class.getDeclaredMethods(); for (Method method : methods) { /* * 判断方法中是否有指定注解类型的注解 */ boolean hasAnnotation = method.isAnnotationPresent(Test.class); if (hasAnnotation) { /* * 根据注解类型返回方法的指定类型注解 */ Test annotation = method.getAnnotation(Test.class); System.out.println("Test( method = " + method.getName() + " , id = " + annotation.id() + " , description = " + annotation.description() + " )"); } } } }
- JAVA Annotation & 自定义Annotation
- java Annotation Annotation elements
- Java Annotation
- Java Annotation
- Java Annotation
- Java Annotation
- Java Annotation
- java annotation
- Java Annotation
- Java Annotation
- Java Annotation
- Java Annotation
- JAVA [ Annotation]
- Java Annotation
- java annotation
- Java annotation
- Java Annotation
- JAVA --Annotation
- 使用OSGi,SCA,BPEL和Spring增强可管理性
- struts2中的跳转详解 ----“请求转发”和“重定向”之间的区别
- Openmeetings安装
- 整合SSH中getHibernateTemplate()中的update()和saveOrUpdate()方法没反应
- windows phone 墓碑化(9)
- Java Annotation
- HastSet中添加一个已有的元素的时候是根本就放不进去还是会替换掉已有的,使之不能重复?求解
- 回家休息
- 函数指针 类成员函数指针 类静态成员函数指针 使用实例
- Java的内存结构(Memory Structure)和垃圾收集(Garbage Collection)图解
- Java的内存布局
- HDU-1509 优先队列
- aspx和cs之间的关系
- 游戏开发两年之产品逻辑鸡肋么?