JavaAPI之注释类型Target

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

位于java.lang.annotation包下

声明:

@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE)
public @interface Target

描述:

指示注释类型所适用的程序元素的种类。如果注释类型声明中不存在 Target 元注释,则声明的类型可以用在任一程序元素上。如果存在这样的元注释,则编译器强制实施指定的使用限制。 例如,此元注释指示该声明类型是其自身,即元注释类型。它只能用在注释类型声明上:

    @Target(ElementType.ANNOTATION_TYPE)
    public @interface MetaAnnotationType {
        ... 
    }
 

此元注释指示该声明类型只可作为复杂注释类型声明中的成员类型使用。它不能直接用于注释:

    @Target({}) 
    public @interface MemberType {
        ...
    }
 

这是一个编译时错误,它表明一个 ElementType 常量在 Target 注释中出现了不只一次。例如,以下元注释是非法的:

    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
    public @interface Bogus {
        ...
    }

元素信息

public abstract ElementType[] value

英文原描述:

Indicates the kinds of program element to which an annotation type is applicable. If a Target meta-annotation is not present on an annotation type declaration, the declared type may be used on any program element. If such a meta-annotation is present, the compiler will enforce the specified usage restriction. For example, this meta-annotation indicates that the declared type is itself a meta-annotation type. It can only be used on annotation type declarations:

    @Target(ElementType.ANNOTATION_TYPE)
    public @interface MetaAnnotationType {
        ...
    }
 

This meta-annotation indicates that the declared type is intended solely for use as a member type in complex annotation type declarations. It cannot be used to annotate anything directly:

    @Target({})
    public @interface MemberType {
        ...
    }
 

It is a compile-time error for a single ElementType constant to appear more than once in a Target annotation. For example, the following meta-annotation is illegal:

    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
    public @interface Bogus {
        ...
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的元数据(Metadata)是描述程序代码的数据,它们提供了有关代码的额外信息,例如类、方法、字段等的注释、版本信息、作者信息等。Java SE 5中引入了注解(Annotation),这是一种可以用来声明元数据的语法。 注解本身并不会影响程序的执行,但是它们可以被工具或框架用来生成代码、配置文件、文档等。Java中的元数据分为三类:注释、注解和反射。 1. 注释 注释是一种文本,它可以放在Java源代码中的任何地方。注释可以用来描述类、接口、方法、变量等的用途、功能、注意事项等。注释有三种形式:单行注释、多行注释和文档注释。 单行注释以“//”开头,可以跟在任何语句后面,直到该行结束。例如: ``` int a = 10; // 定义变量a并赋值为10 ``` 多行注释以“/*”开头,“*/”结尾,可以跨越多行。多行注释通常用于注释代码块。例如: ``` /* 这是一个多行注释 它跨越了多行 */ ``` 文档注释以“/**”开头,“*/”结尾,可以用来生成API文档。文档注释中可以使用特殊的标记(tag)来描述类、方法、变量等的功能、参数、返回值等。例如: ``` /** * 这是一个类的文档注释 */ public class MyClass { /** * 这是一个方法的文档注释 * * @param a 参数a的说明 * @param b 参数b的说明 * @return 返回值的说明 */ public int myMethod(int a, int b) { return a + b; } } ``` 2. 注解 注解是一种特殊的Java语法,它以“@”符号开头,可以放在类、接口、方法、变量等的定义前面。注解可以用来描述类、方法、变量等的功能、用途、配置信息等。注解可以有参数和默认值,参数可以是基本类型、字符串、枚举、注解等。 Java中有一些内置的注解,例如: - @Override:表示该方法是覆盖了父类中的方法。 - @Deprecated:表示该类、方法、变量已经过时,不建议使用。 - @SuppressWarnings:表示抑制编译器的警告信息。 我们也可以自定义注解,例如: ``` @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value() default "default value"; } ``` 以上代码定义了一个名为MyAnnotation的注解,它有一个名为value的参数,默认值为"default value"。 3. 反射 反射是Java中一个强大的机制,可以在运行时检查、获取、修改类、方法、变量等的信息。反射可以用来实现动态代理、依赖注入、插件化等功能。 反射中经常使用的类有Class、Method、Field、Constructor等。 例如,我们可以使用以下代码获取一个类的所有方法: ``` Class clazz = MyClass.class; Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { System.out.println(method.getName()); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值