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 获取类,属性变量,方法,方法参数上注解的值等的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1803无法升级到2004_微软向win
- 下一篇: java取消按钮事件_java按钮事件处