大侠们,小弟学习Annotation时遇到了点小困惑,如何迭代输出任意自定义Annotation的实例的注解成员值呢?,
我自定义的Anotation Validation package com.wanghaisheng.generator.support; 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 Validate {
//必输字段
boolean required() default false;
//最小长度
int minLength() default 0;
//最大长度
int maxlength() default 0;
} 然后有一个自定义的POJO User
import com.wanghaisheng.generator.support.Validate;public class User {
private String userName; @Validate(required=true,minLength=6)
public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
}
}通过代码:
Annotation annotation = User.class.getDeclaredMethod("getEntityId", new Class<?>[]{}).getAnnotation(Validate.class);得到一个Validate的实例,如何迭代输出该实例的注解成员名=注解成员值对呢?
如:对于以上实例须迭代输出
required=true
minLength=6
.........
.........如果对于任意对用户透明的自定义annotation该如何处理?
另自定义的annotation默认的toString方法是如何实现的?
大侠们多多指教啊!
我自定义的Anotation Validation package com.wanghaisheng.generator.support; 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 Validate {
//必输字段
boolean required() default false;
//最小长度
int minLength() default 0;
//最大长度
int maxlength() default 0;
} 然后有一个自定义的POJO User
import com.wanghaisheng.generator.support.Validate;public class User {
private String userName; @Validate(required=true,minLength=6)
public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
}
}通过代码:
Annotation annotation = User.class.getDeclaredMethod("getEntityId", new Class<?>[]{}).getAnnotation(Validate.class);得到一个Validate的实例,如何迭代输出该实例的注解成员名=注解成员值对呢?
如:对于以上实例须迭代输出
required=true
minLength=6
.........
.........如果对于任意对用户透明的自定义annotation该如何处理?
另自定义的annotation默认的toString方法是如何实现的?
大侠们多多指教啊!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货