- 浏览: 597963 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
月光杯:
问题解决了吗?
Exceptions in HDFS -
iostreamin:
神,好厉害,这是我找到的唯一可以ac的Java代码,厉害。
[leetcode] word ladder II -
standalone:
One answer I agree with:引用Whene ...
How many string objects are created? -
DiaoCow:
不错!,一开始对这些确实容易犯迷糊
erlang中的冒号 分号 和 句号 -
standalone:
Exception in thread "main& ...
one java interview question
从oracle的官方文档来学习annotations:
地址在: http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html
总结如下:
Annotations我理解就是程序的一些元数据,没有它程序也是ok的。它的主要用途体现在以下几个方面:
1. 替代以前简单的注释;
比如以前的注释可能是:
class My Class {}
用Annotations可以写成:
再复杂一点的注释:
用Annotations:
其中,@interface是定义一个annotation的关键字,看上去像接口,其实不是。
如果要让@ClassPreamble的信息出现在javadoc生成的文档中,你必须给刚才定义的@ClassPreamble加上@Documented这个保留的annotation。
如下所示:
2. 编译器保留的注解, 例如@Deprecated, @Override, 和 @SuppressWarnings.
@Deprecated意思是这个方法过时了,你最好不要再用了,你用我就给你来个warning;
@Override是提示你这个方法是覆盖了父类的一个方法;
@SuppressWarnings是告诉编译器这个方法可能会抛出warning,但你不要告诉我啦。
3. 在runtime用这些注解信息;
一般的用法在runtime的时候JVM不会load这些annotation信息,如果要让runtime知道,就要使用java.lang.annotation.RetentionPolicy了,这是个enum,有三个可选值,
分别是
具体看下面这个例子:
先定义一个annotation:
@Retention(RetentionPolicy.RUNTIME) 就是上面所说的在runtime时候我也要知道注解的信息。
@Target(ElementType.FIELD) 是指这个注解可以加在Field前面(所以你加到方法签名会编译不过的),java.lang.annotation.ElementType也是一个enum,其他可选择的值包括如下,不翻译了。
CONSTRUCTOR
Constructor declaration
FIELD
Field declaration (includes enum constants)
LOCAL_VARIABLE
Local variable declaration
METHOD
Method declaration
PACKAGE
Package declaration
PARAMETER
Parameter declaration
TYPE
Class, interface (including annotation type), or enum declaration
定义了这个注解,看怎么在runtime时候访问它:
注意在变量str前面使用了定义的注解,在main方法里通过反射获取到了注解中doTestTarget对应的值,所以输出是
Hello World !
地址在: http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html
总结如下:
Annotations我理解就是程序的一些元数据,没有它程序也是ok的。它的主要用途体现在以下几个方面:
1. 替代以前简单的注释;
比如以前的注释可能是:
/* author: Benjamin Franklin date: 3/27/2010 */
class My Class {}
用Annotations可以写成:
@Author( name = "Benjamin Franklin", date = "3/27/2010" ) class MyClass() { }
再复杂一点的注释:
public class Generation3List extends Generation2List { // Author: John Doe // Date: 3/17/2002 // Current revision: 6 // Last modified: 4/12/2004 // By: Jane Doe // Reviewers: Alice, Bill, Cindy // class code goes here }
用Annotations:
@interface ClassPreamble { String author(); String date(); int currentRevision() default 1; String lastModified() default "N/A"; String lastModifiedBy() default "N/A"; // Note use of array String[] reviewers(); } @ClassPreamble ( author = "John Doe", date = "3/17/2002", currentRevision = 6, lastModified = "4/12/2004", lastModifiedBy = "Jane Doe", // Note array notation reviewers = {"Alice", "Bob", "Cindy"} ) public class Generation3List extends Generation2List { // class code goes here }
其中,@interface是定义一个annotation的关键字,看上去像接口,其实不是。
如果要让@ClassPreamble的信息出现在javadoc生成的文档中,你必须给刚才定义的@ClassPreamble加上@Documented这个保留的annotation。
如下所示:
// import this to use @Documented import java.lang.annotation.*; @Documented @interface ClassPreamble { // Annotation element definitions }
2. 编译器保留的注解, 例如@Deprecated, @Override, 和 @SuppressWarnings.
@Deprecated意思是这个方法过时了,你最好不要再用了,你用我就给你来个warning;
@Override是提示你这个方法是覆盖了父类的一个方法;
@SuppressWarnings是告诉编译器这个方法可能会抛出warning,但你不要告诉我啦。
3. 在runtime用这些注解信息;
一般的用法在runtime的时候JVM不会load这些annotation信息,如果要让runtime知道,就要使用java.lang.annotation.RetentionPolicy了,这是个enum,有三个可选值,
分别是
- CLASS:annotation会被记录在class文件里但不会被jvm load。
- RUNTIME:记录在class文件里也会被jvm load,可以通过反射读取。
- SOURCE:会被compiler丢弃。
具体看下面这个例子:
先定义一个annotation:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Test_Target { public String doTestTarget(); }
@Retention(RetentionPolicy.RUNTIME) 就是上面所说的在runtime时候我也要知道注解的信息。
@Target(ElementType.FIELD) 是指这个注解可以加在Field前面(所以你加到方法签名会编译不过的),java.lang.annotation.ElementType也是一个enum,其他可选择的值包括如下,不翻译了。
CONSTRUCTOR
Constructor declaration
FIELD
Field declaration (includes enum constants)
LOCAL_VARIABLE
Local variable declaration
METHOD
Method declaration
PACKAGE
Package declaration
PARAMETER
Parameter declaration
TYPE
Class, interface (including annotation type), or enum declaration
定义了这个注解,看怎么在runtime时候访问它:
import java.lang.reflect.Field; public class TestAnnotations { @Test_Target(doTestTarget="Hello World !") private String str; public static void main(String[] args) throws Exception { // We need to use getDeclaredField here since the field is private. Field field = TestAnnotations.class.getDeclaredField("str"); Test_Target ann = field.getAnnotation(Test_Target.class); if (ann != null) { System.out.println(ann.doTestTarget()); } } }
注意在变量str前面使用了定义的注解,在main方法里通过反射获取到了注解中doTestTarget对应的值,所以输出是
Hello World !
发表评论
-
ssl 与 java 实例
2014-01-27 10:10 764http://www.iteye.com/topic/1125 ... -
Java NIO
2014-01-10 21:28 710看了这个java nio的教程,明白了什么是Selector. ... -
再谈Java的wait(), sleep(), notify()和notifyAll()
2013-07-25 10:59 1878一段时间不用java,这些概念就全混淆了,有必要彻底澄清一下, ... -
Why singleton is anti-pattern?
2013-07-03 10:12 880OO Test Other reasons? -
How to generate the serialVersionUID when you implement Serializable interface,j
2013-07-01 10:52 927http://docs.oracle.com/javase/6 ... -
Java Override的两个问题
2013-06-01 11:40 9351: 如果子类中的方法的参数是父类的方法的子类型,那么算不算o ... -
Java常用类API统计
2013-06-01 11:35 0String charAt(int) compareTo( ... -
How many string objects are created?
2013-06-01 10:18 1320This is a very common java inte ... -
使用Java的DelayQueue容易碰到的一个坑
2013-05-27 17:32 6684今天不忙,学习一下java.util.concurrent.D ... -
[leetcode] Balanced Binary Tree
2013-04-28 14:08 1574Check if a binary tree is balan ... -
[leetcode] find median of two sorted arrays
2013-04-26 10:55 1443http://leetcode.com/onlinejudge ... -
[leetcode] word ladder
2013-04-25 15:05 2268Q: Given two words (start and ... -
[leetcode] word ladder II
2013-04-15 07:35 11663http://leetcode.com/onlinejudge ... -
[leetcode] Count and Say
2013-04-12 14:05 2249http://leetcode.com/onlinejudge ... -
Date/Time处理函数总结 [To Do]
2013-04-12 10:46 654几种我所用到的用来处理日期,时间的函数总结。 Perl 1 ... -
[leetcode] Palindrome Partition
2013-04-12 10:25 1315http://leetcode.com/onlinejudge ... -
[leetcode] Palindrome Partitioning II
2013-04-11 16:45 1506http://leetcode.com/onlinejudge ... -
Profiling your Java code using Spring
2013-03-05 15:02 669Quite good article!!! http://w ... -
Java的Generics的几点限制
2012-12-28 15:00 4718参见 http://docs.oracle.com/ ... -
Overriding Method Using Parameter That is a Subclass?
2012-12-27 22:14 883参见 http://www.coderanch.com/t/3 ...
相关推荐
个人收集的相关Java Annotation的资源资料内容。
JDK5.0 - Java Annotation 介绍 (ppt格式)
JAVA Annotation学习示例 的一个简单示例 有源码 也有相关博客说明
java annotation 手册.doc,java annotation 手册.doc,java annotation 手册.doc
Java Annotation注解技术
Java Annotation的讲解和例子~~~
本文针对java初学者或者annotation初次使用者全面地说明了annotation的使用方法、定义方式、分类。初学者可以通过以上的说明制作简单的annotation程序,但是对于一些高级的annotation应用(例如使用自定义annotation...
Java Annotation详解 Java Annotation详解 Java Annotation详解
struts 拦截器,扩展,以及java annotation 这个是我学习所用,放在此处,作为中转。
JavaAnnotation必须掌握的特性Java开发Java经验技巧共6页.pdf.zip
java annotation手册 关于annotation是什么东西,百度一下会有很多关于它的描述,在这里就不用再copy了,我们还是以一个示例开始吧,在开始示例之前,我们先来一个小故事。
Java Annotation入门,本文初步告诉你怎么使用java anotatin
NULL 博文链接:https://xing-kenny.iteye.com/blog/1338865
如果你想知道java annotation是什么?你可以看看
java annotation 从JDK5.0后提供的功能。里面包含的实例,我是为了学习这个,所以放到这个上面作为中转。从别人博客里面摘取,博客地址里面附有。
javaAnnotation.pdf
Java Annotation学习整理
JavaAnnotation实例.docx