注解的定义和使用
来源:互联网 发布:用c语言求最大公约数 编辑:程序博客网 时间:2024/06/06 08:50
package com.example.test;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;/** * 注解的定义和使用 */public class AnnotationDefineAndUse { @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyAnnotation2 { public int hello() default 0; } @Target({ElementType.TYPE, ElementType.METHOD})//作用范围为 类、方法 @Retention(RetentionPolicy.RUNTIME) //运行时生效 @Documented public @interface MyAnnotation1 { } /** * 测试注解 */ public static void main(String[] args) { Test t = new Test(); //使用遍历的方式获取 /*Annotation[] an= t.getClass().getAnnotations(); for(Annotation a:an){ System.out.println(a.annotationType().getName()); }*/ if (t.getClass().isAnnotationPresent(MyAnnotation1.class)) {//如果类的注解存在 MyAnnotation1 my1 = t.getClass().getAnnotation(MyAnnotation1.class); System.out.println(my1.annotationType().getName()); System.out.println("你得到了类的注解"); } try { Method method1 = t.getClass().getMethod("test1", new Class[]{}); Method method2 = t.getClass().getMethod("test2", new Class[]{}); Method method3 = t.getClass().getMethod("test3", new Class[]{}); try { //方法的调用 method1.invoke(t, null); System.out.println(method1.getModifiers()); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //如果方法的注解存在 if (method1.isAnnotationPresent(MyAnnotation1.class)) { MyAnnotation1 my1 = method1.getAnnotation(MyAnnotation1.class); System.out.println(my1.annotationType().getName()); System.out.println("你得到了方法test1上的MyAnnotation1注解"); } if (method2.isAnnotationPresent(MyAnnotation2.class)) { MyAnnotation2 my2 = method2.getAnnotation(MyAnnotation2.class); System.out.println(my2.hello());//得到注解默认值 System.out.println("你得到了方法test2上的MyAnnotation2注解"); } if (method3.isAnnotationPresent(MyAnnotation2.class)) { MyAnnotation2 my3 = method3.getAnnotation(MyAnnotation2.class); System.out.println(my3.hello());//得到赋予的值 System.out.println("你得到了方法test3上的MyAnnotation2注解"); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } }}@MyAnotationTest.MyAnnotation1class Test { @MyAnotationTest.MyAnnotation1 public void test1() { System.out.println("你使用了MyAnnotation1"); } @MyAnotationTest.MyAnnotation2 public void test2() { System.out.println("你使用了MyAnnotation2默认值"); } @MyAnotationTest.MyAnnotation2(hello = 3) public void test3() { System.out.println("你使用了MyAnnotation2并为其赋值"); }}
阅读全文
0 0
- 注解的定义和使用
- JAVA自定义注解的使用和定义
- java注解之注解的使用和定义
- 自定义注解定义和使用
- Java注解类的定义/使用和反射
- 注解的定义和应用
- java注解定义和使用详解
- 使用Spring注解完成Bean的定义
- 注解类型的定义及使用
- 使用Spring注解完成Bean的定义
- java中注解的定义和注解的获取
- 注解的定义和反射调用
- 注解【定义+使用+读取】
- 使用spring和jdk1.7开发web项目时出现的注解定义pointcut的问题
- 注解分类和定义
- 注解的使用和自定义注解
- Java 注解的定义
- 注解的学习和使用
- POJ 1011--Sticks
- 逻辑智力题
- 数论的几个重要的定理记一波地址
- 人工智能开发语言 =Python
- 51Nod-1773-A国的贸易
- 注解的定义和使用
- Python标准库--re
- Spring Boot 快速入门
- Linux下如何开启MySQL的log_bin日志
- 在 Windows 上搭建本地 Jekyll 编译环境时问题汇总
- UC浏览器主界面滑动折叠效果 使用自定义behavior实现 难度五颗星*****
- 《Javascript高级程序设计(第3版)》阅读笔记(持续更新)
- LINUX学习之GIT
- Activity管理器-2