博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义Java Annotations实例以及用Java Reflection来解析自定义的Annotation
阅读量:5045 次
发布时间:2019-06-12

本文共 5738 字,大约阅读时间需要 19 分钟。

转自

Java程序员都知道,在Java世界里到处可见@Override, @Deprecated等等这些Annotations, 这些代码不仅不影响Java代码编译运行,还能对代码起到很好的解释作用。

那么我们如何定义自己的Annotation呐? 接下来就是很好的例子!

package com.journaldev.annotations; 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; @Documented@Target(ElementType.METHOD)@Inherited@Retention(RetentionPolicy.RUNTIME)public @interface MethodInfo{    String author() default "Pankaj";    String date();    int revision() default 1;    String comments();}

需要注意的几点:

1、Annotation方法不能有参数

2、Annotation方法的返回类型仅限于primitives,例如String, int等等

3、Annotation方法可以有默认值

4、Annotations可以附带一些meta annotations,这些meta annotations向外提供了一些改annotation的信息,

它有以下四种类型:

   1)@Documented – indicates that elements using this annotation should be documented by javadoc and similar tools. This type should be used to annotate the declarations of types whose annotations affect the use of annotated elements by their clients. If a type declaration is annotated with Documented, its annotations become part of the public API of the annotated elements.

   2)@Target – indicates the kinds of program element to which an annotation type is applicable. Some possible values are TYPE, METHOD, CONSTRUCTOR, FIELD etc. If Target meta-annotation is not present, then annotation can be used on any program element.

   3)@Inherited – indicates that an annotation type is automatically inherited. If user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class’s superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached.

   4)@Retention – indicates how long annotations with the annotated type are to be retained. It takes RetentionPolicy argument whose Possible values are SOURCE, CLASS and RUNTIME

微笑抱歉我偷懒了。。

Java内置Annotations

1、@Override,告诉编译器,我们重载了父类的方法,如果父类方法有变或被移除,编译器将对此编译出错

2、@Deprecated,如果我们想弃用哪个方法,就会使用这个annotation,这个会告诉编译器这个方法已弃用

3、@SuppressWarnings,这个只是告诉编译器忽略这个warning。

来看个实例吧

package com.journaldev.annotations; import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.List; public class AnnotationExample {     public static void main(String[] args) {    }     @Override    @MethodInfo(author = "Pankaj", comments = "Main method", date = "Nov 17 2012", revision = 1)    public String toString() {        return "Overriden toString method";    }     @Deprecated    @MethodInfo(comments = "deprecated method", date = "Nov 17 2012")    public static void oldMethod() {        System.out.println("old method, don't use it.");    }     @SuppressWarnings({ "unchecked", "deprecation" })    @MethodInfo(author = "Pankaj", comments = "Main method", date = "Nov 17 2012", revision = 10)    public static void genericsTest() throws FileNotFoundException {        List l = new ArrayList();        l.add("abc");        oldMethod();    } }

解析Java Annotation

package com.journaldev.annotations; import java.lang.annotation.Annotation;import java.lang.reflect.Method; public class AnnotationParsing {     public static void main(String[] args) {        try {            for (Method method : AnnotationParsing.class                    .getClassLoader()                    .loadClass(("com.journaldev.annotations.AnnotationExample"))                    .getMethods()) {                // checks if MethodInfo annotation is present for the method                if (method                        .isAnnotationPresent(com.journaldev.annotations.MethodInfo.class)) {                    try {                        // iterates all the annotations available in the method                        for (Annotation anno : method.getDeclaredAnnotations()) {                            System.out.println("Annotation in Method '"                                    + method + "' : " + anno);                        }                        MethodInfo methodAnno = method                                .getAnnotation(MethodInfo.class);                        if (methodAnno.revision() == 1) {                            System.out.println("Method with revision no 1 = "                                    + method);                        }                     } catch (Throwable ex) {                        ex.printStackTrace();                    }                }            }        } catch (SecurityException | ClassNotFoundException e) {            e.printStackTrace();        }    } }
输出将会是:

Annotation in Method 'public java.lang.String com.journaldev.annotations.AnnotationExample.toString()' : @com.journaldev.annotations.MethodInfo(author=Pankaj, revision=1, comments=Main method, date=Nov 17 2012)Method with revision no 1 = public java.lang.String com.journaldev.annotations.AnnotationExample.toString()Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.oldMethod()' : @java.lang.Deprecated()Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.oldMethod()' : @com.journaldev.annotations.MethodInfo(author=Pankaj, revision=1, comments=deprecated method, date=Nov 17 2012)Method with revision no 1 = public static void com.journaldev.annotations.AnnotationExample.oldMethod()Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.genericsTest() throws java.io.FileNotFoundException' : @com.journaldev.annotations.MethodInfo(author=Pankaj, revision=10, comments=Main method, date=Nov 17 2012)
以上就是Java Annotation一个简单例子, Hope u enjoy it 。

转载于:https://www.cnblogs.com/java20130722/p/3206782.html

你可能感兴趣的文章
Cocos2d-x3.0 文件处理
查看>>
全面整理的C++面试题
查看>>
Activity和Fragment生命周期对比
查看>>
OAuth和OpenID的区别
查看>>
android 分辨率自适应
查看>>
查找 EXC_BAD_ACCESS 问题根源的方法
查看>>
国外媒体推荐的5款当地Passbook通行证制作工具
查看>>
日常报错
查看>>
list-style-type -- 定义列表样式
查看>>
hibernate生成表时,有的表可以生成,有的却不可以 2014-03-21 21:28 244人阅读 ...
查看>>
mysql-1045(28000)错误
查看>>
Ubuntu 编译出现 ISO C++ 2011 不支持的解决办法
查看>>
1.jstl c 标签实现判断功能
查看>>
Linux 常用命令——cat, tac, nl, more, less, head, tail, od
查看>>
超详细的Guava RateLimiter限流原理解析
查看>>
VueJS ElementUI el-table 的 formatter 和 scope template 不能同时存在
查看>>
Halcon一日一练:图像拼接技术
查看>>
Swift - RotateView
查看>>
iOS设计模式 - 中介者
查看>>
centos jdk 下载
查看>>