zoukankan      html  css  js  c++  java
  • 获取方法标注少

    注释不与原代码的操作干扰。在标志中发挥作用。注释工具允许第三方募集

    记笔记,要完成所需的任务。

    package two.zj;
    
    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;
    @Documented
    @Retention<span style="color:#ff0000;">(</span><span style="background-color: rgb(255, 0, 0);">RetentionPolicy.RUNTIME</span><span style="color:#ff0000;">)</span>
    @Target(ElementType.METHOD)
    public @interface TestZJ {
    	public int id() default 1;
    	public String name() default "nametest";
    	public String value() ;
    	public abstract int [] intt();
    	public abstract TestEnum[] tenum();
    	
    	
    }
    
    package two;
    
    
    import two.zj.TestEnum;
    import two.zj.TestZJ;
    public class UserAnn {
    <span style="white-space:pre">	</span>/**
    <span style="white-space:pre">	</span> * @param user
    <span style="white-space:pre">	</span> * @return user
    <span style="white-space:pre">	</span> */
    <span style="white-space:pre">	</span>@TestZJ(id=22,name="liyy", intt = { 0,1,2,3 }, value = "", tenum = {TestEnum.TEST1})
    <span style="white-space:pre">	</span>public User getUser(User user) {
    <span style="white-space:pre">		</span>return user;
    <span style="white-space:pre">	</span>}
    
    
    }
    
    package two;


    import java.lang.reflect.Method;


    import two.zj.TestZJ;


    public class Testmain {


    public static void main(String[] args) throws ClassNotFoundException {
    // Class class1 = Class.forName("two.UserAnn");
    Class class1 = UserAnn.class;
    Method [] methods = class1.getDeclaredMethods();
    for(Method method:methods){
    TestZJ annotations = method.getAnnotation(TestZJ.class);
    if(annotations !=null){
    System.out.println(annotations.id());
    }
    }
    }
    }

    当注解类TestZJ中的@Retention(RetentionPolicy.RUNTIME)   为source 或者class 时候 反射是获取不到注解类的。由于jdk用说明

    public enum RetentionPolicy {
        /**
         * Annotations are to be discarded by the compiler.
         */
        SOURCE,


        /**
         * Annotations are to be recorded in the class file by the compiler
         * but need not be retained by the VM at run time.  This is the default
         * behavior.
         */
        CLASS,


        /**
         * Annotations are to be recorded in the class file by the compiler and
         * retained by the VM at run time, so they may be read reflectively.
         *
         * @see java.lang.reflect.AnnotatedElement
         */
        RUNTIME
    }

    SOURCE在编译的c字节码中去掉注解。CLASS 字节码中有注解可是不载入到虚拟机。RUNTIME能够通过反射获取到注解信息,而注解框架也是採用的这样的原理。

    
    

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    scanf函数读入整数后接着读字符串的换行符残余问题
    二分查找的细节
    PAT 1016 Phone Bills
    1044. Shopping in Mars (25)-PAT甲级真题(二分查找)
    第0次作业
    FPGA开发流程
    quartus2 13.0+modelsim联合开发环境搭建(win10)
    50 years of Computer Architecture: From the Mainframe CPU to the Domain-Specific TPU and the Open RISC-V Instruction Set
    2018.11.26-11.30工作总结
    《步步惊“芯”——软核处理器内部设计分析》前两章读书笔记
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4653988.html
Copyright © 2011-2022 走看看