zoukankan      html  css  js  c++  java
  • 自定义Java Annotations实例以及用Java Reflection来解析自定义的Annotation

    转自 http://www.journaldev.com/721/java-annotations-tutorial-with-custom-annotation-example-and-parsing-using-reflection

    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 。


  • 相关阅读:
    Spring线程池由浅入深的3个示例
    ThreadPoolExecutor之一:使用基本介绍
    Spring中的线程池ThreadPoolTaskExecutor介绍
    ThreadPoolTaskExecutor异常收集
    SPRING中的线程池ThreadPoolTaskExecutor
    ThreadPoolTaskExecutor的配置解释
    jenkins邮件配置
    使用Jenkins配置自动化构建
    Hudson和Jenkins的关系
    PV 和 UV IP
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206782.html
Copyright © 2011-2022 走看看