1 28 package net.sf.jasperreports.engine.data; 29 30 import java.beans.BeanInfo ; 31 import java.beans.IndexedPropertyDescriptor ; 32 import java.beans.IntrospectionException ; 33 import java.beans.Introspector ; 34 import java.beans.PropertyDescriptor ; 35 import java.util.ArrayList ; 36 37 import net.sf.jasperreports.engine.JRDataSourceProvider; 38 import net.sf.jasperreports.engine.JRException; 39 import net.sf.jasperreports.engine.JRField; 40 import net.sf.jasperreports.engine.JasperReport; 41 import net.sf.jasperreports.engine.design.JRDesignField; 42 43 44 51 public abstract class JRAbstractBeanDataSourceProvider implements JRDataSourceProvider 52 { 53 54 55 private Class beanClass; 56 57 62 public JRAbstractBeanDataSourceProvider(Class beanClass) { 63 if (beanClass == null) 64 throw new NullPointerException ("beanClass must not be null"); 65 66 this.beanClass = beanClass; 67 } 68 69 72 public boolean supportsGetFieldsOperation() { 73 return true; 74 } 75 76 79 public JRField[] getFields(JasperReport report) throws JRException { 80 BeanInfo beanInfo = null; 81 82 try { 83 beanInfo = Introspector.getBeanInfo(beanClass); 84 } catch (IntrospectionException e) { 85 throw new JRException(e); 86 } 87 88 PropertyDescriptor [] descriptors = beanInfo.getPropertyDescriptors(); 89 if(descriptors != null) { 90 ArrayList fields = new ArrayList (descriptors.length); 91 92 for (int i = 0; i < descriptors.length; i++) { 93 PropertyDescriptor descriptor = descriptors[i]; 94 95 if (!(descriptor instanceof IndexedPropertyDescriptor ) && descriptor.getReadMethod() != null) { 96 JRDesignField field = new JRDesignField(); 97 field.setValueClassName(normalizeClass(descriptor.getPropertyType()).getName()); 98 field.setName(descriptor.getName()); 99 100 fields.add(field); 101 } 102 } 103 104 return (JRField[]) fields.toArray(new JRField[fields.size()]); 105 } 106 107 return new JRField[0]; 108 } 109 110 113 private static Class normalizeClass(Class clazz) { 114 if(clazz.isPrimitive()) { 115 if(clazz == boolean.class) 116 return Boolean .class; 117 if(clazz == byte.class) 118 return Byte .class; 119 if(clazz == char.class) 120 return Character .class; 121 if(clazz == short.class) 122 return Short .class; 123 if(clazz == int.class) 124 return Integer .class; 125 if(clazz == long.class) 126 return Long .class; 127 if(clazz == float.class) 128 return Float .class; 129 if(clazz == double.class) 130 return Double .class; 131 } 132 133 return clazz; 134 } 135 } 136 | Popular Tags |