本文目錄一覽:
java如何獲取類上的註解
如何獲取類的註解和註解的內容
java 反射
Class someClass = Some.getClass();
註解 somtAnnotation = someClass.getAnnotation(註解.class)
屬性類型 屬性值 = someAnnotation.屬性();
要一一遍歷么?
這個要根據需求來頂,誰用誰遍歷,
java反射機制 怎樣獲取到類上面的註解
// 定義註解並指定java註解保留策略為運行時RUNTIME,運行時注入到JAVA位元組碼文件里
// 這樣才可以在運行時反射並獲取它。
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@interface MyAnnotation{
String key() default “”;
int value() default 0;
}
// 使用註解
@MyAnnotation(key=”key1″,value=200)
class MyClass{}
// 反射註解
public static void main(String[] args){
MyClass myClass=new MyClass();
MyAnnotation annotation=myClass.getClass().getAnnotation(MyAnnotation.class);
System.out.println(“key=”+annotation.key()+”\tvalue=”+annotation.value());
}
Java 註解的讀取註解信息的方法
屬於重點,在系統中用到註解許可權時非常有用,可以精確控制許可權的粒度
注意:要想使用反射去讀取註解,必須將Retention的值選為Runtime Java代碼import java.lang.annotation.Annotation;import java.lang.reflect.Method;//讀取註解信息public class ReadAnnotationInfoTest { public static void main(String[] args) throws Exception { // 測試AnnotationTest類,得到此類的類對象 Class c = Class.forName(com.iwtxokhtd.annotation.AnnotationTest); // 獲取該類所有聲明的方法 Method[] methods = c.getDeclaredMethods(); // 聲明註解集合 Annotation[] annotations; // 遍歷所有的方法得到各方法上面的註解信息 for (Method method : methods) { // 獲取每個方法上面所聲明的所有註解信息 annotations = method.getDeclaredAnnotations(); // 再遍歷所有的註解,列印其基本信息 System.out.println(method.getName()); for (Annotation an : annotations) { System.out.println(方法名為: + method.getName() + 其上面的註解為: + an.annotationType().getSimpleName()); Method[] meths = an.annotationType().getDeclaredMethods(); // 遍歷每個註解的所有變數 for (Method meth : meths) { System.out.println(註解的變數名為: + meth.getName()); } } } }}
java獲取當前類上的註解內容
@Retention(RetentionPolicy.RUNTIME) // 註解會在class位元組碼文件中存在,在運行時可以通過反射獲取到
@Target({ElementType.FIELD,ElementType.METHOD})//定義註解的作用目標**作用範圍欄位、枚舉的常量/方法
@Documented//說明該註解將被包含在javadoc中
public @interface FieldMeta {
/**
* 是否為序列號
* @return
*/
boolean id() default false;
/**
* 欄位名稱
* @return
*/
String name() default “”;
/**
* 是否可編輯
* @return
*/
boolean editable() default true;
/**
java 獲取調用此方法的方法的註解
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String name();
}
public class AnnonTestA {
public void methodA(){
}
@MyAnnotation(name=”111″)
public void methodA(String a) throws Exception{
AnnonTestB.methodB(“methodA”,String.class);
}
}
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class AnnonTestB {
public static void methodB(String methodName, Class?… parameterTypes) throws Exception {
AnnonTestA annonTestA = new AnnonTestA();
// 獲取AnnotationTest2的Class實例
ClassAnnonTestA c = AnnonTestA.class;
// 獲取需要處理的方法Method實例
Method method = c.getMethod(methodName, parameterTypes);
Method[] methods = c.getMethods();
// 判斷該方法是否包含MyAnnotation註解
if (method.isAnnotationPresent(MyAnnotation.class)) {
// 獲取該方法的MyAnnotation註解實例
MyAnnotation myAnnotation = method
.getAnnotation(MyAnnotation.class);
// 執行該方法
// method.invoke(annonTestA, “12345”);
// 獲取myAnnotation
String value1 = myAnnotation.name();
System.out.println(value1);
}
// 獲取方法上的所有註解
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
}
}
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class TestString {
public static void main(String[] args) throws Exception {
AnnonTestA annonTestA = new AnnonTestA();
annonTestA.methodA(“123”);
}
}
代碼都給上了,不明白再追問吧。
參考地址:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/252940.html