可用通过BeanInfo接口创建BeanInfo类,并提供关于bean的方法、属性、事件和其他特性的显式信息。
通用方式
BeanInfo beanInfo = Introspector.getBeanInfo(TradeTransaction.class);
其中Introspector类提供了标准方法获取目标Java Bean支持的属性、事件和方法。
public class TradeTransaction { private String id;//交易ID private double price;//交易金额 public TradeTransaction() { } public TradeTransaction(String id, double price) { super(); this.id = id; this.price = price; } public String getId() { return id; } public void setId(String id) { this.id = id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
public static void main(String[] args) { BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(TradeTransaction.class); } catch (IntrospectionException e) { throw new IllegalStateException(e.getMessage(), e); } for (MethodDescriptor methodDescriptor : beanInfo.getMethodDescriptors()) { Method method = methodDescriptor.getMethod(); System.out.println(method.getName()); } }
日志
getClass setPrice setId wait notifyAll getId notify wait hashCode getPrice wait equals toString