學(xué)習(xí)筆記之注解

一. 什么是注解

Annotation是JDK1.5開始引入的新技術(shù)

Annotation的作用:
  1. 不是程序本身,可以對程序作出解釋。這一點(diǎn)與注釋沒什么區(qū)別。注釋是給程序猿看的,注解是給機(jī)器看的。
  2. 可以被其他程序(比如編譯器等)讀取。注解信息處理流程,是注解和注釋的重要區(qū)別。如果沒有注解信息處理流程,則注解毫無意義。
Annotation的格式:
  1. 注解是以“@注釋名”在代碼中存在的,如:@Test
  2. 還可以添加一些參數(shù)信息。如:String value default "";

二. 內(nèi)置注解:

1. @Override
2. @Deprecated
3. @SuppressWarnings:
  • deprecation:使用了過時(shí)的方法或類的警告
  • unchecked:執(zhí)行了未檢查的轉(zhuǎn)換時(shí)的警告,如果使用集合時(shí)未指定泛型。
  • fallthrough:當(dāng)使用switch語句使用時(shí)發(fā)生case穿透
  • path:在類路徑、源文件路徑等中有不存在路徑的警告
  • serial:當(dāng)在序列化的類上確山serialVersionUID定義時(shí)的警告
  • finally:任何finally子句不能完成時(shí)的警告
  • all:以上所有情況
4. @SafeVarargs:JDK1.7后新增,用于壓制泛,堆內(nèi)存污染。
5. @FunctionalInterface:JDK1.8后增加,指定是函數(shù)式編程接口

三、元注解

1. @Retention:保留期

RetentionPolicy.SOURCE:源碼
RetentionPolicy.CLASS:class文件
RetentionPolicy.RUNTIME:運(yùn)行時(shí)

2. @Target:目標(biāo)

ElementType.ANNOTATION_TYPE:類型成員(方法、構(gòu)造方法、成員變量、枚舉值)
ElementType.CONSTRUCTOR:類型成員(方法、構(gòu)造方法、成員變量、枚舉值)
ElementType.FIELD:類型成員(方法、構(gòu)造方法、成員變量、枚舉值)
ElementType.LOCAL_VARIBALE:方法參數(shù)、本地變量
ElementType.METHOD:類型成員(方法、構(gòu)造方法、成員變量、枚舉值)
ElementType.PACKAGE:包
ElementType.PARAMETER:方法參數(shù)、本地變量
ElementType.TYPE:類、接口、枚舉、Annotation

3. @Documented
4. @Inherited

Inherited 是繼承的意思,但是它并不是說注解本身可以繼承,而是說如果一個(gè)超類被 @Inherited 注解過的注解進(jìn)行注解的話,那么如果它的子類沒有被任何注解應(yīng)用的話,那么這個(gè)子類就繼承了超類的注解。 說的比較抽象。代碼來解釋。
注解 Test 被 @Inherited 修飾,之后類 A 被 Test 注解,類 B 繼承 A,類 B 也擁有 Test 這個(gè)注解。

/* * @Inherited測試*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface Test {
    
}
@Test
public class A {
    
}
public class B extends A {
    
}
5. @Repeatable:JDK1.8新增元注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Persons {
    Person[] value();
}

@Repeatable(Persons.class)
public @interface Person {
    String role() default "";
}

/**
 * 測試多重角色,JDK1.8后新特性
 */
@Person(role = "coder")
@Person(role = "artist")
@Person(role = "singging")
public class SuperMan {

}

/**
 *  測試JDK1.8后的新增元注解
 */
public class RetentionTest {
    public static void main(String[] args) throws ClassNotFoundException {
        //獲取class對象,下獲取注解
        Class<?> clazz = Class.forName("com.dayDayUp.annotation.retentionAnno.SuperMan");
        System.out.println("=====================是否是注解========================");
        boolean flag1 = clazz.isAnnotationPresent(Person.class);//是否是注解
        boolean flag2 = clazz.isAnnotationPresent(Persons.class);
        System.out.println(flag1);
        System.out.println(flag2);
        System.out.println("=====================是否是注解========================");
        boolean c1 = clazz.isAnnotation();
        boolean c2 = Person.class.isAnnotation();
        System.out.println(c1);
        System.out.println(c2);
        System.out.println("=====================clazz.getAnnotation()========================");
        Person person = clazz.getAnnotation(Person.class);
        Persons persons = clazz.getAnnotation(Persons.class);
        //String role = person.role();
        //System.out.println("person:" + role);
        //Class<? extends Annotation> ca1 = person.annotationType();
        //System.out.println("person:" + ca1);
        //Person[] personArr = persons.value();
        //System.out.println("persons:" + personArr);
        //for (Person p1 : personArr) {
        //    System.out.println(p1.role());
        //    System.out.println(p1.annotationType());
        //}
        //System.out.println("persons:" +persons.annotationType());
        System.out.println(person);
        System.out.println(persons);
        System.out.println("=====================clazz.getAnnotations()========================");
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation1 : annotations) {
            System.out.println(annotation1.annotationType());
        }
    }
}

四. 注解的分類

根據(jù)注解參數(shù)的個(gè)數(shù),我們可以將注解分為三類:
  1. 標(biāo)記注解:一個(gè)沒有成員定義的Annotation類型被稱為標(biāo)記注解。這種Annotation類型僅使用自身的存在與否來為我們提供信息。比如后面的系統(tǒng)注解@Override;
  2. 單值注解
  3. 完整注解
