java注解FIELD

package com.msli.test;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Property {
public String classPath();
public String description()default "类的路径";
}


package com.msli.test;

public class PersonVO {
public String name;
public String age;
@Property (classPath = "com.msli.test.DogVO")
public DogVO dogVO;
public PersonVO subPersonVO;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public DogVO getDogVO() {
return dogVO;
}
public void setDogVO(DogVO dogVO) {
this.dogVO = dogVO;
}
public PersonVO getSubPersonVO() {
return subPersonVO;
}
public void setSubPersonVO(PersonVO subPersonVO) {
this.subPersonVO = subPersonVO;
}

}
package com.msli.test;

public class DogVO {
public String dogName;
public String dogColor;
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public String getDogColor() {
return dogColor;
}
public void setDogColor(String dogColor) {
this.dogColor = dogColor;
}

}

package com.msli.test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

public class TestAnnotation {

public static void main(String[] args) throws Exception {
Class test2 = Class.forName("com.msli.test.PersonVO");
Field[] field=test2.getFields();
boolean flag2=test2.isAnnotationPresent(Property.class);
for(int i=0;i<field.length;i++){
if(field[i].isAnnotationPresent(Property.class)){
System.out.println("-----------"+field[i]);
Property pro=(Property)field[i].getAnnotation(Property.class);
System.out.println("--------------"+pro.classPath());
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值