java的注释包括类注释,属性注释,方法注释
定义注释与定义一般的接口没什么区别,在interface关键字之前有"@"
以下三段代码是对类,属性,方法注释的定义
以下是应用上面所定义的三种注释的类MyClass.java
一下是测试类,可以提取注释中的信息
执行结果:
定义注释与定义一般的接口没什么区别,在interface关键字之前有"@"
以下三段代码是对类,属性,方法注释的定义
package annotation;
//修饰类的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.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ClassAnnotation {
String value();
}
package 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;
//修饰方法的Annotation类型
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodAnnotation {
String methodName();
String destination();
}
package 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;
//修饰属性的Annotation类型
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FieldAnnotation {
String value();
}
以下是应用上面所定义的三种注释的类MyClass.java
package annotation;
//@ClassAnnotation(value ="Annotation用于类")
@ClassAnnotation("Annotation用于类") //annotation只有一个value时可以省略
public class MyClass {
@FieldAnnotation("Annotation用于属性")
public String id ="009";
@MethodAnnotation(methodName = "method1",destination ="Annotation用于方法")
public String method1(){
return this.id;
}
@MethodAnnotation(methodName = "method2",destination ="Annotation用于方法")
public String method2(){
return null;
}
}
一下是测试类,可以提取注释中的信息
package annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
//在运行期间从.class文件中提取用annotation修饰的信息
public class TestAnnotation {
public static void main(String[] args) throws Exception {
String CLASS_NAME = "annotation.MyClass";
Class test = Class.forName(CLASS_NAME);
boolean flag = test.isAnnotationPresent(ClassAnnotation.class);// 判断是否采用ClassAnnotation
if (flag) {
ClassAnnotation classAnnotation = (ClassAnnotation) test
.getAnnotation(ClassAnnotation.class);
System.out.println("类的描述:" + classAnnotation.value());
System.out.println("-----------------");
}
// 把利用到@MethodAnnotation的全部方法保存到Set中去
Set<Method> set = new HashSet<Method>();
Method[] methods = test.getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].isAnnotationPresent(MethodAnnotation.class))// 判断每个方法是否采用MethodAnnotation
set.add(methods[i]);
}
for (Method m : set) {
MethodAnnotation methodAnnotation = m
.getAnnotation(MethodAnnotation.class);
System.out.print("方法:" + methodAnnotation.methodName());
System.out.println("目的:" + methodAnnotation.destination());
}
System.out.println("-----------------");
// 把利用到@FieldAnnotation的全部属性保存到Set中去
Set<Field> set2 = new HashSet<Field>();
Field[] fields = test.getFields();
for (int i = 0; i < fields.length; i++) {
if (fields[i].isAnnotationPresent(FieldAnnotation.class))// 判断是否采用FieldAnnotation
set2.add(fields[i]);
}
for (Field f : set2) {
FieldAnnotation fieldAnnotation = f
.getAnnotation(FieldAnnotation.class);
System.out.println("属性的描述:" + fieldAnnotation.value());
}
}
}
执行结果:
类的描述:Annotation用于类
-----------------
方法:method1目的:Annotation用于方法
方法:method2目的:Annotation用于方法
-----------------
属性的描述:Annotation用于属性