2阶——注解

1:注解从jdk1.5之后支持

2: jdk中预定义的一些注解

*@Override (检查被该注解标注的方法是否继承于父类)    @Deprecated(标注的内容表示已经过时)  @SuppressWarnings(压制警告))





3:自定义注解
本质相当于一个接口,接口能定义什么 注解就可以定义什么)

public @interface MyAnotation { //javac把源文件先编译成为class 文件 在通过javap命令反编译就会发现注解的 本质是 public interface Annotaticn extends Annotation{} 这就可以说接口中能定义什么 注解就可以定义什么
  属性的返回值类型 //javadoc可以编译成Document文档
   *基本数据类型
   *String
   *枚举 注解 以上数据的数组类型

例如:int show1();
}

 




4:元注解
用于描述注解的注解


//自定义注解
@Target(value = {ElementType.TYPE,ElementType.FIELD) //定义作用范围 Type是只能作用于类上  FIELD 成员变量上
@Retention(RetentionPolicy.RUNTIME)   // SOURCE CLASS RUNTIME
//@Documented
public @interface MyAnotation {  //本质是 public interface Annotaticn extends Annotation


}


 5注解替代配置文件(框架注解的简易实现原理)


package cn;

import java.lang.reflect.Method;

@Pro(className = "cn.refect.Person",methodName = "eat")  //注解替代了配置文件 相当于直接配置到了类上!
public class ReDemo {
    public static void main(String[] args) {
        //用注解代替配置文件

        try {
            Class<ReDemo> clazz = ReDemo.class;
            //获取一个注解对象
            Pro po = clazz.getAnnotation(Pro.class);
            String className = po.className();
            String methodName = po.methodName();

            System.out.println(className);
            System.out.println(methodName);

            //反射执行
            Object o = clazz.newInstance();
            Method method = clazz.getMethod(methodName);
            method.invoke(o);




        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


//自定义的注解
package cn;

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)
public @interface Pro {
    String className();
    String methodName();
}

 

 //利用注解来测试方法
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestDemo {

    public static void main(String[] args) {

        Demo4 demo4 = new Demo4();

        Class<Demo4> clazz = Demo4.class;

        Method[] methods = clazz.getMethods();
        int num = 0;

        for (int i = 0; i <methods.length ; i++) {

            if(methods[i].isAnnotationPresent(Demo2.class)){

                try {
                    methods[i].invoke(demo4);

                } catch (Exception e) {
                    num++;
                    System.out.println(e.getCause().getClass().getSimpleName());
                    System.out.println(e.getCause().getMessage());
                    System.out.println("-----------------------------------");
                }
            }
        }

        System.out.println("一共出现了"+num+"次异常");
    }


}


public class Demo4 {

@Demo2
public void getsum(){
String str = null;
str.toString();
System.out.println("getsum");
}
@Demo2
public void getsum2(){
int i = 0;

int a = 10 / i;
System.out.println("getsum2");
}

@Demo2
public void getsum3(){
System.out.println("3333");
}

public void hello(){
System.out.println("right");
}
}



import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface Demo2 { //注解本质就是一个接口接口怎么定义的 注解就怎么定义

// public String name(); //返回值 字符串类型
//public int age(); //返回值 int类型
// public Demo3 meiju(); // 返回值 枚举类型
// public String[] all(); //以上上中的数组类型

//直接简写如下
// String name(); //返回值 字符串类型
// int age(); //返回值 int类型
// Demo3 meiju(); // 返回值 枚举类型
// String[] all(); //以上上中的数组类型

}
 






















坚持
原文地址:https://www.cnblogs.com/gaoSJ/p/12877521.html