java元注解@Target注解用法

java元注解@Target注解用法

2023年6月22日发(作者:)

java元注解@Target注解⽤法@Target:   @Target说明了Annotation所修饰的对象范围:Annotation可被⽤于 packages、types(类、接⼝、枚举、Annotation类型)、类型成员(⽅法、构造⽅法、成员变量、枚举值)、⽅法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使⽤了target可更加明晰其修饰的⽬标。  作⽤:⽤于描述注解的使⽤范围(即:被描述的注解可以⽤在什么地⽅) 取值(ElementType)有public enum ElementType { /**⽤于描述类、接⼝(包括注解类型) 或enum声明 Class, interface (including annotation type), or enum declaration */ TYPE, /** ⽤于描述域 Field declaration (includes enum constants) */ FIELD, /**⽤于描述⽅法 Method declaration */ METHOD, /**⽤于描述参数 Formal parameter declaration */ PARAMETER, /**⽤于描述构造器 Constructor declaration */ CONSTRUCTOR, /**⽤于描述局部变量 Local variable declaration */ LOCAL_VARIABLE, /** Annotation type declaration */ ANNOTATION_TYPE, /**⽤于描述包 Package declaration */ PACKAGE, /** * ⽤来标注类型参数 Type parameter declaration * @since 1.8 */ TYPE_PARAMETER, /** *能标注任何类型名称 Use of a type * @since 1.8 */ TYPE_USE  _PARAMETER(Type parameter declaration) ⽤来标注类型参数, 栗⼦如下:@Target(_PARAMETER)@Retention(E)public @interface TypeParameterAnnotation {

}// 如下是该注解的使⽤例⼦public class TypeParameterClass<@TypeParameterAnnotation T> { public <@TypeParameterAnnotation U> T foo(T t) { return null; }

}  _USE(Use of a type) 能标注任何类型名称,包括上⾯这个(_PARAMETER的),栗⼦如下:public class TestTypeUse { @Target(_USE) @Retention(E) public @interface TypeUseAnnotation {

}

public static @TypeUseAnnotation class TypeUseClass<@TypeUseAnnotation T> extends @TypeUseAnnotation Object { public void foo(@TypeUseAnnotation T t) throws @TypeUseAnnotation Exception {

} }

// 如下注解的使⽤都是合法的 @SuppressWarnings({ "rawtypes", "unused", "resource" }) public static void main(String[] args) throws Exception { TypeUseClass<@TypeUseAnnotation String> typeUseClass = new @TypeUseAnnotation TypeUseClass<>(); (""); List<@TypeUseAnnotation Comparable> list1 = new ArrayList<>(); List list2 = new ArrayList<@TypeUseAnnotation Comparable>(); @TypeUseAnnotation String text = (@TypeUseAnnotation String)new Object(); . @TypeUseAnnotation Scanner console = new .@TypeUseAnnotation Scanner(); }}  

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1687442278a10291.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信