提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取Java Bean属性描述符
//package com.nowjava; import java.beans.BeanInfo;//n o w j a v a . c o m import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; public class Main { public static void main(String[] argv) throws Exception { Class beanClass = String.class; String propertyName = "nowjava.com"; System.out.println(getPropertyDescriptor(beanClass, propertyName)); } /*nowjava - 时 代 Java*/ public static PropertyDescriptor getPropertyDescriptor(Class beanClass, String propertyName) { for (PropertyDescriptor pd : getPropertyDescriptors(beanClass)) { if (pd.getName().equals(propertyName)) { return pd; } } return null; } public static PropertyDescriptor[] getPropertyDescriptors( Class beanClass) { BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(beanClass); } catch (IntrospectionException e) { return (new PropertyDescriptor[0]); } PropertyDescriptor[] descriptors = beanInfo .getPropertyDescriptors(); if (descriptors == null) { descriptors = new PropertyDescriptor[0]; } return descriptors; } public static