1、Java中的注解
注解是Java 1.5引入的,目前已被广泛应用于各种Java框架,如Hibernate,Jersey,Spring。注解相当于是一种嵌入在程序中的元数据,可以使用注解解析工具或编译器对其进行解析,也可以指定注解在编译期或运行期有效。
http://www.importnew.com/10294.html这篇文章中对注解描述比较深入
注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。
注解的作用:
1、编写文档:通过代码里标识的元数据生成文档【生成文档doc文档】
2、代码分析:通过代码里标识的元数据对代码进行分析【使用反射】
3、编译检查:通过代码里标识的元数据让编译器能够实现基本的编译检查【Override】
2、Java内置三种最基本的注解(真正的理解@override,@Deprecated @SuppressWarnings的作用)
2.1、@Override
它的作用是对覆盖超类中方法的方法进行标记,如果被标记的类并没有实际覆盖超类,则编译器会发出错误警告。
/**
* Annotation type used to mark methods that override a method declaration in a
* superclass. Compilers produce an error if a method annotated with @Override
* does not actually override a method in a superclass.
*
* @since 1.5
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
示例:创建一个Fruit类让Test继承Fruit类,我们在方法上用@Override,它保证父类一定包含这样一个方法,否则编译就会出错,这样 就避免一些低级的错误,比如把们把info()方法写成inf()就会提示这样的错误,这样的错误如果不解决,可能在后期难以维护的排错带来巨大的障碍。
public class Test extends Fruit{
@Override
public void info() {
super.info();
}
}
class Fruit{
public void info(){
}
}
2.2、@Deprecated 表示当前的方法已经过时的,已经不建议使用。
/**
* Annotation type used to mark program elements that should no longer be used
* by programmers. Compilers produce a warning if a deprecated program element
* is used.
*
* @since 1.5
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Deprecated {
}
示例代码:
2.3、@SuppressWarnings 抑制编译器警告信息。
其参数有:
deprecation,使用了过时的类或方法时的警告
unchecked,执行了未检查的转换时的警告
fallthrough,当 Switch 程序块直接通往下一种情况而没有 Break 时的警告
path,在类路径、源文件路径等中有不存在的路径时的警告
serial,当在可序列化的类上缺少serialVersionUID 定义时的警告
finally ,任何 finally 子句不能正常完成时的警告
all,关于以上所有情况的警告
/**
* Annotation type used to indicate that the compiler should not issue the
* specified warnings for the marked program element. Warnings are not only
* suppressed for the annotated element but also for all program elements
* contained in that element.
* <p>
* It is recommended that programmers always use this annotation on the most
* deeply nested element where it is actually needed.
*
* @since 1.5
*/
@Target( { ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
ElementType.PARAMETER, ElementType.CONSTRUCTOR,
ElementType.LOCAL_VARIABLE })
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
/**
* The list of warnings a compiler should not issue.
*/
public String[] value();
}
从上面三个可以看出,这种注解的主要作用是检查我们代码,帮助的们减少一些不必要的错误,如果有错误,并且在编译时期提示出来。
3、Java中的元注解
元注解的作用就是负责注解其他注解
3.1、@Retention
表示需要在什么级别保存该注释信息,用于描述注解的生命周期,即注解在什么范围内有效(retention本身就是保留的意思)
RetentionPoicy有:
1.SOURCE:在源文件中有效
2.CLASS:在class文件中有效
3.RUNTIME:在运行时有效
源代码:
package java.lang.annotation;
/**
* Defines an enumeration for annotation retention policies. Used in conjunction
* with the {@link Retention} annotation to specify an annotation's time-to-live
* in the overall development life cycle.
*
* @since 1.5
*/
public enum RetentionPolicy {
/**
* Annotation is only available in the source code.
*/
SOURCE,
/**
* Annotation is available in the source code and in the class file, but not
* at runtime. This is the default policy.
*/
CLASS,
/**
* Annotation is available in the source code, the class file and is
* available at runtime.
*/
RUNTIME
}
3.2、@Target
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.ANNOTATION_TYPE:用于描述注解类型
8.TYPE:用于描述类、接口(包括注解类型) 或enum声明
源代码:
/**
* Defines an enumeration for Java program elements. It is used in conjunction
* with the {@link Target} meta-annotation to restrict the use of an annotation
* to certain program elements.
*
* @since 1.5
*/
public enum ElementType {
/**
* Class, interface or enum declaration.
*/
TYPE,
/**
* Field declaration.
*/
FIELD,
/**
* Method declaration.
*/
METHOD,
/**
* Parameter declaration.
*/
PARAMETER,
/**
* Constructor declaration.
*/
CONSTRUCTOR,
/**
* Local variable declaration.
*/
LOCAL_VARIABLE,
/**
* Annotation type declaration.
*/
ANNOTATION_TYPE,
/**
* Package declaration.
*/
PACKAGE
}
3.3、@Documented
通过代码里标识的元数据生成文档
3.4、@Inherited
@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
示例代码:
package com.example.test1;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyInherited {
}
调用:
package com.example.test1;
/**
* 该类没有直接标@MyInherited,而其父类有@MyInherited
* @author yu
*
*/
public class InheritableDemo extends Base{
public static void main(String[] args) {
// 打印一下InheritableDemo是否具有@MyInherited
System.out.println(InheritableDemo.class.isAnnotationPresent(MyInherited.class));
}
}
@MyInherited
class Base{
}
运行结果:true
从运行结果上看它的子类也具有了annotation