java.lang.annotation.ElementType
程序元素类型。此枚举类型的常量提供了Java程序中声明的元素的简单分类。这些常量与Target元注释类型一起使用,以指定使用注释类型的合法位置。
有以下常量:
有人能解释一下它们中的每一个(在实际代码中注释它们的位置)吗?
发布于 2010-08-23 18:15:21
本报告概述了以下主要内容:
@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是另一个注释的注释,如下所示:
@CustomAnnotationTypeAnnotation
public @interface SomeAnnotation {
..
}包在包中的package-info.java文件中定义,如下所示:
@CustomPackageLevelAnnotation
package com.some.package;
import com.some.package.annotation.PackageLevelAnnotation;发布于 2010-08-23 18:09:54
让我们假设指定ElementType的注释称为YourAnnotation
@SuppressWarnings注释。
公共无效someMethod() { @YourAnnotation a= 0;}package-info.java中使用。
@YourAnnotation org.yourcompany.somepackage;您可以为给定的注释指定多个ElementType。例如:
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})发布于 2010-08-23 18:11:22
型
注释:
@Target({ElementType.TYPE}) // This annotation can only be applied to
public @interface Tweezable { // class, interface, or enum declarations.
}还有一个例子用法:
@Tweezable
public class Hair {
...
}https://stackoverflow.com/questions/3550292
复制相似问题