java Annotation原理

本文深入探讨了Java SE5引入的注解机制,阐述了注解如何作为核心语言变更,用于存储额外的程序描述信息,并通过编译时验证。文章还介绍了注解在生成描述文件、新类定义和减轻模板代码负担方面的应用,以及自定义注解的语法和使用方式。重点讲述了内置的@Override、@Deprecated和@SuppressWarnings注解,强调了注解在构建处理工具时的灵活性和优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    Annotations are one of the fundamental language changes introduced in Java SE5. They provide information that you need to fully describe your program, but that cannot be expressed in Java. Thus, annotations allow you to store extra information about your program in a format that is tested and verified by the compiler

    Annotations can be used to generate descriptor files or even new class definitions and help ease the burden of writing "boilerplate" code(样板代码). Using annotations, you can keep this metadata in the Java source code, and have the advantage of cleaner looking code, compile-time type checking and the annotation API to help build processing tools for your annotations. Although a few types of metadata come predefined in Java SE5, in general the kind of annotations you add and what you do with them are entirely up to you.

      The syntax of annotations is reasonably simple and consists mainly of the addition of the @ symbol to the language. Java SE5 contains three generalpurpose built-in annotations, defined in java.lang:

     @Override, to indicate that a method definition is intended to override a method in the base class. This generates a compiler error if you accidentally misspell the method name or give an improper signature.

    @Deprecated, to produce a compiler warning if this element is used.

    @SuppressWarnings, to turn off inappropriate compiler warnings. This annotation is allowed but not supported in earlier releases of Java SE5 (it was ignored).

Syntactically, annotations are used in much the same way as modifiers.

Most of the time, you will be defining your own annotations and writing your own processors to deal with them.

Apart from the @ symbol, the definition of @Test is much like that of an empty interface.

(注解相当于一个空的接口,除了系统自带的注解之外,可以自定义注解)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值