android 自定義註解 通過反射獲取註解屬性值
1.自定義註解
package cn.veji.hibernate.po;
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 Privilege {
String[] value();
}
public enum ElementType {
TYPE,// 類、介面、註解型別或列舉
FIELD, //屬性
METHOD, //方法
PARAMETER,// 用於描述引數
CONSTRUCTOR,//構造方法
LOCAL_VARIABLE,//區域性變數
ANNOTATION_TYPE,//註解類
PACKAGE //包
}
從上面程式碼可以看出Target 對應的作用域(Target可以接受多個引數,逗號分隔即可)
例如:
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention (RetentionPolicy.RUNTIME)
public @interface MyTestIn {
String author() default "kaelthas.wang";
String address() default "山東青島";
}
2.獲取類註解屬性值
/**
* 讀取註解值
*
* @param annotationClasss 處理Annotation類名稱
* @param annotationField 處理Annotation類屬性名稱
* @param className 處理Annotation的使用類名稱
* @return
* @throws Exception
*/
@SuppressWarnings("all")
public Map<String, String> loadVlaue(Class annotationClasss,
String annotationField, String className) throws Exception {
System.out.println("處理Annotation類名稱 === "+annotationClasss.getName());
System.out.println("處理Annotation類屬性名稱 === "+annotationField);
System.out.println("處理Annotation的呼叫類名稱 === "+className);
Map<String, String> map = new HashMap<String, String>();
Method[] methods = Class.forName(className).getDeclaredMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(annotationClasss)) {
Annotation p = method.getAnnotation(annotationClasss);
Method m = p.getClass()
.getDeclaredMethod(annotationField, null);
//這裡根據屬性引數型別進行強制型別轉換
String[] values = (String[]) m.invoke(p, null);
for (String key : values) {
System.out.println("註解值 === " + key);
map.put(key, key);
}
}
}
System.out.println("map數量 === " + map.size());
return map;
}
3.獲取方法註解屬性值
這裡的屬性值為int 強制型別轉換時候使用Integer
@SuppressWarnings("all")
public int loadVlaue(Class annotationClasss,
String annotationField, String className) {
System.out.println("處理Annotation類名稱 === " + annotationClasss.getName());
System.out.println("處理Annotation類屬性名稱 === " + annotationField);
System.out.println("處理Annotation的呼叫類名稱 === " + className);
Map<String, String> map = new HashMap<String, String>();
try {
Method[] methods = Class.forName(className).getDeclaredMethods();
Class test = Class.forName(className);
if (test.isAnnotationPresent(annotationClasss)) {
Annotation p = test.getAnnotation(annotationClasss);
Method m = p.getClass()
.getDeclaredMethod(annotationField, null);
return (Integer) m.invoke(p, null);
}
} catch (Exception e) {
return -1;
}
return -1;
}
相關推薦
android 自定義註解 通過反射獲取註解屬性值
參考文章:http://xuwenjin666.iteye.com/blog/1637247 1.自定義註解 package cn.veji.hibernate.po; import java.lang.annotation.ElementType; import ja
android 自定義註解 通過反射獲取註解屬性值
1.自定義註解 package cn.veji.hibernate.po; import java.lang.annotation.ElementType; import java.
在多執行緒中使用匿名類通過反射獲取其屬性值
標題挺彆扭的,至少我對這些高階技術瞭解的還很少,繼續學習吧...初次接觸匿名類感覺很爽,簡化了程式碼量(可能有些時候還真需要它) 控制檯的程式碼 using System.Collections.Generic; using System.Linq; using Sys
Java的自定義註解及通過反射獲取註解
tco element pos pri jvm ado 參數聲明 機制 world 一、註解基本知識 1、元註解:@Retention @Target @Document @Inherited 2、Annotation型定義為@interface, 所有的Annot
java中自定義註解並通過反射獲取註解屬性值
直接上程式碼,註釋中有說明: 1、定義自定義註解類(類註解和欄位註解): package com.uno.ray; import java.lang.annotation.Documented; import java.lang
java @interface自定義註解和通過反射獲取註解屬性值
@interface @interface用來宣告一個註解,其中的每一個方法實際上是聲明瞭一個配置引數。 方法的名稱就是引數的名稱,返回值型別就是引數的型別 引數型別只能是基本型別、Class、String、enum。 可以通過default來宣告引數的
java 通過反射獲取註解
使用反射獲取某個類的註釋、方法上的註釋、屬性上的註釋。 import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import javax.x
Java自定義Annotation,通過反射解析Annotation
建立一個自定義的Annotation import java.lang.annotation.*; import java.lang.reflect.Method; @Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUN
Android通過Java反射獲取系統屬性值
android.os.SystemProperties類可以用於獲取系統的屬性值, 然而這個類是內部使用的, 在應用程式中我們無法匯入此類. 本文通過Java反射機制, 實現了呼叫這個類的方法, 從而獲取系統屬性值. 閒話休提, 書歸正傳.程式設計師最想看到的還是程式碼.下
Java 通過getProperties()獲取系統屬性值,引數值
System.getProperties()獲得系統的所有屬性值,然後返回一個Properties屬性的集合。 裡面存放的是屬性和屬性值的關係對映,包括預設的編碼方式。簡單演示如下 獲取系統屬性值 import java.util.Map.Entry; import ja
java獲取反射獲取私有屬性值
//拼接類名 String ob = "com.project.framework.entity.form.ReShipChangeregistration"; //反向生成class物件 Class cls=Class.forNam
AOP通過反射獲取自定義註解
ram tar .get tty sig runt type log eth 自定義註解: @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component
Java 自定義註解&通過反射獲取類、方法、屬性上的註解
反射 JAVA中的反射是執行中的程式檢查自己和軟體執行環境的能力,它可以根據它發現的進行改變。通俗的講就是反射可以在執行時根據指定的類名獲得類的資訊。 註解的定義 註解通過 @interface 關鍵字進行定義。 /** * 自定義註解 *
自定義註解---反射獲取註解案例
自定義註解: package Demo01; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy
Java自定義註解和執行時靠反射獲取註解
<span style="font-family:SimHei;font-size:18px;color:#663300;"><strong><span class="bh_code_a_Java_keywords">public</span> <span
自定義註解,通過反射獲得註解中的值(詳細自定義註解解釋)
lean ner interface 子類 cts clas def EDA 通過 自定義註解(@Alias): package com.nf.lc.demo3; import java.lang.annotation.*; /* 定義註解的生命周期元註解:@
Android自定義註解處理器
Android自定義註解處理器 理論 相信不少開發人員在開發過程中都用到過不少的註解,它能提高我們的工作效率,也讓專案和程式碼結構變得更簡潔和清晰。目前已經有很多主流的框架也用到了註解技術。例如:ButterKnife、Dagger2、Retrofit、Glide等。可見,註解變
Android 自定義註解(Annotation)
現在市面上很多框架都有使用到註解,比如butterknife庫、EventBus庫、Retrofit庫等等。也是一直好奇他們都是怎麼做到的,註解的工作原理是啥。咱們能不能自己去實現一個簡單的註解呢。 &nb
通過反射獲取類上的註解
@AnnotationField(tableName = "tb_animal", flag = 0)//使用多個註解值,中間用,割開,如果不想使用預設值,可以在註解中為其重新賦值 @AnnotationTest1("tb_animal") public class Animal { priva
java通過反射獲取類名、屬性名稱以及@Table註解上的表名稱
import javax.persistence.Column; import javax.persistence.Id; import javax.persistence.Table; import java.lang.reflect.Field; import java.