1 46 47 package org.jfree.xml.factory.objects; 48 49 import java.lang.reflect.Method ; 50 import java.lang.reflect.Modifier ; 51 import java.util.Iterator ; 52 import java.util.TreeSet ; 53 import java.util.HashMap ; 54 import java.beans.Introspector ; 55 import java.beans.IntrospectionException ; 56 import java.beans.BeanInfo ; 57 import java.beans.PropertyDescriptor ; 58 import java.io.ObjectInputStream ; 59 import java.io.IOException ; 60 61 import org.jfree.util.Log; 62 63 70 public class BeanObjectDescription extends AbstractObjectDescription { 71 72 private TreeSet ignoredParameters; 73 private transient HashMap properties; 74 75 80 public BeanObjectDescription(final Class className) { 81 this(className, true); 82 } 83 84 92 public BeanObjectDescription(final Class className, final boolean init) { 93 super(className); 94 this.ignoredParameters = new TreeSet (); 96 readBeanDescription(className, init); 97 } 98 99 private boolean isValidMethod (final Method method, final int parCount) 100 { 101 if (method == null) { 102 return false; 103 } 104 if (!Modifier.isPublic(method.getModifiers())) { 105 return false; 106 } 107 if (Modifier.isStatic(method.getModifiers())) { 108 return false; 109 } 110 if (method.getParameterTypes().length != parCount) { 111 return false; 112 } 113 return true; 114 } 115 116 121 public Object createObject() { 122 try { 123 final Object o = getObjectClass().newInstance(); 124 126 final Iterator it = getParameterNames(); 127 while (it.hasNext()) { 128 final String name = (String ) it.next(); 129 130 if (isParameterIgnored(name)) { 131 continue; 132 } 133 134 final Method method = findSetMethod(name); 135 final Object parameterValue = getParameter(name); 136 if (parameterValue == null) { 137 } 139 else { 140 method.invoke(o, new Object []{parameterValue}); 141 } 142 } 143 return o; 144 } 145 catch (Exception e) { 146 Log.error("Unable to invoke bean method", e); 147 } 148 return null; 149 } 150 151 158 private Method findSetMethod(final String parameterName) { 159 final PropertyDescriptor descriptor 160 = (PropertyDescriptor ) this.properties.get(parameterName); 161 return descriptor.getWriteMethod(); 162 } 163 164 170 private Method findGetMethod(final String parameterName) { 171 final PropertyDescriptor descriptor 172 = (PropertyDescriptor ) this.properties.get(parameterName); 173 return descriptor.getReadMethod(); 174 } 175 176 183 public void setParameterFromObject(final Object o) 184 throws ObjectFactoryException { 185 if (o == null) { 186 throw new NullPointerException ("Given object is null"); 187 } 188 final Class c = getObjectClass(); 189 if (!c.isInstance(o)) { 190 throw new ObjectFactoryException("Object is no instance of " + c 191 + "(is " + o.getClass() + ")"); 192 } 193 194 final Iterator it = getParameterNames(); 195 while (it.hasNext()) { 196 final String propertyName = (String ) it.next(); 197 198 if (isParameterIgnored(propertyName)) { 199 continue; 200 } 201 202 try { 203 final Method method = findGetMethod(propertyName); 204 final Object retval = method.invoke(o, (Object []) null); 205 if (retval != null) { 206 setParameter(propertyName, retval); 207 } 208 } 209 catch (Exception e) { 210 Log.info("Exception on method invokation.", e); 211 } 212 213 } 214 } 215 216 221 protected void ignoreParameter(final String parameter) { 222 this.ignoredParameters.add (parameter); 223 } 224 225 233 protected boolean isParameterIgnored (final String parameter) { 234 return this.ignoredParameters.contains(parameter); 235 } 236 237 private void readObject(final ObjectInputStream in) 238 throws IOException , ClassNotFoundException { 239 in.defaultReadObject(); 240 readBeanDescription(getObjectClass(), false); 241 } 242 243 private void readBeanDescription(final Class className, final boolean init) { 244 try { 245 this.properties = new HashMap (); 246 247 final BeanInfo bi = Introspector.getBeanInfo(className); 248 final PropertyDescriptor [] propertyDescriptors 249 = bi.getPropertyDescriptors(); 250 for (int i = 0; i < propertyDescriptors.length; i++) 251 { 252 final PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; 253 final Method readMethod = propertyDescriptor.getReadMethod(); 254 final Method writeMethod = propertyDescriptor.getWriteMethod(); 255 if (isValidMethod(readMethod, 0) && isValidMethod(writeMethod, 1)) 256 { 257 final String name = propertyDescriptor.getName(); 258 this.properties.put(name, propertyDescriptor); 259 if (init) { 260 super.setParameterDefinition(name, 261 propertyDescriptor.getPropertyType()); 262 } 263 } 264 } 265 } 266 catch (IntrospectionException e) { 267 Log.error ("Unable to build bean description", e); 268 } 269 } 270 } 271 | Popular Tags |