Java examples for Reflection:Java Bean
hash a Bean
//package com.java2s; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; public class Main { public static int hashBean(Object bean) { if (bean == null) return -1; int hash = bean.getClass().hashCode(); // new ToStringCreator(this) try {/*w w w .j ava 2s . c o m*/ BeanInfo bi = Introspector.getBeanInfo(bean.getClass()); PropertyDescriptor[] pd = bi.getPropertyDescriptors(); for (int i = 0; i < pd.length; i++) { if (!"class".equals(pd[i].getName())) { Object result = pd[i].getReadMethod().invoke(bean); if (result != null) { hash += result.hashCode(); if (i == pd.length - 1) continue; } } } } catch (Exception ex) { return -1; } return hash; } }