根據(jù)注解使用方法和用途,我們可以將Annotation分為三類:
  1. JDK內(nèi)置系統(tǒng)注解
  2. 元注解
  3. 自定義注解

五. 注解與反射

獲取Class對象:Class<?> clazz = Class.forName("com.dayDayUp.annotation.retentionAnno.SuperMan");
判斷Class對象是否是注解:boolean flag2 = clazz.isAnnotationPresent(Persons.class);
判斷Class對象是否是注解:boolean c2 = Person.class.isAnnotation();
獲取指定注解:**``Person person = clazz.getAnnotation(Person.class);
獲取所有注解:Annotation[] annotations = clazz.getAnnotations();
獲取注解類型:annotation1.annotationType();

六. 自定義注解

@Target(value =ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface StuFiled {
    String cloumnName();
    String type();
    int length();
}

@StuTable("t_student")
public class Student {
    @StuFiled(cloumnName="id" ,type = "int" ,length = 6)
    private int id;
    @StuFiled(cloumnName="id" ,type = "String" ,length = 18)
    private String name;
    @StuFiled(cloumnName="id" ,type = "String" ,length = 2)
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }
}

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
 * @Description: 使用反射讀取注解的信息,模擬處理注解信息的流程
 */
public class Demo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> clazz = Class.forName("tf56.partyManage.study.annotation.Student");
        //獲取這個(gè)類所有的有效注解注解(獲得了類上的注解)
        //@tf56.partyManage.study.annotation.StuTable(value=t_student)
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations){
            System.out.println(annotation);
        }
        //獲取類的指定的注解
        StuTable st = clazz.getAnnotation(StuTable.class);
        System.out.println(st);
        System.out.println(st.value());

        /**
         * 獲取類的屬性的注解
         *  1、首先通過反射獲取file,屬性
         *  2、通過file獲取對應(yīng)屬性上的注解
         *  2、獲取注解的值
         *  反射
         */
        Field f = clazz.getDeclaredField("name");
        System.out.println(f);
        StuFiled sf = f.getAnnotation(StuFiled.class);
        System.out.println(sf.cloumnName());
        System.out.println(sf.type());
        System.out.println(sf.length());
    }
}
#案例二
public class NoBug {
    @Jiecha
    public void suanShu(){
        System.out.println("1234567890");
    }
    @Jiecha
    public void jiafa(){
        System.out.println("1+1="+1+1);
    }
    @Jiecha
    public void jiefa(){
        System.out.println("1-1="+(1-1));
    }
    @Jiecha
    public void chengfa(){
        System.out.println("3 x 5="+ 3*5);
    }
    @Jiecha
    public void chufa(){
        System.out.println("6 / 0="+ 6 / 0);
    }
    public void ziwojieshao(){
        System.out.println("我寫的程序沒有 bug!");
    }
}

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Jiecha {

}

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestTool {
   public static void main(String[] args) {
        // TODO Auto-generated method stub
        NoBug testobj = new NoBug();
        Class clazz = testobj.getClass();
        Method[] method = clazz.getDeclaredMethods();
        //用來記錄測試產(chǎn)生的 log 信息
        StringBuilder log = new StringBuilder();
        // 記錄異常的次數(shù)
        int errornum = 0;
        for ( Method m: method ) {
            // 只有被 @Jiecha 標(biāo)注過的方法才進(jìn)行測試
            if ( m.isAnnotationPresent( Jiecha.class )) {
                try {
                    m.setAccessible(true);
                    m.invoke(testobj, null);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    //e.printStackTrace();
                    errornum++;
                    log.append(m.getName());
                    log.append(" ");
                    log.append("has error:");
                    log.append("\n\r  caused by ");
                    //記錄測試過程中,發(fā)生的異常的名稱
                    log.append(e.getCause().getClass().getSimpleName());
                    log.append("\n\r");
                    //記錄測試過程中,發(fā)生的異常的具體信息
                    log.append(e.getCause().getMessage());
                    log.append("\n\r");
                } 
            }
        }
        log.append(clazz.getSimpleName());
        log.append(" has  ");
        log.append(errornum);
        log.append(" error.");
        // 生成測試報(bào)告
        System.out.println(log.toString());

    }

}

測試的結(jié)果是:

1234567890
1+1=11
1-1=0
3 x 5=15
chufa has error:
caused by ArithmeticException
/ by zero
NoBug has 1 error.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 什么是注解(Annotation):Annotation(注解)就是Java提供了一種元程序中的元素關(guān)聯(lián)任何信息和...
    九尾喵的薛定諤閱讀 3,425評論 0 2
  • 關(guān)于注解首先引入官方文檔的一句話:Java 注解用于為 Java 代碼提供元數(shù)據(jù)。作為元數(shù)據(jù),注解不直接影響你的代...
    編程小世界閱讀 539評論 0 0
  • 從JDK5開始,Java增加了Annotation(注解),Annotation是代碼里的特殊標(biāo)記,這些標(biāo)記可以在...
    CarlosLynn閱讀 658評論 0 2
  • 枚舉類的理解 枚舉類的理解 : 類的對象只有有限個(gè),確定的.我們稱之為枚舉類2.當(dāng)定義一組常量的時(shí)候,強(qiáng)烈建議使用...
    是小豬童鞋啦閱讀 257評論 0 0
  • 從JDK5開始,Java增加了Annotation(注解),Annotation是代碼里的特殊標(biāo)記,這些標(biāo)記可以在...
    lay_wn閱讀 995評論 0 1

友情鏈接更多精彩內(nèi)容