Java 注释 ElementType 常量是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3550292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:46:46  来源:igfitidea点击:

What do Java annotation ElementType constants mean?

javaannotations

提问by Action Hymanson

java.lang.annotation.ElementType:

java.lang.annotation.ElementType

A program element type. The constants of this enumerated type provide a simple classification of the declared elements in a Java program. These constants are used with the Targetmeta-annotation type to specify where it is legal to use an annotation type.

程序元素类型。这种枚举类型的常量提供了 Java 程序中声明元素的简单分类。这些常量与Target元注释类型一起使用,以指定在何处使用注释类型是合法的。

There are the following constants:

有以下常量:

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

Can someone explain what each of them are (where they'd be annotated in actual code)?

有人可以解释他们每个人是什么(他们会在实际代码中注释的地方)吗?

采纳答案by Javid Jamae

This summarizes the main ones:

这总结了主要的:

@CustomTypeAnnotation
public class MyAnnotatedClass {
  @CustomFieldAnnotation
  private String foo;

  @CustomConstructorAnnotation
  public MyAnnotatedClass() {
  }

  @CustomMethodAnnotation
  public String bar(@CustomParameterAnnotation String str) {
    @CustomLocalVariableAnnotation String asdf = "asdf";
    return asdf + str;
  }
}

ANNOTATION_TYPE is an annotation on another annotation, like this:

ANNOTATION_TYPE 是另一个注解上的注解,像这样:

@CustomAnnotationTypeAnnotation
public @interface SomeAnnotation {
  ..
}

Package is defined in a package-info.javafile in the package, like this:

包定义在包中的一个package-info.java文件中,如下所示:

@CustomPackageLevelAnnotation
package com.some.package;

import com.some.package.annotation.PackageLevelAnnotation;

For more info on PACKAGE annotations see hereand here.

有关 PACKAGE 注释的更多信息,请参见此处此处

回答by Bozho

Let's say the annotation to which you specify the ElementTypeis called YourAnnotation:

假设您指定的注释ElementType被称为YourAnnotation

  • ANNOTATION_TYPE - Annotation type declaration. Note:This goes on other annotations

    @YourAnnotation
    public @interface AnotherAnnotation {..}
    
  • CONSTRUCTOR - Constructor declaration

    public class SomeClass {
        @YourAnnotation
        public SomeClass() {..}
    }
    
  • FIELD - Field declaration (includes enum constants)

    @YourAnnotation
    private String someField;
    
  • LOCAL_VARIABLE - Local variable declaration. Note:This can't be read at runtime, so it is used only for compile-time things, like the @SuppressWarningsannotation.

    public void someMethod() {
        @YourAnnotation int a = 0;
    }
    
  • METHOD - Method declaration

    @YourAnnotation
    public void someMethod() {..}
    
  • PACKAGE - Package declaration. Note:This can be used only in package-info.java.

    @YourAnnotation
    package org.yourcompany.somepackage;
    
  • PARAMETER - Parameter declaration

    public void someMethod(@YourAnnotation param) {..}
    
  • TYPE - Class, interface (including annotation type), or enum declaration

    @YourAnnotation
    public class SomeClass {..}
    
  • ANNOTATION_TYPE - 注释类型声明。注意:这适用于其他注释

    @YourAnnotation
    public @interface AnotherAnnotation {..}
    
  • CONSTRUCTOR - 构造函数声明

    public class SomeClass {
        @YourAnnotation
        public SomeClass() {..}
    }
    
  • FIELD - 字段声明(包括枚举常量)

    @YourAnnotation
    private String someField;
    
  • LOCAL_VARIABLE - 局部变量声明。注意:这不能在运行时读取,所以它只用于编译时的东西,比如@SuppressWarnings注解。

    public void someMethod() {
        @YourAnnotation int a = 0;
    }
    
  • METHOD - 方法声明

    @YourAnnotation
    public void someMethod() {..}
    
  • PACKAGE - 包声明。注意:这只能在package-info.java.

    @YourAnnotation
    package org.yourcompany.somepackage;
    
  • PARAMETER - 参数声明

    public void someMethod(@YourAnnotation param) {..}
    
  • TYPE - 类、接口(包括注解类型)或枚举声明

    @YourAnnotation
    public class SomeClass {..}
    

You can specify multiple ElementTypes for a given annotation. E.g.:

您可以ElementType为给定的注释指定多个s。例如:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})

回答by Action Hymanson

TYPE:

类型

Annotation:

注解:

@Target({ElementType.TYPE})    // This annotation can only be applied to
public @interface Tweezable {  // class, interface, or enum declarations.
}

and an example usage:

和一个示例用法:

@Tweezable
public class Hair {
    ...
}