简体   繁体   English

Java - 参数注释

[英]Java - Parameter annotations

Having trouble getting the parameter annotations of a method, below is a easy to test demonstration, any directions towards the error would be welcome : 在获取方法的参数注释时遇到问题,下面是一个易于测试的演示,欢迎任何指向错误的方向:

// Annotation
public @interface At {}

// Class
public class AnnoTest {

   public void myTest(@At String myVar1, String myVar2){}
}

// Test
public class App {

    public static void main(String[] args) {

        Class myClass = AnnoTest.class;

        Method method = myClass.getMethods()[0];
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();

        // Should output 1 instead of 0
        System.out.println(parameterAnnotations[0].length);
    }
}

You are not setting implicitly setting the Retention to Runtime, that way it defaults to @Retention (RetentionPolicy.CLASS) this says it is represent in the class file but not present in the VM. 您没有隐式设置Retention to Runtime,这样它默认为@Retention (RetentionPolicy.CLASS)这表示它表示在类文件中但不存在于VM中。 To make it work add this to your interface: @Retention (RetentionPolicy.RUNTIME) as class annotatio, then it works again ! 为了使其工作,将其添加到您的界面: @Retention (RetentionPolicy.RUNTIME)作为类注释,然后它再次工作! :D :d

While you are at it, you might want to set a specific @Target to only parameters and not methods/fields/classes etc. 在您使用它时,您可能希望将特定的@Target设置为仅参数而不是方法/字段/类等。

By default, annotations are recorded in the class file by the compiler but need not be retained by the VM at run time (RetentionPolicy.CLASS retention policy is being applied). 默认情况下,编译器会在类文件中记录注释,但运行时不需要VM保留注释(正在应用RetentionPolicy.CLASS保留策略)。

To change how long annotations are retained, you may use the Retention meta-annotation. 要更改注释的保留时间,可以使用“保留”元注释。

In your case, you want to make it available for reading reflectively so you need it must use RetentionPolicy.RUNTIME to record the annotation in the class file but stil be retained by VM at run time. 在您的情况下,您希望使其可供反射阅读,因此您需要它必须使用RetentionPolicy.RUNTIME在类文件中记录注释,但在运行时由VM保留。

@Retention(RetentionPolicy.RUNTIME)
public @interface At {}

I also suggest you indicate the program element to which the annotation type At is applicable. 我还建议您指出注释类型At适用的程序元素。

In your case, a parameter annotion should be 在您的情况下,参数注释应该是

 @Target(ElementType.PARAMETER)

this way the compiler will enforce the specified usage restriction. 这样编译器将强制执行指定的使用限制。

By default the declared type may be used on any program element: 默认情况下,声明的类型可以在任何程序元素上使用:

  • ANNOTATION_TYPE - Annotation type declaration ANNOTATION_TYPE - 注释类型声明
  • CONSTRUCTOR - Constructor declaration CONSTRUCTOR - 构造函数声明
  • FIELD - Field declaration (includes enum constants) FIELD - 字段声明(包括枚举常量)
  • LOCAL_VARIABLE - Local variable declaration LOCAL_VARIABLE - 局部变量声明
  • METHOD - Method declaration 方法 - 方法声明
  • PACKAGE - Package declaration 包裹 - 包裹声明
  • PARAMETER - Parameter declaration PARAMETER - 参数声明
  • TYPE - Class, interface (including annotation type), or enum declaration TYPE - 类,接口(包括注释类型)或枚举声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM