1 16 package org.directwebremoting.hibernate; 17 18 import java.beans.BeanInfo ; 19 import java.beans.IntrospectionException ; 20 import java.beans.Introspector ; 21 import java.beans.PropertyDescriptor ; 22 import java.lang.reflect.Method ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import net.sf.hibernate.Hibernate; 27 28 import org.directwebremoting.convert.BeanConverter; 29 import org.directwebremoting.extend.Converter; 30 import org.directwebremoting.extend.MarshallException; 31 32 36 public class H2BeanConverter extends BeanConverter implements Converter 37 { 38 41 public Map getPropertyMapFromObject(Object example, boolean readRequired, boolean writeRequired) throws MarshallException 42 { 43 Class clazz = Hibernate.getClass(example); 44 try 45 { 46 BeanInfo info = Introspector.getBeanInfo(clazz); 47 PropertyDescriptor [] descriptors = info.getPropertyDescriptors(); 48 49 Map properties = new HashMap (); 50 for (int i = 0; i < descriptors.length; i++) 51 { 52 PropertyDescriptor descriptor = descriptors[i]; 53 String name = descriptor.getName(); 54 55 if (name.equals("class")) 57 { 58 continue; 59 } 60 61 if (!isAllowedByIncludeExcludeRules(name)) 63 { 64 continue; 65 } 66 67 if (readRequired && descriptor.getReadMethod() == null) 68 { 69 continue; 70 } 71 72 if (writeRequired && descriptor.getWriteMethod() == null) 73 { 74 continue; 75 } 76 77 properties.put(name, new H2PropertyDescriptorProperty(descriptor)); 78 } 79 80 return properties; 81 } 82 catch (Exception ex) 83 { 84 throw new MarshallException(clazz, ex); 85 } 86 } 87 88 96 protected Method findGetter(Object data, String property) throws IntrospectionException 97 { 98 String key = data.getClass().getName() + ":" + property; 99 100 Method method = (Method ) methods.get(key); 101 if (method == null) 102 { 103 PropertyDescriptor [] props = Introspector.getBeanInfo(data.getClass()).getPropertyDescriptors(); 104 for (int i = 0; i < props.length; i++) 105 { 106 if (props[i].getName().equalsIgnoreCase(property)) 107 { 108 method = props[i].getReadMethod(); 109 } 110 } 111 112 methods.put(key, method); 113 } 114 115 return method; 116 } 117 118 121 protected static final Map methods = new HashMap (); 122 } 123 | Popular Tags |