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
What do Java annotation ElementType constants mean?
提问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 Target
meta-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.java
file in the package, like this:
包定义在包中的一个package-info.java
文件中,如下所示:
@CustomPackageLevelAnnotation
package com.some.package;
import com.some.package.annotation.PackageLevelAnnotation;
回答by Bozho
Let's say the annotation to which you specify the ElementType
is 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
@SuppressWarnings
annotation.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 ElementType
s 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 {
...
}