日期:2014-05-20 浏览次数:20881 次
@Target({ ElementType.TYPE, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Label {
String chineseLabel();
}
@Label(chineseLabel = "人员信息")
public class Person {
@Label(chineseLabel = "姓名")
String name;
@Label(chineseLabel = "性别")
String sex;
@Label(chineseLabel = "年龄")
Integer age;
@Label(chineseLabel = "身份证号")
String idNo;
@Label(chineseLabel = "是否已婚(已婚:T,未婚:F)")
Boolean isMerried;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getIdNo() {
return idNo;
}
public void setIdNo(String idNo) {
this.idNo = idNo;
}
public Boolean getIsMerried() {
return isMerried;
}
public void setIsMerried(Boolean isMerried) {
this.isMerried = isMerried;
}
}
public interface PersonAction {
public Person process(Person person);
}
public class PersonInput implements PersonAction {
@Override
public Person process(Person person) {
if (person == null) {
person = new Person();
}
Class<? extends Person> clazz = person.getClass();
if (clazz.isAnnotationPresent(Label.class)) {
System.out.println(clazz.getAnnotation(Label.class).chineseLabel());
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Object content;
PropertyDescriptor pd;
Method method;
for (Field field : clazz.getDeclaredFields()) {
try {
if (field.isAnnotationPresent(Label.class)) {