java 反射 Introspector

public class Person {

	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}
public class IntrospectorTest {


<span style="white-space:pre">	</span>@SuppressWarnings("rawtypes")
<span style="white-space:pre">	</span>public static void main(String[] args) throws IntrospectionException {


<span style="white-space:pre">		</span>BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);//Introspector 内省
<span style="white-space:pre">		</span>System.out.println("BeanDescriptor================================");
<span style="white-space:pre">		</span>BeanDescriptor beanDesc = beanInfo.getBeanDescriptor(); //bean描述  descriptor描述符号
<span style="white-space:pre">		</span>Class cls = beanDesc.getBeanClass();
<span style="white-space:pre">		</span>System.out.println(cls.getName());
//<span style="white-space:pre">		</span>com.spring.learning.Person
<span style="white-space:pre">		</span>System.out.println("MethodDescriptor===============================");
<span style="white-space:pre">		</span>MethodDescriptor[] methodDescs = beanInfo.getMethodDescriptors();//method描述
<span style="white-space:pre">		</span>for (int i = 0; i < methodDescs.length; i++) {
<span style="white-space:pre">			</span>Method method = methodDescs[i].getMethod();
<span style="white-space:pre">			</span>System.out.println(method.getName());
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>/*
<span style="white-space:pre">		</span>hashCode
<span style="white-space:pre">		</span>wait
<span style="white-space:pre">		</span>notifyAll
<span style="white-space:pre">		</span>equals
<span style="white-space:pre">		</span>wait
<span style="white-space:pre">		</span>toString
<span style="white-space:pre">		</span>notify
<span style="white-space:pre">		</span>wait
<span style="white-space:pre">		</span>getClass
<span style="white-space:pre">		</span>setAge
<span style="white-space:pre">		</span>getAge
<span style="white-space:pre">		</span>setName
<span style="white-space:pre">		</span>getName
<span style="white-space:pre">		</span>*/
<span style="white-space:pre">		</span>System.out.println("PropertyDescriptor==============================");
<span style="white-space:pre">		</span>PropertyDescriptor[] propDescs = beanInfo.getPropertyDescriptors();
<span style="white-space:pre">		</span>for (int i = 0; i < propDescs.length; i++) {
<span style="white-space:pre">			</span>Method methodR = propDescs[i].getReadMethod();
<span style="white-space:pre">			</span>if (methodR != null) {
<span style="white-space:pre">				</span>System.out.println(methodR.getName());
<span style="white-space:pre">			</span>}
//<span style="white-space:pre">			</span>getAge
//<span style="white-space:pre">			</span>getClass
//<span style="white-space:pre">			</span>getName
<span style="white-space:pre">			</span>Method methodW = propDescs[i].getWriteMethod();
<span style="white-space:pre">			</span>if (methodW != null) {
<span style="white-space:pre">				</span>System.out.println(methodW.getName());
<span style="white-space:pre">			</span>}
//<span style="white-space:pre">			</span>setAge
//<span style="white-space:pre">			</span>setName
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>/*
<span style="white-space:pre">		</span>getAge
<span style="white-space:pre">		</span>setAge
<span style="white-space:pre">		</span>getClass
<span style="white-space:pre">		</span>getName
<span style="white-space:pre">		</span>setName
<span style="white-space:pre">		</span> */
<span style="white-space:pre">	</span>}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值