zoukankan      html  css  js  c++  java
  • 注释创建与应用

    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;

    @Target(ElementType.TYPE)//这个标注应用于类
    @Retention(RetentionPolicy.RUNTIME)//标注会一直保留到运行时
    @Documented//将此注解包含在javadoc中
    public @interface Description {
    String value();
    }
    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;
    
    //注意这里的@Target与@Description里的不同,参数成员也不同
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface Name {
        String originate();
        String community();
    }
    
    
    @Description(value="javaeye,做最棒的软件开发交流社区")
    public class JavaEyer {
        @Name(originate="创始人:robbin",community="javaEye")
        public String getName()
        {
            return null;
        }
    
        @Name(originate="创始人:江南白衣",community="springside")
        public String getName2()
        {
            return "借用两位的id一用,写这一个例子,请见谅!";
        }
    }
    import java.lang.reflect.Method;
    import java.util.HashSet;
    import java.util.Set;
    
    public class TestAnnotation {
        /**
         * author lighter
         * 说明:具体关天Annotation的API的用法请参见javaDoc文档
         */
        public static void main(String[] args) throws Exception {
            String CLASS_NAME = "lighter.javaeye.com.JavaEyer";
            //        返回CLASS_NAME路径类下的所有类
            Class test = Class.forName(CLASS_NAME);
            //        获得所有方法
            Method[] method = test.getMethods();
            //        Description注释是否存在
            boolean flag = test.isAnnotationPresent(Description.class);
            if(flag)
            {
            //        引用Description注释
                Description des = (Description)test.getAnnotation(Description.class);
                System.out.println("描述:"+des.value());
                System.out.println("-----------------");
            }
    
            //把JavaEyer这一类有利用到@Name的全部方法保存到Set中去
            Set<Method> set = new HashSet<Method>();
            for(int i=0;i<method.length;i++)
            {
                boolean otherFlag = method[i].isAnnotationPresent(Name.class);
                if(otherFlag) set.add(method[i]);
            }
            for(Method m: set)
            {
                Name name = m.getAnnotation(Name.class);
                System.out.println(name.originate());
                System.out.println("创建的社区:"+name.community());
            }
        }
    }
  • 相关阅读:
    R12.2.4 ORA-01017: invalid username/password; logon denied
    VBA 判断一个TXT编码方式,再创建一个新的文件,复制数据进去
    查看功能所挂在的菜单
    EBS WebADI 存储过程增加参数
    用C++实现半透明按钮控件(PNG,GDI+)
    Linux五种IO模型性能分析
    用DirectX实现多视图渲染
    论YUV422(YUYV)与YUV420相互转换
    图文详解YUV420数据格式
    YUV422 YUV420 Planar Semi-Planar Interleaved YCbCr与YUV
  • 原文地址:https://www.cnblogs.com/xxupup/p/14804612.html
Copyright © 2011-2022 走看看