Open kagxin opened 4 years ago
package annoDemo1;
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) //保留到运行时
@interface Conf { //注解的签名
String className() default ""; //注解属性
String methodName() default "";
}
package annoDemo1;
public class Persion {
private String name;
private int age;
public Persion(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show() {
System.out.println("i am persion");
}
@Override
public String toString() {
return "Persion{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package annoDemo1;
public class Student extends Persion {
private int score;
public Student(String name, int age) {
super(name, age);
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public void show()
{
System.out.println("i am student");
}
@Override
public String toString() {
return "Student{" +
"score=" + score +
'}';
}
}
package annoDemo1;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/*
执行注解的className对应的类的methodName方法
* */
@Conf(className = "annoDemo1.Persion", methodName = "show")
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Main.class;
Conf conf = (Conf) cls.getAnnotation(Conf.class); // 获取注解
String className = conf.className(); // 通过注解的属性获取 className, methodName
String methodName = conf.methodName();
System.out.println(String.format("%s, %s", className, methodName));
Class aClass = Class.forName(className); // 获取Class对象
Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class, int.class); // 获取注解className属性值对应的类
Persion xiaoming = (Persion) declaredConstructor.newInstance("xiaoming", 12); //生成对象用于调用方法
Method declaredMethods = aClass.getDeclaredMethod(methodName); // 获取注解methodName属性值对应的方法
declaredMethods.invoke(xiaoming); // 执行方法
}
}
package test;
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 test;
public class Calc {
@Check
public static void add_t() {
System.out.println(1 + 2);
}
@Check
public static void div_t() {
System.out.println(1 / 0);
}
public static void test() {
System.out.println("hello");
}
}
package test;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
Calc c = new Calc();
Class cls = c.getClass(); // 获取Calc的Class的对象
Method[] declaredMethods = cls.getDeclaredMethods(); // 获取所有方法
int number = 0;
for (Method declaredMethod : declaredMethods) {
if (declaredMethod.isAnnotationPresent(Check.class)) { // 判断是否被Check注解
try {
declaredMethod.invoke(c); // 执行被Check注解的方法
} catch (Exception e) {
number++;
System.out.println(e.getCause().getMessage());
}
}
System.out.println("=========");
}
System.out.println(String.format("测试结束:共%s个错误", number));
}
}
定义
作用分类:
内置注解
@Override
它的作用是对覆盖超类中方法的方法进行标记,如果被标记的方法并没有实际覆盖超类中的方法,则编译器会发出错误警告。@Deprecated
它的作用是对不应该再使用的方法添加注解,当编程人员使用这些方法时,将会在编译时显示提示信息。@SuppressWarnings
压制警告信息的,@SuppressWarnings("all")压制所有警告信息。注解的本质,继承Annotation的接口
典型的注解
元注解
@Target
:描述注解能够作用的位置@Retention
:描述注解被保留的阶段@Retention
(RetentionPolicy.RUNTIME):当前被描述的注解,会保留到class字节码文件中,并被JVM读取到@Documented
:描述注解是否被抽取到api文档中@Inherited
:描述注解是否被子类继承注解的属性
例子1:解析注解中的属性值
anno.java
main.java
例子二:简单的测试例子
Check.java
Test.java