- 自定义注解
- 1、注解的概述
- 2、自定义注解
- 3、自定义注解的基本使用
- 4、自定注解实现测试框架
从JDK5开始,Java增加对元数据的支持,也就是注解,注解与注释是有一定区别的,可以把注解理解为代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理。通过注解开发人员可以在不改变原有代码和逻辑的情况下在源代码中嵌入补充信息。
作用的分类
- 编写文档: 通过代码里表示的注解生成文档 【生成doc文档】
- 命令行输入:javadoc 要生成文档的.java文件
- 代码分析: 通过代码里的标识的注解对代码进行分析 【使用反射】
- 编译检查: 通过代码里的标识的注解让编译器能够实现基本的编译检查 【@Override】
JDK中预定义的一些注解
- @Override :检测被标注的方法是否是继承自父类。
- @Deprecated :该注解标注的内容表示已过时,但仍然可以使用只是不推荐。
- @SuppressWarnings(“all”) :压制所有警告信息。
格式
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited //元注解
public @interface Text {
}
- 注解的本质就是一个接口,接口默认继承Annotation接口 【可通过反编译验证 javap .class文件】
public interface Text extends java.lang.annotation.Annotation{
}
- 注解中的属性就是接口中可以定义的方法
【要求】属性的返回值类型必须为一下类型
- 基本数据类型
- String
- 枚举
- 注解
- 以上类型的数组类型
元注解
- @Target({ElementType.TYPE,ElementType.METHOD}) 注解的使用位置
- @Retention(RetentionPolicy.RUNTIME) 保留到什么时候
- @Documented 文档
- @Inherited 子类是否能够继承
- 注解
package com.qiumin.annotation;
import java.lang.annotation.*;
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Text {
String className();
String method();
}
- 解析注解
package com.qiumin.Test;
import com.qiumin.annotation.Text;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Text(className = "com.qiumin.demo.Demo2",method = "show") //Demo类的show方法
public class TestAnnot {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Class textClass = TestAnnot.class;
Text annotation = textClass.getAnnotation(Text.class);
String className = annotation.className();
String method = annotation.method();
Class> aClass = Class.forName(className);
Method method1 = aClass.getMethod(method);
Object obj = aClass.newInstance();
method1.invoke(obj);
}
}
4、自定注解实现测试框架
测试框架
- 注解
package com.qiumin.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Check {
}
- 待测试的方法
package com.qiumin.Test;
import com.qiumin.annotation.Check;
public class Count {
@Check
public void addNum(int a,int b){
System.out.println(a+b);
}
@Check
public void decre(int a,int b){
System.out.println(a-b);
}
@Check
public void cheng(int a,int b){
System.out.println(a*b);
}
@Check
public void chu(int a,int b){
System.out.println(a/b/0);
}
}
- 测试
package com.qiumin.Test;
import com.qiumin.annotation.Check;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
public class TestCheck {
public static void main(String[] args) throws IOException {
Count count = new Count();
int number=0;
BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));
Class countClass = Count.class;
Method[] methods = countClass.getMethods();
for (Method method:methods){
if(method.isAnnotationPresent(Check.class)){
try {
method.invoke(count,10,5);
} catch (Exception e) {
number++;
bw.write(method.getName()+"出现异常了");
bw.newLine();
bw.write("异常名称:"+e.getClass().getSimpleName());
bw.newLine();
bw.write("异常原因:"+e.getCause().getMessage());
bw.newLine();
bw.write("--------------------------------------");
bw.newLine();
}
}
}
bw.write("出现异常总次数为:"+number+"次");
bw.flush();
bw.close();
}
}
qiumin



