KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > instantiation > BeanInstantiator


1 package org.jbpm.instantiation;
2
3 import java.lang.reflect.*;
4 import org.apache.commons.logging.*;
5 import org.dom4j.*;
6
7 public class BeanInstantiator extends FieldInstantiator implements Instantiator {
8   
9   protected void setPropertyValue(Class JavaDoc clazz, Object JavaDoc newInstance, String JavaDoc propertyName, Element propertyElement) {
10     try {
11       // create the setter method name from the property name
12
String JavaDoc setterMethodName = "set"+propertyName.substring(0,1).toUpperCase()+propertyName.substring(1);
13       
14       // find the setter method
15
Method[] methods = clazz.getDeclaredMethods();
16       Method method = null;
17       Class JavaDoc propertyType = null;
18       for( int i=0; ( (i<methods.length)
19                       && (method==null) ); i++) {
20         if ( (setterMethodName.equals(methods[i].getName()))
21              && (methods[i].getParameterTypes()!=null)
22              && (methods[i].getParameterTypes().length==1) ) {
23           method = methods[i];
24           propertyType = methods[i].getParameterTypes()[0];
25         }
26       }
27       
28       // if the setter method was found
29
if (method!=null) {
30         // make it accessible
31
method.setAccessible(true);
32         // invoke it
33
method.invoke(newInstance, new Object JavaDoc[]{ getValue(propertyType, propertyElement) });
34       } else {
35         log.error( "couldn't set property '"+propertyName+"' to value '"+propertyElement.asXML()+"'" );
36       }
37     } catch (Exception JavaDoc e) {
38       log.error( "couldn't parse property '"+propertyName+"' to value '"+propertyElement.asXML()+"'", e );
39     }
40   }
41   
42   private static final Log log = LogFactory.getLog(BeanInstantiator.class);
43 }
44
45
Popular Tags