List of usage examples for org.apache.commons.beanutils PropertyUtils getProperty
public static Object getProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:io.lightlink.excel.WritingExcelStreamVisitor.java
private Object getPropertyValue(Object data, String property) { if (data instanceof Map && ((Map) data).containsKey(property)) return ((Map) data).get(property); int pos = property.indexOf("[]."); if (pos != -1) { String containerProperty = property.substring(0, pos + 2); String subProperty = property.substring(pos + 3); Object containerObj = getPropertyValue(data, containerProperty); return getPropertyValue(containerObj, subProperty); }//from w w w.j av a 2 s . co m try { return PropertyUtils.getProperty(data, property); } catch (Exception e) { LOG.error(e.toString(), e); return null; } }
From source file:com.yougou.merchant.api.common.MerchantLogTools.java
public static String builContractOperationNotes(ContractVo source, ContractVo target) throws Exception { if (target == null) { throw new NullPointerException("target"); }/*from w w w . java 2 s . c om*/ if (source == null) { return "??"; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : CONTRACT_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if (!ObjectUtils.equals(o1, o2)) { if (StringUtils.equals("clearingForm", entry.getKey())) { o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "" : ObjectUtils.equals(2, o1) ? "" : ObjectUtils.equals(3, o1) ? "?" : ObjectUtils.equals(4, o1) ? "" : ""; o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "" : ObjectUtils.equals(2, o2) ? "" : ObjectUtils.equals(3, o2) ? "?" : ObjectUtils.equals(4, o2) ? "" : ""; } sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } return sb.toString(); }
From source file:com.belle.yitiansystem.merchant.service.impl.MerchantOperationLogServiceImpl.java
@Override public String buildMerchantBasicDataOperationNotes(SupplierSp source, SupplierSp target) throws Exception { if (target == null) { throw new NullPointerException("target"); }/*from www .j a v a2 s.com*/ if (source == null) { return ""; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : BASIC_DATA_TRANSLATABLE_FIELDS.entrySet()) { Object o1 = PropertyUtils.getProperty(source, entry.getKey()); Object o2 = PropertyUtils.getProperty(target, entry.getKey()); if (!ObjectUtils.equals(o1, o2)) { if (StringUtils.equals("isInputYougouWarehouse", entry.getKey())) { CooperationModel[] cooperationModels = SupplierSp.CooperationModel.values(); o1 = cooperationModels[(Integer) o1].getDescription(); o2 = cooperationModels[(Integer) o2].getDescription(); } else if (StringUtils.equals("shipmentType", entry.getKey())) { o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "" : ""; o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "" : ""; } sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(), o1, o2, LINE_SEPARATOR)); } } return sb.toString(); }
From source file:com.erudika.para.core.ParaObjectUtils.java
/** * Returns a map of annotated fields of a domain object. Only annotated fields are returned. This method forms the * basis of an Object/Grid Mapper. It converts an object to a map of key/value pairs. That map can later be * persisted to a data store.//from w w w.j av a 2 s. c o m * <br> * If {@code convertNestedToJsonString} is true all field values that are objects (i.e. not primitive types or * wrappers) are converted to a JSON string otherwise they are left as they are and will be serialized as regular * JSON objects later (structure is preserved). Null is considered a primitive type. Transient fields and * serialVersionUID are skipped. * * @param <P> the object type * @param pojo the object to convert to a map * @param filter a filter annotation. fields that have it will be skipped * @param convertNestedToJsonString true if you want to flatten the nested objects to a JSON string. * @return a map of fields and their values */ public static <P extends ParaObject> Map<String, Object> getAnnotatedFields(P pojo, Class<? extends Annotation> filter, boolean convertNestedToJsonString) { HashMap<String, Object> map = new HashMap<String, Object>(); if (pojo == null) { return map; } try { List<Field> fields = getAllDeclaredFields(pojo.getClass()); // filter transient fields and those without annotations for (Field field : fields) { boolean dontSkip = ((filter == null) ? true : !field.isAnnotationPresent(filter)); if (field.isAnnotationPresent(Stored.class) && dontSkip) { String name = field.getName(); Object value = PropertyUtils.getProperty(pojo, name); if (!Utils.isBasicType(field.getType()) && convertNestedToJsonString) { value = getJsonWriterNoIdent().writeValueAsString(value); } map.put(name, value); } } } catch (Exception ex) { logger.error(null, ex); } return map; }
From source file:jp.terasoluna.fw.collector.util.ControlBreakChecker.java
/** * .<br>//www . ja v a 2 s . c om * keys??compareStrategies?????CompareStrategy??????.<br> * <ul> * <li>keys : compareStrategies = N : N (???N)?? * <ul> * <li>keys[i]?CompareStrategy?compareStrategies[i]</li> * </ul> * </li> * <li>keys : compareStrategies = N : 1?? * <ul> * <li>keys[i]?CompareStrategy?compareStrategies[0]</li> * </ul> * </li> * <li>keys : compareStrategies = N : M (N > M)?? * <ul> * <li>keys[i] (i < M)?CompareStrategy?compareStrategies[i]</li> * <li>keys[i] (i >= M)?CompareStrategy?null</li> * </ul> * </li> * </ul> * ?? {@link #equalsObjects(Object, Object, CompareStrategy)} ?. * @param current Object * @param other Object * @param compareStrategies CompareStrategy<?>[] * @param keys String... * @return true:?/false:??? * @see #equalsObjects(Object, Object, CompareStrategy) */ protected static boolean isBreakInternal(Object current, Object other, CompareStrategy<?>[] compareStrategies, String... keys) { // key?????null???false if (keys == null || keys.length == 0) { // ?? return false; } // ?null????not null???true if ((current != null && other == null) || (current == null && other != null)) { // return true; } if (other != null && current != null) { for (int keyIndex = 0; keyIndex < keys.length; keyIndex++) { String key = keys[keyIndex]; CompareStrategy<?> compareStrategy = null; if (compareStrategies != null) { if (compareStrategies.length == 1) { compareStrategy = compareStrategies[0]; } else if (keyIndex < compareStrategies.length) { compareStrategy = compareStrategies[keyIndex]; } } if (key != null && key.length() != 0) { Object currentValue = null; Object otherValue = null; // ?? try { currentValue = PropertyUtils.getProperty(current, key); } catch (Exception e) { logOutputPropNotFound(e, current, key); // ???? continue; } // ?? try { otherValue = PropertyUtils.getProperty(other, key); } catch (Exception e) { logOutputPropNotFound(e, other, key); // ???? continue; } // if (!equalsObjects(currentValue, otherValue, compareStrategy)) { return true; } } } } // ?? return false; }
From source file:com.zhuangjy.dao.ReflectionUtil.java
/** * @since 2.0.29/*from ww w.j a va2 s. c om*/ */ public static Object getPropertyValue(Object bean, String name) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { return PropertyUtils.getProperty(bean, name); }
From source file:net.kamhon.ieagle.util.VoUtil.java
public static boolean isPropertySameValue(Object obj, Object obj2, String propertiesName) { try {/* w w w . j a va2 s .c o m*/ Object objValue = PropertyUtils.getProperty(obj, propertiesName); Object objValue2 = PropertyUtils.getProperty(obj2, propertiesName); if (objValue == null && objValue2 == null) { return true; } else if (objValue == null || objValue2 == null) { return false; } else { return objValue.equals(objValue2); } } catch (Exception ex) { throw new DataException(ex); } }
From source file:com.swingtech.commons.util.ClassUtil.java
/** * Utility method that uses bean introspection to get a value for a property * for any given Bean. This allows you to dynamically get bean values by * specifying the the name of the property. This method will return an * object./* w ww . j a v a 2 s .c o m*/ * * For example: <blockquote> * * <pre> * Person person = . . . <font color="#339933">//(get a populated Person object from somewhere)</font> * String age = getBeanProperty(person, "age"); * </pre> * * </blockquote> Is the same as: <blockquote> * * <pre> * String age = person.getAge(); * </pre> * * </blockquote> * * @param pBean * - the bean which contains the data for which you want to get. * @param pPropertyName * - the bean property you want to get the data for. This should * follow all the JavaBean convention...i.e. if the property name * is "age", there should be a method "getAge()" on the bean. * @return Object - The value for the property on the bean. */ public static Object getBeanProperty(final Object pBean, final String pPropertyName) { Object vReturnObject = null; try { vReturnObject = PropertyUtils.getProperty(pBean, pPropertyName); } catch (final RuntimeException re) { // if it's a runtime exception, throw the original exception. This // way, it gets bubbled to the top. throw re; } catch (final Exception e) { throw new RuntimeException("Error occured trying to get property " + "name, '" + pPropertyName + "', from object of class, '" + pBean.getClass().getName() + "'. Error: " + e.getClass().getName() + ": " + e.getMessage(), e); } return vReturnObject; }
From source file:com.mycollab.aspect.AuditLogAspect.java
private Integer saveAuditLog(Class<?> targetCls, Object bean, String changeSet, String username, Integer sAccountId, Integer activityStreamId) { try {/* w w w . j a v a2 s. c om*/ Integer typeId = (Integer) PropertyUtils.getProperty(bean, "id"); AuditLog auditLog = new AuditLog(); auditLog.setPosteduser(username); auditLog.setModule(ClassInfoMap.getModule(targetCls)); auditLog.setType(ClassInfoMap.getType(targetCls)); auditLog.setTypeid(typeId); auditLog.setSaccountid(sAccountId); auditLog.setPosteddate(new GregorianCalendar().getTime()); auditLog.setChangeset(changeSet); auditLog.setObjectClass(bean.getClass().getName()); if (activityStreamId != null) { auditLog.setActivitylogid(activityStreamId); } return auditLogService.saveWithSession(auditLog, ""); } catch (Exception e) { LOG.error("Error when save audit for save action of service " + targetCls.getName() + "and bean: " + BeanUtility.printBeanObj(bean) + " and changeset is " + changeSet, e); return null; } }
From source file:corner.orm.tapestry.component.gain.GainPoint.java
/** * //from www . j ava 2 s . c o m */ private void delNullEntity() { String prop = this.getShowPropertys().replace(this.getPagePersistentId(), getPersistentId()); String entityPropertys[] = prop.split(","); int count = 0; Object obj = null; List nulllist = new ArrayList(); for (Object bean : getSaveOrUpdateEntitys()) { count = 0; for (String name : entityPropertys) { try { obj = PropertyUtils.getProperty(bean, name); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } if (obj == null || obj.toString() == "") { count++; } if (count == entityPropertys.length) { nulllist.add(bean); } } } for (Object bean : nulllist) { getSaveOrUpdateEntitys().remove(bean); } }