List of usage examples for org.apache.commons.beanutils DynaClass getDynaProperty
public DynaProperty getDynaProperty(String name);
null
. From source file:com.xpfriend.fixture.cast.temp.PojoUtil.java
private static String find(String name, Object bean) { if (Strings.isEmpty(name)) { return name; }//from w w w. j a v a 2 s . c om if (bean instanceof DynaBean) { DynaClass dynaClass = ((DynaBean) bean).getDynaClass(); if (dynaClass.getDynaProperty(name) != null) { return name; } return PropertyNames.find(dynaClass, name); } Class<?> beanClass = bean.getClass(); PropertyNames propertyNames = propertyNamesMap.get(beanClass.getName()); if (propertyNames == null) { PropertyUtilsBean propertyUtil = TypeConverter.getBeanUtils().getPropertyUtils(); propertyNames = new PropertyNames(propertyUtil, beanClass); } return propertyNames.find(name); }
From source file:es.caib.zkib.jxpath.ri.model.dynabeans.DynaBeanPropertyPointer.java
/** * Returns true if the bean has the currently selected property. * @return boolean//from ww w . ja v a 2s.c o m */ protected boolean isActualProperty() { DynaClass dynaClass = dynaBean.getDynaClass(); return dynaClass.getDynaProperty(getPropertyName()) != null; }
From source file:es.caib.zkib.jxpath.ri.model.dynabeans.DynaBeanPropertyPointer.java
/** * Learn whether the property referenced is an indexed property. * @return boolean/*from w w w . j av a2 s. co m*/ */ protected boolean isIndexedProperty() { DynaClass dynaClass = dynaBean.getDynaClass(); DynaProperty property = dynaClass.getDynaProperty(name); return property.isIndexed(); }
From source file:es.caib.zkib.jxpath.ri.model.dynabeans.DynaBeanPropertyPointer.java
/** * Convert a value to the appropriate property type. * @param value to convert//w w w .j a v a2 s.c om * @param element whether this should be a collection element. * @return conversion result */ private Object convert(Object value, boolean element) { DynaClass dynaClass = (DynaClass) dynaBean.getDynaClass(); DynaProperty property = dynaClass.getDynaProperty(getPropertyName()); Class type = property.getType(); if (element) { if (type.isArray()) { type = type.getComponentType(); } else { return value; // No need to convert } } try { return TypeUtils.convert(value, type); } catch (Exception ex) { String string = value == null ? "null" : value.getClass().getName(); throw new JXPathTypeConversionException("Cannot convert value of class " + string + " to type " + type, ex); } }
From source file:es.caib.zkib.jxpath.ri.model.dynabeans.DynaBeanPropertyPointer.java
public String[] getPropertyNames() { /* @todo do something about the sorting - LIKE WHAT? - MJB */ if (names == null) { DynaClass dynaClass = dynaBean.getDynaClass(); DynaProperty[] properties = dynaClass.getDynaProperties(); int count = properties.length; boolean hasClass = dynaClass.getDynaProperty("class") != null; if (hasClass) { count--; // Exclude "class" from properties }/* w w w. j a v a 2s. c o m*/ names = new String[count]; for (int i = 0, j = 0; i < properties.length; i++) { String name = properties[i].getName(); if (!hasClass || !name.equals("class")) { names[j++] = name; } } Arrays.sort(names); } return names; }
From source file:net.sf.json.util.DynaBeanToBeanMorpher.java
/** * DOCUMENT ME!/*w w w .j a v a 2s . c o m*/ * * @param value DOCUMENT ME! * * @return DOCUMENT ME! */ public Object morph(Object value) { if (value == null) { return null; } if (!supports(value.getClass())) { throw new MorphException("value is not a DynaBean"); } Object bean = null; try { bean = beanClass.newInstance(); DynaBean dynaBean = (DynaBean) value; DynaClass dynaClass = dynaBean.getDynaClass(); PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(beanClass); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; String name = pd.getName(); DynaProperty dynaProperty = dynaClass.getDynaProperty(name); if (dynaProperty != null) { Class dynaType = dynaProperty.getType(); Class type = pd.getPropertyType(); if (type.isAssignableFrom(dynaType)) { PropertyUtils.setProperty(bean, name, dynaBean.get(name)); } else { if (IdentityObjectMorpher.getInstance() == morpherRegistry.getMorpherFor(type)) { throw new MorphException("Can't find a morpher for target class " + type); } else { PropertyUtils.setProperty(bean, name, morpherRegistry.morph(type, dynaBean.get(name))); } } } } } catch (InstantiationException e) { throw new MorphException(e); } catch (IllegalAccessException e) { throw new MorphException(e); } catch (InvocationTargetException e) { throw new MorphException(e); } catch (NoSuchMethodException e) { throw new MorphException(e); } return bean; }
From source file:br.com.kproj.salesman.infrastructure.helpers.BeanUtils.java
@SuppressWarnings({ "rawtypes", "unchecked" }) public void copyProperty(Object dest, String fieldName, Object value, Identifiable orig) throws IllegalAccessException, InvocationTargetException { if (log.isTraceEnabled()) { createLogCopyProperty(dest, fieldName, value); }/*from w ww . j av a 2s . c om*/ if (!orig.getFields().contains(fieldName)) { return; } Object target = dest; int delim = fieldName.lastIndexOf(46); if (delim >= 0) { try { target = getPropertyUtils().getProperty(dest, fieldName.substring(0, delim)); } catch (NoSuchMethodException e) { return; } fieldName = fieldName.substring(delim + 1); if (log.isTraceEnabled()) { log.trace(" Target bean = " + target); log.trace(" Target name = " + fieldName); } } String propName = null; Class type = null; int index = -1; String key = null; propName = fieldName; int i = propName.indexOf(91); if (i >= 0) { int k = propName.indexOf(93); try { index = Integer.parseInt(propName.substring(i + 1, k)); } catch (NumberFormatException e) { } propName = propName.substring(0, i); } int j = propName.indexOf(40); if (j >= 0) { int k = propName.indexOf(41); try { key = propName.substring(j + 1, k); } catch (IndexOutOfBoundsException e) { } propName = propName.substring(0, j); } if (target instanceof DynaBean) { DynaClass dynaClass = ((DynaBean) target).getDynaClass(); DynaProperty dynaProperty = dynaClass.getDynaProperty(propName); if (dynaProperty == null) { return; } type = dynaProperty.getType(); } else { PropertyDescriptor descriptor = null; try { descriptor = getPropertyUtils().getPropertyDescriptor(target, fieldName); if (descriptor == null) return; } catch (NoSuchMethodException e) { return; } type = descriptor.getPropertyType(); if (type == null) { if (log.isTraceEnabled()) { log.trace(" target type for property '" + propName + "' is null, so skipping ths setter"); } return; } } if (log.isTraceEnabled()) { log.trace(" target propName=" + propName + ", type=" + type + ", index=" + index + ", key=" + key); } if (index >= 0) { Converter converter = getConvertUtils().lookup(type.getComponentType()); if (converter != null) { log.trace(" USING CONVERTER " + converter); value = converter.convert(type, value); } try { getPropertyUtils().setIndexedProperty(target, propName, index, value); } catch (NoSuchMethodException e) { throw new InvocationTargetException(e, "Cannot set " + propName); } } else if (key != null) { try { getPropertyUtils().setMappedProperty(target, propName, key, value); } catch (NoSuchMethodException e) { throw new InvocationTargetException(e, "Cannot set " + propName); } } else { Converter converter = getConvertUtils().lookup(type); if (converter != null && value != null) { log.trace(" USING CONVERTER " + converter); value = converter.convert(type, value); } try { if (value instanceof Map) { Map mapDest = (Map) getPropertyUtils().getSimpleProperty(dest, fieldName); if (mapDest != null) { Map mapOrig = (Map) value; Set entrySet = mapOrig.entrySet(); for (Object object : entrySet) { Entry entryOrig = (Entry) object; if (entryOrig.getValue() instanceof Identifiable) { if (mapDest.containsKey(entryOrig.getKey())) { Object destValue = mapDest.get(entryOrig.getKey()); new BeanUtils().copyProperties(destValue, entryOrig.getValue()); } else { mapDest.put(entryOrig.getKey(), entryOrig.getValue()); } } else { mapDest.put(entryOrig.getKey(), entryOrig.getValue()); } } } else { getPropertyUtils().setSimpleProperty(target, propName, value); } } else { getPropertyUtils().setSimpleProperty(target, propName, value); } } catch (NoSuchMethodException e) { throw new InvocationTargetException(e, "Cannot set " + propName); } } }
From source file:com.xpfriend.fixture.cast.temp.Database.java
private Class<?> getColumnType(String columnName, DynaClass metaData, TempDynaSet table) { DynaProperty column = metaData.getDynaProperty(columnName); if (column != null) { return column.getType(); }/*from w w w .j av a2s . c o m*/ return table.getColumn(columnName).getType(); }
From source file:nl.strohalm.cyclos.utils.binding.CustomBeanUtilsBean.java
@Override public void setProperty(final Object bean, String name, final Object value) throws IllegalAccessException, InvocationTargetException { // Resolve any nested expression to get the actual target bean Object target = bean;//from ww w . ja va 2s . c o m final int delim = findLastNestedIndex(name); if (delim >= 0) { try { target = getPropertyUtils().getProperty(bean, name.substring(0, delim)); } catch (final NoSuchMethodException e) { return; // Skip this property setter } name = name.substring(delim + 1); } // Declare local variables we will require String propName = null; // Simple name of target property Class<?> type = null; // Java type of target property int index = -1; // Indexed subscript value (if any) String key = null; // Mapped key value (if any) // Calculate the property name, index, and key values propName = name; final int i = propName.indexOf(PropertyUtils.INDEXED_DELIM); if (i >= 0) { final int k = propName.indexOf(PropertyUtils.INDEXED_DELIM2); try { index = Integer.parseInt(propName.substring(i + 1, k)); } catch (final NumberFormatException e) { // Ignore } propName = propName.substring(0, i); } final int j = propName.indexOf(PropertyUtils.MAPPED_DELIM); if (j >= 0) { final int k = propName.indexOf(PropertyUtils.MAPPED_DELIM2); try { key = propName.substring(j + 1, k); } catch (final IndexOutOfBoundsException e) { // Ignore } propName = propName.substring(0, j); } // Calculate the property type if (target instanceof DynaBean) { final DynaClass dynaClass = ((DynaBean) target).getDynaClass(); final DynaProperty dynaProperty = dynaClass.getDynaProperty(propName); if (dynaProperty == null) { return; // Skip this property setter } type = dynaProperty.getType(); if (type.isArray() || Collection.class.isAssignableFrom(type)) { type = Object[].class; } } else { PropertyDescriptor descriptor = null; try { descriptor = getPropertyUtils().getPropertyDescriptor(target, name); if (descriptor == null) { return; // Skip this property setter } } catch (final NoSuchMethodException e) { return; // Skip this property setter } if (descriptor instanceof MappedPropertyDescriptor) { if (((MappedPropertyDescriptor) descriptor).getMappedWriteMethod() == null) { return; // Read-only, skip this property setter } type = ((MappedPropertyDescriptor) descriptor).getMappedPropertyType(); /** * Overriden behaviour ------------------- When a type is Object on a mapped property, retrieve the value to check if it's an array */ if (Object.class.equals(type)) { try { final Object retrieved = getPropertyUtils().getMappedProperty(target, propName, key); if (retrieved != null) { final Class<?> retrievedType = retrieved.getClass(); if (retrievedType.isArray() || Collection.class.isAssignableFrom(retrievedType)) { type = Object[].class; } } } catch (final NoSuchMethodException e) { throw new PropertyException(target, propName + "(" + key + ")"); } } } else if (descriptor instanceof IndexedPropertyDescriptor) { if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null) { return; // Read-only, skip this property setter } type = ((IndexedPropertyDescriptor) descriptor).getIndexedPropertyType(); } else { if (descriptor.getWriteMethod() == null) { return; // Read-only, skip this property setter } type = descriptor.getPropertyType(); } } /** * Overriden behaviour ------------------- When a type is Map on a mapped property, retrieve the value to check if it's an array */ if (Map.class.isAssignableFrom(type) && StringUtils.isNotEmpty(key)) { try { final Map<?, ?> map = (Map<?, ?>) getPropertyUtils().getProperty(target, propName); final Object retrieved = map.get(key); if (retrieved != null) { final Class<?> retrievedType = retrieved.getClass(); if (retrievedType.isArray() || Collection.class.isAssignableFrom(retrievedType)) { type = Object[].class; } } } catch (final NoSuchMethodException e) { throw new PropertyException(target, propName + "(" + key + ")"); } } // Convert the specified value to the required type Object newValue = null; if (type.isArray() && (index < 0)) { // Scalar value into array if (value == null) { final String values[] = new String[1]; values[0] = (String) value; newValue = getConvertUtils().convert(values, type); } else if (value instanceof String) { final String values[] = new String[1]; values[0] = (String) value; newValue = getConvertUtils().convert(values, type); } else if (value instanceof String[]) { newValue = getConvertUtils().convert((String[]) value, type); } else { newValue = value; } } else if (type.isArray()) { // Indexed value into array if (value instanceof String) { newValue = getConvertUtils().convert((String) value, type.getComponentType()); } else if (value instanceof String[]) { newValue = getConvertUtils().convert(((String[]) value)[0], type.getComponentType()); } else { newValue = value; } } else { // Value into scalar if ((value instanceof String) || (value == null)) { newValue = getConvertUtils().convert((String) value, type); } else if (value instanceof String[]) { newValue = getConvertUtils().convert(((String[]) value)[0], type); } else if (getConvertUtils().lookup(value.getClass()) != null) { newValue = getConvertUtils().convert(value.toString(), type); } else { newValue = value; } } // Invoke the setter method try { if (index >= 0) { getPropertyUtils().setIndexedProperty(target, propName, index, newValue); } else if (key != null) { getPropertyUtils().setMappedProperty(target, propName, key, newValue); } else { getPropertyUtils().setProperty(target, propName, newValue); } } catch (final NoSuchMethodException e) { throw new InvocationTargetException(e, "Cannot set " + propName); } }
From source file:org.apache.action2.legacy.DynaBeanPropertyAccessor.java
public Object getProperty(Map context, Object target, Object name) throws OgnlException { if (target instanceof DynaBean && name != null) { DynaBean bean = (DynaBean) target; DynaClass cls = bean.getDynaClass(); String key = name.toString(); if (cls.getDynaProperty(key) != null) { return bean.get(key); }/*from w w w . j av a2 s . c om*/ } return null; }