2、javadoc中的@author、@version、@param、@return、@deprecated、@hide、@throws、@exception、@see是标记,并不是注解。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
2、减少工作量:大量重复的工作可以通过注解来减少工作量。
|
@Target
|
表示该注解可以用于什么地方。可能的ElementType参数包括:
CONSTRUCTOR:构造器声明
FIELD:域声明(包括enum实例)
LOCAL_VARIABLE:局部变量声明
METHOD:方法声明
PACKAGE:包声明
PARAMETER:参数声明
TYPE:类、接口(包括注解类型)或enum声明
(没指定的话可以放到任意地方)
|
|
@Retention
|
表示需要在什么级别保存该注解信息。参数有:
SOURCE:注解将被编译器丢弃(如:@Override、@SuppressWarnings)
CLASS:注解在class文件中可用,但会在VM中丢弃(默认值;如:@Deprecated)
RUNTIME:VM将在运行期也保留注解,因此可以通过反射机制读取注解信息 |
|
@Documented
|
将此注解包含在Javadoc中(表示是否出现在API文档中)
|
|
@Inherited
|
允许子类继承父类中的注解
|
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}
元注解
public @interface 注解名{
定义体;
}
package com.fxp.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyFirstAnnotation {
String value() default "";
}
事先准备一个Person接口和Child实现类
package com.fxp.annotation.test;
public interface Person {
String name();
int age();
}
package com.fxp.annotation.test;
public class Child implements Person {
@Override
public String name() {
// TODO Auto-generated method stub
return null;
}
@Override
public int age() {
// TODO Auto-generated method stub
return 0;
}
}
定义一个注解类Description:
package com.fxp.annotation.test;
import java.lang.annotation.Documented;
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.METHOD,ElementType.TYPE})//作用域:在类和方法上可用
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
String value();
}
在Child类上使用该注解:
package com.fxp.annotation.test;
@Description("I am Child Class annotation")
public class Child implements Person {
@Override
@Description("I am method annotation")
public String name() {
// TODO Auto-generated method stub
return null;
}
@Override
public int age() {
// TODO Auto-generated method stub
return 0;
}
}
定义一个解析注解的类:
package com.fxp.annotation.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class ParseAnno {
public static void main(String[] args) {
try {
//1、使用类加载器加载类
Class c = Class.forName("com.fxp.annotation.test.Child");
//2、找到类上面的注解
boolean isExist = c.isAnnotationPresent(Description.class);
if(isExist){//判断注解是否存在
//3、拿到注解实例
Description d = (Description) c.getAnnotation(Description.class);
System.out.println(d.value());
}
//4、找到方法上的注解
Method[] ms = c.getMethods();
for(Method m : ms){
boolean isMExist = m.isAnnotationPresent(Description.class);
if(isMExist){
Description d = m.getAnnotation(Description.class);
System.out.println(d.value());
}
}
//另外一种解析方法
for(Method m : ms){
Annotation[] as = m.getAnnotations();
for(Annotation a : as){
if(a instanceof Description){
System.out.println(((Description) a).value());
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
解析的结果:
I am Child Class annotation
I am method annotation
I am method annotation
5万+



被折叠的 条评论
为什么被折叠?



