前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >注解 @Target 用法

注解 @Target 用法

作者头像
微风-- 轻许--
发布2020-03-18 17:55:23
发布2020-03-18 17:55:23
1.7K00
代码可运行
举报
文章被收录于专栏:java 微风java 微风
运行总次数:0
代码可运行

@Target:

   @Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。

作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)

 取值(ElementType)有

代码语言:javascript
代码运行次数:0
复制
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

1

ElementType.TYPE_PARAMETER(Type parameter declaration) 用来标注类型参数, 栗子如下:

代码语言:javascript
代码运行次数:0
复制
@Target(ElementType.TYPE_PARAMETER)

@Retention(RetentionPolicy.RUNTIME)

public @interface TypeParameterAnnotation {
     

}



// 如下是该注解的使用例子

public class TypeParameterClass<@TypeParameterAnnotation T> {

    public <@TypeParameterAnnotation U> T foo(T t) {

        return null;

    }   

}

ElementType.TYPE_USE(Use of a type) 能标注任何类型名称,包括上面这个(ElementType.TYPE_PARAMETER的),栗子如下:

代码语言:javascript
代码运行次数:0
复制
public class TestTypeUse {


    @Target(ElementType.TYPE_USE)

    @Retention(RetentionPolicy.RUNTIME)

    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<>();

        typeUseClass.foo("");

        List<@TypeUseAnnotation Comparable> list1 = new ArrayList<>();

        List<? extends Comparable> list2 = new ArrayList<@TypeUseAnnotation Comparable>();

        @TypeUseAnnotation String text = (@TypeUseAnnotation String)new Object();

        java.util. @TypeUseAnnotation Scanner console = new java.util.@TypeUseAnnotation Scanner(System.in);

    }

} 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档