日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java element 获取属性_java 获取类,属性变量,方法,方法参数上注解的值等

發布時間:2024/10/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java element 获取属性_java 获取类,属性变量,方法,方法参数上注解的值等 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一:獲取類上注解的值

定義注解@Target(ElementType.TYPE)用于類,接口等

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

public @interface Orange {

String getName();

String getValue();

}

獲取

@Orange(getName = "3333",getValue = "4444")

public class ParameterNameTest {

。。。

@Test

public void main() throws Exception {

Class clazz = ParameterNameTest.class;

if(clazz.isAnnotationPresent(Orange.class)){

// 獲取 "類" 上的注解

Orange getAnnotation = clazz.getAnnotation(Orange.class);

System.out.println("\"類\"上的注解值獲取到第一個 :"

+ getAnnotation.getName()+ ",第二個:"+ getAnnotation.getValue());

}

}

返回

"類"上的注解值獲取到第一個 :3333,第二個:4444

二:獲取屬性變量上注解的值

定義注解@Target(ElementType.FIELD)用于屬性變量

@Target(ElementType.FIELD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Banana {

String length();

String price();

}

獲取

public class ParameterNameTest {

@Banana(length = "6666", price = "$888")

String something = "other information";

@Test

public void main() throws Exception {

Class clazz = ParameterNameTest.class;

// 獲取 "屬性變量" 上的注解的值

Field[] fields = clazz.getDeclaredFields();

for(Field field: fields){

if(field.isAnnotationPresent(Banana.class)){

Banana bananaAnnotation = field.getAnnotation(Banana.class);

System.out.println("\"屬性變量\"上的注解值獲取到第一個 :"

+ bananaAnnotation.length()+ ",第二個:"+ bananaAnnotation.price());

}

}

}

}

返回

"屬性變量"上的注解值獲取到第一個 :6666,第二個:$888

三: 獲取方法上注解的值

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Apple {

String color();

String number();

}

獲取

public class ParameterNameTest {

@Apple(color = "紅色", number = "5555")

public void method1(){

// ...

}

@Test

public void main() throws Exception {

Class clazz = ParameterNameTest.class;

// 獲取 "方法"上的注解的值

Method[] methods = clazz.getDeclaredMethods();

for (Method method: methods){

if(method.isAnnotationPresent(Apple.class)){

Apple appleAnnotation = method.getAnnotation(Apple.class);

System.out.println("\"方法\"上的注解值獲取到第一個 :"

+ appleAnnotation.color()+ ",第二個:"+ appleAnnotation.number());

}

}

}

}

返回

"方法"上的注解值獲取到第一個 :紅色,第二個:5555

三: 獲取" 方法參數 " 上注解的值

@Target(ElementType.PARAMETER)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Cherry {

String value();

}

獲取

public class ParameterNameTest {

public void method2(@Cherry("1111") String param1, @Cherry("2222") String param2) {

System.out.println(param1 + param2);

}

@Test

public void main() throws Exception {

Class clazz = ParameterNameTest.class;

// 獲取 "方法參數" 上的注解的值

Method method = clazz.getDeclaredMethod("method2", String.class, String.class);

String[] parameterNames = getMethodParameterNamesByAnnotation(method);

System.out.println("\"方法參數\"上的注解值獲取到"+Arrays.toString(parameterNames));

}

/**

* 獲取給 "方法參數" 進行注解的值

*

* @param method 要獲取參數名的方法

* @return 按參數順序排列的參數名列表

*/

public static String[] getMethodParameterNamesByAnnotation(Method method) {

Annotation[][] parameterAnnotations = method.getParameterAnnotations();

if (parameterAnnotations == null || parameterAnnotations.length == 0) {

return null;

}

String[] parameterNames = new String[parameterAnnotations.length];

int i = 0;

for (Annotation[] parameterAnnotation : parameterAnnotations) {

for (Annotation annotation : parameterAnnotation) {

if (annotation instanceof Cherry) {

Cherry param = (Cherry) annotation;

parameterNames[i++] = param.value();

}

}

}

return parameterNames;

}

}

返回

"方法參數"上的注解值獲取到[1111, 2222]

小結:主要使用的API是Class類中的實現接口AnnotatedElement的方法

isAnnotationPresent --- 檢測該元素是否被對應注解修飾

default boolean isAnnotationPresent(Class extends Annotation> annotationClass) {

return getAnnotation(annotationClass) != null;

}

getAnnotation --- 獲取注解對象

T getAnnotation(Class annotationClass);

總結

以上是生活随笔為你收集整理的java element 获取属性_java 获取类,属性变量,方法,方法参数上注解的值等的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。