private static Map getMap(Element mapElement, Map map) { Class keyClass = classForAttributeValue(mapElement, "key-type"); Class valueClass = classForAttributeValue(mapElement, "value-type"); for (Iterator iter = mapElement.elementIterator(); iter.hasNext();) { Element element = (Element) iter.next(); Element keyElement = element.element("key"); Element valueElement = element.element("value"); map.put(getValue(keyClass, keyElement), getValue(valueClass, valueElement)); } return map; }
public Object instantiate(Class clazz, String configuration) { // create a new instance with the default constructor Object newInstance = newInstance(clazz); if (configuration != null && configuration.length() > 0) { // parse the bean configuration Element configurationElement = parseConfiguration(configuration); // loop over the configured properties for (Iterator iter = configurationElement.elementIterator(); iter.hasNext();) { Element propertyElement = (Element) iter.next(); String propertyName = propertyElement.getName(); setPropertyValue(clazz, newInstance, propertyName, propertyElement); } } return newInstance; }
protected void setPropertyValue(Class clazz, Object instance, String propertyName, Element propertyElement) { Field field = findField(clazz, propertyName); if (field != null) { field.setAccessible(true); Object value = getValue(field.getType(), propertyElement); try { field.set(instance, value); } catch (IllegalArgumentException e) { log.error("could not set '" + propertyName + "' to: " + value, e); } catch (IllegalAccessException e) { log.error(getClass() + " has no access to " + field, e); } } else { log.error("field not found: " + propertyName); } }
value = getCollection(propertyElement, new ArrayList()); value = getCollection(propertyElement, new HashSet()); value = getCollection(propertyElement, new TreeSet()); value = getMap(propertyElement, new HashMap()); value = getMap(propertyElement, new TreeMap()); try { if (Collection.class.isAssignableFrom(type)) { value = getCollection(propertyElement, (Collection) type.newInstance()); value = getMap(propertyElement, (Map) type.newInstance());
private static Map createInstantiatorCache() { Map instantiators = new HashMap(); instantiators.put(null, new FieldInstantiator()); instantiators.put("field", new FieldInstantiator()); instantiators.put("bean", new BeanInstantiator()); instantiators.put("constructor", new ConstructorInstantiator()); instantiators.put("configuration-property", new ConfigurationPropertyInstantiator()); instantiators.put("xml", new XmlInstantiator()); return instantiators; }
protected void setPropertyValue(Class clazz, Object instance, String propertyName, Element propertyElement) { Field field = findField(clazz, propertyName); if (field != null) { field.setAccessible(true); Object value = getValue(field.getType(), propertyElement); try { field.set(instance, value); } catch (IllegalArgumentException e) { log.error("could not set '" + propertyName + "' to: " + value, e); } catch (IllegalAccessException e) { log.error(getClass() + " has no access to " + field, e); } } else { log.error("field not found: " + propertyName); } }
value = getCollection(propertyElement, new ArrayList()); value = getCollection(propertyElement, new HashSet()); value = getCollection(propertyElement, new TreeSet()); value = getMap(propertyElement, new HashMap()); value = getMap(propertyElement, new TreeMap()); try { if (Collection.class.isAssignableFrom(type)) { value = getCollection(propertyElement, (Collection) type.newInstance()); value = getMap(propertyElement, (Map) type.newInstance());
private static Map createInstantiatorCache() { Map instantiators = new HashMap(); instantiators.put(null, new FieldInstantiator()); instantiators.put("field", new FieldInstantiator()); instantiators.put("bean", new BeanInstantiator()); instantiators.put("constructor", new ConstructorInstantiator()); instantiators.put("configuration-property", new ConfigurationPropertyInstantiator()); instantiators.put("xml", new XmlInstantiator()); return instantiators; }
public Object instantiate(Class clazz, String configuration) { // create a new instance with the default constructor Object newInstance = newInstance(clazz); if (configuration != null && configuration.length() > 0) { // parse the bean configuration Element configurationElement = parseConfiguration(configuration); // loop over the configured properties for (Iterator iter = configurationElement.elementIterator(); iter.hasNext();) { Element propertyElement = (Element) iter.next(); String propertyName = propertyElement.getName(); setPropertyValue(clazz, newInstance, propertyName, propertyElement); } } return newInstance; }
private static Map getMap(Element mapElement, Map map) { Class keyClass = classForAttributeValue(mapElement, "key-type"); Class valueClass = classForAttributeValue(mapElement, "value-type"); for (Iterator iter = mapElement.elementIterator(); iter.hasNext();) { Element element = (Element) iter.next(); Element keyElement = element.element("key"); Element valueElement = element.element("value"); map.put(getValue(keyClass, keyElement), getValue(valueClass, valueElement)); } return map; }
private static Object getCollection(Element collectionElement, Collection collection) { Class elementClass = classForAttributeValue(collectionElement, "element-type"); for (Iterator iter = collectionElement.elementIterator(); iter.hasNext();) { Element element = (Element) iter.next(); collection.add(getValue(elementClass, element)); } return collection; }
private static Object getCollection(Element collectionElement, Collection collection) { Class elementClass = classForAttributeValue(collectionElement, "element-type"); for (Iterator iter = collectionElement.elementIterator(); iter.hasNext();) { Element element = (Element) iter.next(); collection.add(getValue(elementClass, element)); } return collection; }