本篇文章為大家展示了怎么利用反射取得Annotation信息,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
崇義ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
在進(jìn)行類或方法定義時(shí),都可以使用一系列的Annotation進(jìn)行聲明,于是如果要想獲得這些Annotation信息,那么可以直接通過反射來(lái)完成。在java.lang.reflect里面有一個(gè)AccessibleObject類,在本類中提供有獲取Annotation類的方法:
獲取全部Annotation:public Annotation[] getAnnotations()
獲取指定Annotation:public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
范例:定義一個(gè)接口,并在接口在使用Annotation
import java.io.Serializable;import java.lang.annotation.Annotation;import java.lang.reflect.Method;public class JavaAPIDemo {public static void main(String[] args) throws Exception { { //獲取接口上的Annotation信息Annotation annotations [] = IMessage.class.getAnnotations(); //獲取接口上的全部Annotationfor (Annotation temp : annotations) { System.out.println(temp);//@java.lang.FunctionalInterface()//@java.lang.Deprecated(forRemoval=false, since="1.0")} } System.out.println("-----------------------"); {//獲取MessageImpl子類上的Annotation信息Annotation annotations []= MessageImpl.class.getAnnotations(); //獲取類上的全部Annotationfor (Annotation temp : annotations) { System.out.println(temp); } } System.out.println("-----------------------"); { //獲取MessageImpl.toString()方法上的Annotation信息Method method = MessageImpl.class.getDeclaredMethod("send", String.class);Annotation annotations [] = method.getAnnotations(); for (Annotation temp : annotations) { System.out.println(temp); } } } }@FunctionalInterface //程序執(zhí)行時(shí)可以獲取@Deprecated(since = "1.0") interface IMessage { //有2個(gè)Annotationpublic void send(String msg); }@SuppressWarnings("serial") //無(wú)法在程序執(zhí)行時(shí)獲取class MessageImpl implements IMessage, Serializable {@Override //無(wú)法在程序執(zhí)行時(shí)獲取public void send(String msg) { System.out.println("【消息發(fā)送】" + msg); } }
不同的Annotation有它的存在范圍,下面對(duì)比兩個(gè)Annotation:
@FunctionalInterface(運(yùn)行時(shí)):
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE) public @interface FunctionalInterface {}
@SuppressWarnings(源代碼):
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})@Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings {}
現(xiàn)在發(fā)現(xiàn)“@FunctionalInterface”是在運(yùn)行時(shí)生效的Annotation,所以程序執(zhí)行時(shí)可以獲取Annotation;而“@SuppressWarnings”是在源代碼編寫時(shí)有效。
在RetentionPolicy枚舉類中還有一個(gè)class的定義,指的是在類定義時(shí)生效。
現(xiàn)在已經(jīng)清楚了Annotation的獲取,以及Annotation的運(yùn)行策略,但是最為關(guān)鍵性的因素是如何實(shí)現(xiàn)自定義的Annotation呢?為此在Java中提供了新的語(yǔ)法,使用“@interface”來(lái)定義Annotation。
范例:自定義Annotation
import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.reflect.Method;@Retention(RetentionPolicy.RUNTIME) //定義Annotation的運(yùn)行策略@interface DefaultAnnotation { //自定義的Annotationpublic String title(); //獲取數(shù)據(jù)public String url() default "www.mldn.cn"; //獲取數(shù)據(jù)class Message {@DefaultAnnotation(title = "MLDN")public void send(String msg) { System.out.println("【消息發(fā)送】" + msg); } }public class JavaAPIDemo {public static void main(String[] args) throws Exception { Method method = Message.class.getMethod("send",String.class); //獲取指定方法DefaultAnnotation anno = method.getAnnotation(DefaultAnnotation.class); //獲取指定的Annotation//System.out.println(anno.title()); //直接調(diào)用Annotation中的方法 MLDN//System.out.println(anno.url()); //直接調(diào)用Annotation中的方法 www.mldn.cn//直接調(diào)用Annotation中的方法String msg = anno.title()+"("+anno.url()+")"; //消息內(nèi)容 method.invoke(Message.class.getDeclaredConstructor().newInstance(), msg); //【消息發(fā)送】MLDN(www.mldn.cn)} }
使用Annotation之后的最大特點(diǎn)是可以結(jié)合反射機(jī)制實(shí)現(xiàn)程序的處理。
上述內(nèi)容就是怎么利用反射取得Annotation信息,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:怎么利用反射取得Annotation信息
文章地址:http://jinyejixie.com/article8/jjjiip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、ChatGPT、網(wǎng)頁(yè)設(shè)計(jì)公司、電子商務(wù)、做網(wǎng)站、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)