zoukankan      html  css  js  c++  java
  • Annotation

       在进行类或方法定义的时候,都可以使用一系列的Annotation(public interface Annotation)进行声明,如果想要获取这些Annotation的信息,可以直接通过反射来完成。在 java.lang.reflect 里面有一个AccessibleObject类,在本类中提供有Annotation类的方法:

      1.获取全部Annotation:public Annotation[] getAnnotations();

      2.获取指定Annotation:public <T extends Annotation> T getAnnotation​(Class<T> annotationClass)

    package com.annotation.demo;
    
    import java.io.Serializable;
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
    
    @FunctionalInterface
    @Deprecated(forRemoval = false,since = "1.1")
    interface IMessage{
        public void send(String msg);
    }
    
    @SuppressWarnings("serial")
    class MessageImpl implements IMessage,Serializable{
        @Override
        @Deprecated(since = "1.3")
        public void send(String msg) {
            System.out.println("【消息发送】" + msg);
        }
    }
    
    public class annotationdemo {
        public static void main(String[] args) throws Exception {
            {
                System.out.println("-----------------------IMessage接口:-----------------------------");
                Annotation annotations[] = IMessage.class.getAnnotations();//获取接口上的全部Annotation
                for(Annotation annotation:annotations){
                    System.out.println(annotation);
                }
            }
            
            {
                System.out.println("-----------------------MessageImpl子类:-----------------------------");
                Annotation annotations[] = MessageImpl.class.getAnnotations();//获取MessageImpl子类上的全部Annotation
                for(Annotation annotation:annotations){
                    System.out.println(annotation);
                }
            }
    
            {
                System.out.println("-----------------------send()方法:-----------------------------");
                Method method = MessageImpl.class.getDeclaredMethod("send", String.class);
                Annotation annotations[] = method.getAnnotations();//获取send方法上的全部Annotation
                for(Annotation annotation:annotations){
                    System.out.println(annotation);
                }
            }
        }
    }

    不同的Annotation有不同的作用范围:

    Annotation名称 作用范围                          
    @SuppressWarnings
     

    @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})

    @Retention(RetentionPolicy.SOURCE)

    public @interface SuppressWarnings {}

    @FunctionalInterface

    @Documented

    @Retention(RetentionPolicy.RUNTIME)

    @Target(ElementType.TYPE)

    public @interface FunctionalInterface {}

    @Deprecated
     @Documented

    @Retention(RetentionPolicy.RUNTIME)

    @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE})

  • 相关阅读:
    Mongodb 与 MySQL对比
    MongoDB的真正性能-实战百万用户
    走进科学之揭开神秘的"零拷贝"!
    对于 Netty ByteBuf 的零拷贝(Zero Copy) 的理解
    <Netty>(入门篇)TIP黏包/拆包问题原因及换行的初步解决之道
    MSSQL复制功能实现与Oracle数据库同步
    SQLServer与Oracle的数据同步(触发器trigger)
    ORACLE和SQL SERVER的数据同步常用方法
    MS SQL SERVER: msdb.dbo.MSdatatype_mappings & msdb.dbo.sysdatatypemappings
    FORM 错误:此责任无可用函数。 更改责任或与您的系统管理员联系。
  • 原文地址:https://www.cnblogs.com/sunzhongyu008/p/11224907.html
Copyright © 2011-2022 走看看