java 自定义注解以及获得注解的值
1.自定義注解
import java.lang.annotation.*;@Documented @Target(ElementType.FIELD) @Inherited @Retention(RetentionPolicy.RUNTIME ) public @interface MyAnno {/*** 是否能為null* @return*/boolean isCanNull() default true;/*** 是否能為空字符串* @return*/boolean isCanEmpty() default true;/*** 是否能為0* @return*/boolean isCanZero() default true; }2.使用注解:
public class Mouse {@MyAnno(isCanNull=true)private String name;@MyAnno(isCanNull = false,isCanZero = false)private int age;@MyAnno(isCanNull = false)private String address;@MyAnno(isCanZero = false)private double money;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public double getMoney() {return money;}public void setMoney(double money) {this.money = money;}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;} }3.獲得注解的值并對(duì)其判斷
package com.vweb.util;import com.vweb.webapp.Mouse; import com.vweb.webapp.MyAnno; import com.vweb.webapp.TestUtil; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Arrays; import java.util.List; public class IntactCheckUtil {public static boolean check(Object obj){// list = Arrays.asList(AnnotationParsing.class.getClassLoader().loadClass(((Class)obj).getClass().getName()).getDeclaredFields());List<Field> list = Arrays.asList(obj.getClass().getDeclaredFields());for(int i=0;i<list.size();i++){Field field = list.get(i);if(field.isAnnotationPresent(MyAnno.class)){//是否使用MyAnno注解for (Annotation anno : field.getDeclaredAnnotations()) {//獲得所有的注解if(anno.annotationType().equals(MyAnno.class) ){//找到自己的注解if(!((MyAnno)anno).isCanNull()){//注解的值if(TestUtil.getFieldValueByName(field.getName(),obj)==null){throw new RuntimeException("類:"+Mouse.class+"的屬性:"+field.getName()+"不能為空,實(shí)際的值:"+TestUtil.getFieldValueByName(field.getName(),obj)+",注解:field.getDeclaredAnnotations()");}}if(!((MyAnno)anno).isCanEmpty()){if(TestUtil.getFieldValueByName(field.getName(),obj).equals("")){throw new RuntimeException("類:"+Mouse.class+"的屬性:"+field.getName()+"不能為空字符串,實(shí)際的值:"+TestUtil.getFieldValueByName(field.getName(),obj)+",注解:field.getDeclaredAnnotations()");}}if(!((MyAnno)anno).isCanZero()){if(TestUtil.getFieldValueByName(field.getName(),obj).toString().equals("0") || TestUtil.getFieldValueByName(field.getName(),obj).toString().equals("0.0")){throw new RuntimeException("類:"+Mouse.class+"的屬性:"+field.getName()+"不能為空字符0,實(shí)際的值:"+TestUtil.getFieldValueByName(field.getName(),obj)+",注解:field.getDeclaredAnnotations()");}}}}}}return true;} }備注:注解各參數(shù)的使用(以下內(nèi)容來自互聯(lián)網(wǎng)http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html)
@Target:
@Target說明了Annotation所修飾的對(duì)象范圍:Annotation可被用于 packages、types(類、接口、枚舉、Annotation類型)、類型成員(方法、構(gòu)造方法、成員變量、枚舉值)、方法參數(shù)和本地變量(如循環(huán)變量、catch參數(shù))。在Annotation類型的聲明中使用了target可更加明晰其修飾的目標(biāo)。
? ? 取值(ElementType)有:
    1.CONSTRUCTOR:用于描述構(gòu)造器
    2.FIELD:用于描述域
    3.LOCAL_VARIABLE:用于描述局部變量
    4.METHOD:用于描述方法
    5.PACKAGE:用于描述包
    6.PARAMETER:用于描述參數(shù)
    7.TYPE:用于描述類、接口(包括注解類型) 或enum聲明
@Retention定義了該Annotation被保留的時(shí)間長短:某些Annotation僅出現(xiàn)在源代碼中,而被編譯器丟棄;而另一些卻被編譯在class文件中;編譯在class文件中的Annotation可能會(huì)被虛擬機(jī)忽略,而另一些在class被裝載時(shí)將被讀取(請(qǐng)注意并不影響class的執(zhí)行,因?yàn)锳nnotation與class在使用上是被分離的)。使用這個(gè)meta-Annotation可以對(duì) Annotation的“生命周期”限制。
取值(RetentionPoicy)有:
    1.SOURCE:在源文件中有效(即源文件保留)
    2.CLASS:在class文件中有效(即class保留)
    3.RUNTIME:在運(yùn)行時(shí)有效(即運(yùn)行時(shí)保留)
@Documented:
該屬性用于描述其它類型的annotation應(yīng)該被作為被標(biāo)注的程序成員的公共API,因此可以被例如javadoc此類的工具文檔化。Documented是一個(gè)標(biāo)記注解,沒有成員。
@Inherited:
@Inherited 元注解是一個(gè)標(biāo)記注解,@Inherited闡述了某個(gè)被標(biāo)注的類型是被繼承的。如果一個(gè)使用了@Inherited修飾的annotation類型被用于一個(gè)class,則這個(gè)annotation將被用于該class的子類。
注意:@Inherited annotation類型是被標(biāo)注過的class的子類所繼承。類并不從它所實(shí)現(xiàn)的接口繼承annotation,方法并不從它所重載的方法繼承annotation。
當(dāng)@Inherited annotation類型標(biāo)注的annotation的Retention是RetentionPolicy.RUNTIME,則反射API增強(qiáng)了這種繼承性。如果我們使用java.lang.reflect去查詢一個(gè)@Inherited annotation類型的annotation時(shí),反射代碼檢查將展開工作:檢查class和其父類,直到發(fā)現(xiàn)指定的annotation類型被發(fā)現(xiàn),或者到達(dá)類繼承結(jié)構(gòu)的頂層。
自定義注解:
使用@interface自定義注解時(shí),自動(dòng)繼承了java.lang.annotation.Annotation接口,由編譯程序自動(dòng)完成其他細(xì)節(jié)。在定義注解時(shí),不能繼承其他的注解或接口。@interface用來聲明一個(gè)注解,其中的每一個(gè)方法實(shí)際上是聲明了一個(gè)配置參數(shù)。方法的名稱就是參數(shù)的名稱,返回值類型就是參數(shù)的類型(返回值類型只能是基本類型、Class、String、enum)??梢酝ㄟ^default來聲明參數(shù)的默認(rèn)值。
  定義注解格式:
  public?@interface 注解名 {定義體}
注解參數(shù)的可支持?jǐn)?shù)據(jù)類型:
    1.所有基本數(shù)據(jù)類型(int,float,boolean,byte,double,char,long,short)
    2.String類型
    3.Class類型
    4.enum類型
    5.Annotation類型
    6.以上所有類型的數(shù)組
  Annotation類型里面的參數(shù)該怎么設(shè)定:?
  第一,只能用public或默認(rèn)(default)這兩個(gè)訪問權(quán)修飾.例如,String value();這里把方法設(shè)為defaul默認(rèn)類型;   
  第二,參數(shù)成員只能用基本類型byte,short,char,int,long,float,double,boolean八種基本數(shù)據(jù)類型和 String,Enum,Class,annotations等數(shù)據(jù)類型,以及這一些類型的數(shù)組.例如,String value();這里的參數(shù)成員就為String;  
  第三,如果只有一個(gè)參數(shù)成員,最好把參數(shù)名稱設(shè)為"value",后加小括號(hào).例:下面的例子FruitName注解就只有一個(gè)參數(shù)成員。
簡單的自定義注解和使用注解實(shí)例:
轉(zhuǎn)載于:https://www.cnblogs.com/mouseIT/p/5033746.html
總結(jié)
以上是生活随笔為你收集整理的java 自定义注解以及获得注解的值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 下载各种在线视频及字幕
 - 下一篇: java 解析二进制文件保存为txt文件