译 Programming with typesafe enums and annotations in Java 5
2014-12-05 10:28
337 查看
原文为:
Programming with typesafe enums and annotations in Java 5
Annotations——Java注解
你可能会遇到这种需求:通过与元数据(描述其他数据的数据)关联来注解你Java应用程序。一直以来,Java通过transient关键字提供了一种即时注解(ad hoc annotation)机制,这允许你标识一个成员变量在序列化过程中应该被忽略。但是,直到java5才正式地引入了一种标准方式去注解程序。
Java5平台提供了下面四种注解机制:
1. 声明注解类型:@interface;
2. 元注解类型:可以识别应用于注解类型的程序元素和注解的生命周期(注解类型的一个实例)等;
3.通过Java反射API的一种扩展方式来支持注解处理,你可以用来发现一个程序的运行时注解,并引入一个泛型Annotation处理工具(APT);
4. 标准的注解类型;
我将通过下面的实例解释如何运用这些方式并阐明注解的难点。
Annotations
You’veprobably encountered the need to annotate elements of your Java applicationsbyassociating metadata
(data that describes other data) with them.Java has always provided an ad hoc annotation mechanism via the transientreserved word, which lets you annotate fields that
are to be excluded duringserialization. But it didn’t offer a standard way to annotate program elementsuntil Java 5.
Java5’s general annotation mechanism consists of four components:
1.An @interface mechanism for declaring annotation types.
2. Meta-annotation types, which you can use to identify the applicationelements to which an annotation type applies; to identify the lifetime of anannotation (an instance of an annotation type); and more.
3. Support for annotation processing via an extension to the Java ReflectionAPI, which you can use to discover a program’s runtime annotations, and theintroduction of a generalized tool for processing annotations.
4. Standard annotation types.
I’ll explain how to use these componentsand point out some of the challenges of annotations in the examples thatfollow.
持续更新中。。。
Java 101: The next generation: The essential Java language features tour, Part 2
Programming with typesafe enums and annotations in Java 5Annotations——Java注解
你可能会遇到这种需求:通过与元数据(描述其他数据的数据)关联来注解你Java应用程序。一直以来,Java通过transient关键字提供了一种即时注解(ad hoc annotation)机制,这允许你标识一个成员变量在序列化过程中应该被忽略。但是,直到java5才正式地引入了一种标准方式去注解程序。
Java5平台提供了下面四种注解机制:
1. 声明注解类型:@interface;
2. 元注解类型:可以识别应用于注解类型的程序元素和注解的生命周期(注解类型的一个实例)等;
3.通过Java反射API的一种扩展方式来支持注解处理,你可以用来发现一个程序的运行时注解,并引入一个泛型Annotation处理工具(APT);
4. 标准的注解类型;
我将通过下面的实例解释如何运用这些方式并阐明注解的难点。
Annotations
You’veprobably encountered the need to annotate elements of your Java applicationsbyassociating metadata
(data that describes other data) with them.Java has always provided an ad hoc annotation mechanism via the transientreserved word, which lets you annotate fields that
are to be excluded duringserialization. But it didn’t offer a standard way to annotate program elementsuntil Java 5.
Java5’s general annotation mechanism consists of four components:
1.An @interface mechanism for declaring annotation types.
2. Meta-annotation types, which you can use to identify the applicationelements to which an annotation type applies; to identify the lifetime of anannotation (an instance of an annotation type); and more.
3. Support for annotation processing via an extension to the Java ReflectionAPI, which you can use to discover a program’s runtime annotations, and theintroduction of a generalized tool for processing annotations.
4. Standard annotation types.
I’ll explain how to use these componentsand point out some of the challenges of annotations in the examples thatfollow.
持续更新中。。。
相关文章推荐
- Type Annotations in Java 8: Tools and Opportunities
- Java Tip 27: Typesafe constants in C++ and Java
- 说Hand-over-hand lock: 通读Concurrent Programming in Java: Design Principles and Patterns (1)
- Distributed Java Programming with RMI and CORBA
- The difference between scheduleAtFixedRate and scheduleWithFixedDelay in JAVA
- JDOM Example : Reading and Parsing XML with SAX parser in Java
- 啃"创建线程"一章的疑难解析: 通读Concurrent Programming in Java: Design Principles and Patterns (2)
- Java is safe and secure in Oracle's hands
- Working with hashCode and equals methods in java
- how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language
- How to use Comparator and Comparable in Java? With example
- Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
- Python Programming with the Java(TM) Class Libraries: A Tutorial for Building Web and Enterprise App
- The Essence of Object-Oriented Programming with Java and UML
- Polymorphism in Perl comparing with JAVA and C++
- Typesafe DSLs in Java: Part 1 — Typesafe Bytecode
- Concurrent and Real-Time Programming in Java
- Programming Cameras and Pan-Tilts: with DirectX and Java
- Difference between RegularEnumSet and JumboEnumSet in Java
- Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java