您的位置:首页 > 编程语言 > Java开发

JavaSE第六十九讲:Java Annotation详解

2013-01-07 16:11 337 查看
1. Java annotation的工作方式

  从Javaf5.0版发布以来,5.0平台提供了一个正式的annotation的功能:允许开发者定义、使用自己的annotation类型。此功能由一个定义annotation类型的语法和一个描述annotation声明的语法,读取annotation的API,一个使用annotation修饰的class文件,一个annotation处理工具(apt)组成。

  annotation并不直接影响代码语义,但是它能够工作的方法被看做类似程序的工具或者类库,它会反过来对正在运行的程序语义有所影响。

annotation可以从源文件、class文件或者在运行时反射的多种方式被读取

package com.ahuier.annotation;

public class OverrideTest {

/*
* 使用@Override注解
* 通知Java表示被Override所修饰的方法必须要是重写父类型的方法。
* 这边如果没有写注解的话,假如程序员将toString()方法 写成 tostring()则编译时正常通过的,Java会认为其定义了tostring()方法
* 所以在输出的时候依然会输出Object类中toString()方法,但是这明显违背了程序员的逻辑业务需求了。如果加上注解,程序员犯相同的错误,
* 就会在编译的时候给你提示编译错误,这就是注解带来的一个好处。
*/
@Override
public String toString(){
return "This is OverrideTest";
}
public static void main(String[] args) {
OverrideTest test = new OverrideTest();
System.out.println(test);
}
}
编译执行结果:

This is OverrideTest

程序一:

package com.ahuier.annotation;

import java.util.Date;

public class DeprecatedTest {
public static void main(String[] args) {
/*
* 查看JDK Doc 文档中的Date类,这个类是用来获取当前系统时间的
*/
Date date = new Date();

/*输出:Mon Jan 07 20:57:20 CST 2013  这个时间格式不是我们想要的,调用toLocaleString()转换为本地时间
System.out.println(date);*/

//这边toLocaleString()被划一条横线,并且出现一个警告,表示这个方法是不建议被时候,将来有可能这种方法会被修改。
System.out.println(date.toLocaleString()); //输出:2013-1-7 20:58:47
}
}

编译执行结果:

2013-1-7 21:04:06

程序二:如下图所示



【说明】:注解可以用在类、成员变量、方法、方法参数、方法局部变量上都可以使用注解

2. Java注解(Annotation[注意这边不翻译成注释,注释是Comment]): 

a) Override注解表示子类要重写(override)父类的对应方法。 

b) Deprecated注解表示方法是不建议被使用的。 

c) SuppressWarnings注解表示抑制警告。

3. 限定Override父类方法@Override

java.lang.Override是个Marker annotation,用于标示的Annotation,Annotation名称本身即表示了要给工具程序的信息
[编译器在看到 @Override 后就知道将要进行重写其父类的方法,所以这个一般在需要修饰的代码前面注解]

查看JDK Doc文档中的Override类

java.lang

Annotation Type Override

  Indicates[标示] that a method declaration is intended to override a method declaration in
a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

[从这边可以看出在定义注解的时候也是跟定义类、接口、枚举是在一个层次上,注解有它的一种定义方法]

4.
标示方法为Deprecated @Deprecated

对编译程序说明某个方法已经不建议使用,即该方法是过时的。java.lang.Deprecated也是一个Marker annotation

Deprecated这个名称在告知编译程序,被 @Deprecated 标示的方法是一个不建议被使用的方法

查看JDK Doc文档中的
Deprecated 类

java.lang

Annotation Type Deprecated
  A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program
element is used or overridden in non-deprecated code.

5.
抑制编译程序警告 @SuppressWarnings

对编译程序说明某个方法中若有警告讯息,则加以抑制

java.lang

Annotation Type SuppressWarnings

  Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element). Note that the set of warnings suppressed in a given element is a superset of the warnings suppressed
in all containing elements. For example, if you annotate a class to suppress one warning and annotate a method to suppress another, both warnings will be suppressed in the method.

[指定名称的值就是其里面带的参数,如果在类上使用SuppressWarnings压制了A的警告,在方法上SuppressWarnings压制了B警告,那么这两个警告都会被压制]



图69-2

这个value数组,提供了警告的一些压制的信息

package com.ahuier.annotation;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
public class SuppressWarningsTest {

/*
* 程序前面 @SuppressWarnings("unchecked") 表示压制不检查的警告
* 参数 unchecked 是警告的信息值,警告有很多警告的信息值用数组形式存储
*/
@SuppressWarnings({"unchecked", "deprecation"})
public static void main(String[] args) {
Map map = new TreeMap();
map.put("hello", new Date());
System.out.println(map.get("hello"));

Date date = new Date();
System.out.println(date.toLocaleString());
}
}


编译执行结果
Mon Jan 07 21:42:29 CST
2013

2013-1-7 21:42:29

6. 自定义Annotation类型

定义Marker Annotation,也就是Annotation
名称本身即提供信息对于程序分析工具来说,主要是检查是否有MarkerAnnotation的出现,并作出对应的动作。

以上的程序都是使用JDK给我们提供的注解信息,现在去定义一个属于自己的注解

新建一个
Annotation

package com.ahuier.annotation;

public @interface AnnotationTest {

//注意不要忘记小括号
String value();
}

package com.ahuier.annotation;

/*
* @AnnotationTest(value = "hello") 与 @AnnotationTest("hello")都可以,前提是定义的注解里面必须是 value 值
*/
@AnnotationTest("hello")
public class AnnotationUsage {

@AnnotationTest("hello")
public void method(){
System.out.println("usage of annotation");
}
public static void main(String[] args) {
AnnotationUsage usage = new AnnotationUsage();
usage.method();
}
}

编译执行结果:

usage
of annotation

【说明】:自定义注解:当注解中的属性名为 value 时,在对其赋值时可以不指定属性的名称 而直接写上属性值即可;除了 value 以外的其他值都需要使用 name=value 这种赋值 方式,即明确指定给谁赋值。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: