首页 > 学院 > 开发设计 > 正文

运行时注解解析

2019-11-06 06:56:08
字体:
来源:转载
供稿:网友

运行时 Annotation 指 @Retention 为 RUNTIME 的 Annotation,可手动调用下面常用 API 解析 method.getAnnotation(AnnotationName.class); method.getAnnotations(); method.isAnnotationPResent(AnnotationName.class); 运行期注解解析案例:

定义一个自定义注解

package org.vincent.maven.annotation.anno;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;/** * 自定义注解 * * @author PengRong * */@Documented@Retention(RetentionPolicy.RUNTIME) // 这个自定义注解生命到运行期@Target(ElementType.METHOD)@Inheritedpublic @interface MethodInfo { String author() default "Vicent"; String date() default "2017/03/03"; int version() default 1;}

在一个类中使用上面定义的注解:

package org.vincent.maven.annotation;import org.vincent.maven.annotation.anno.MethodInfo;/** * 使用自定义的注解 * * @ClassName: App * @Description: TODO(这里用一句话描述这个类的作用) * @author PengRong * @date 2017年3月5日 下午9:52:55 * */public class App { @MethodInfo(author = "Cindy", date = "day", version = 2) public static void main(String[] args) { System.out.println("Hello World!"); } @Override public java.lang.String toString() { // TODO Auto-generated method stub return super.toString(); } @Deprecated private void xx() { // TODO Auto-generated method stub } @SuppressWarnings(value = { "deprecation", "unchecked" }) private void y() { // TODO Auto-generated method stub }}

测试:

package org.vincent.maven.annotation;import java.lang.annotation.Annotation;import java.lang.reflect.Method;public class AnnotationParser { public static void main(String[] args) throws ClassNotFoundException { // 测试AnnotationTest类,得到被注解的类对象 Class c = Class.forName("org.vincent.maven.annotation.App"); // 获取该类所有声明的方法 Method[] methods = c.getDeclaredMethods(); // 声明注解集合 Annotation[] annotations; // 遍历所有的方法得到各方法上面的注解信息 for (Method method : methods) { // 获取每个方法上面所声明的所有注解信息 annotations = method.getDeclaredAnnotations(); // 再遍历所有的注解,打印其基本信息 System.out.println("--------------------------------------------------"); System.out.println("方法: " + method.getName()); for (Annotation an : annotations) { System.out.println("方法名为:" + method.getName() + " 其上面的注解为:" + an.annotationType().getSimpleName()); Method[] meths = an.annotationType().getDeclaredMethods();// 通过反射获取到注解类声明的所有成员变量 // 遍历注解类的所有成员变量 for (Method meth : meths) { System.out.println("注解的变量名为:" + meth.getName()); } } System.out.println("--------------------------------------------------"); } }}

输出结果:

--------------------------------------------------方法: main方法名为:main 其上面的注解为:MethodInfo注解的变量名为:version注解的变量名为:date注解的变量名为:author----------------------------------------------------------------------------------------------------方法: toString----------------------------------------------------------------------------------------------------方法: xx方法名为:xx 其上面的注解为:Deprecated----------------------------------------------------------------------------------------------------方法: y--------------------------------------------------
上一篇:引用

下一篇:排序算法(2)——堆排序

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表