Example usage for org.apache.commons.beanutils PropertyUtils getSimpleProperty

List of usage examples for org.apache.commons.beanutils PropertyUtils getSimpleProperty

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils getSimpleProperty.

Prototype

public static Object getSimpleProperty(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the value of the specified simple property of the specified bean, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:com.cubeia.firebase.test.common.rules.impl.MemberAssertFilter.java

public boolean accept(Object o) {
    try {/* w w  w .  j ava2s  .  c  om*/
        Object test = PropertyUtils.getSimpleProperty(o, member);
        return testValue(test);
    } catch (NoSuchMethodException e) {
        return checkDirectAccess(o);
    } catch (Exception e) {
        return handleError(e, o);
    }
}

From source file:io.kahu.hawaii.domain.FieldChecker.java

public boolean checkAllFieldsAreBlank(Object bean, String... fieldNames) throws ServerException {
    Assert.notNull(bean);/*from w w  w  .  ja v a2s .c  o  m*/
    boolean allBlank = true;
    for (String fieldName : fieldNames) {
        try {
            Object value = PropertyUtils.getSimpleProperty(bean, fieldName);
            boolean isBlank = isBlank(value);
            allBlank = allBlank && isBlank;
        } catch (Exception e) {
            throw new ServerException(ServerError.ILLEGAL_ARGUMENT,
                    "Field '" + fieldName + "' cannot be obtained from object '" + bean + "'.", e);
        }

    }
    return allBlank;
}

From source file:com.jythonui.server.RUtils.java

public static <T extends RMap> void toTProperties(String[] prop, T dest, Object sou) {
    for (String key : prop) {
        String val = null;
        try {//from  w  ww . jav a2s .c o m
            val = (String) PropertyUtils.getSimpleProperty(sou, key);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            errorMess(L(), IErrorCode.ERRORCODE112, ILogMess.BEANCANNOTGETPROPERTY, key);
        }
        dest.setAttr(key, val);
    }
}

From source file:com.yukthi.validators.MatchWithValidator.java

@Override
public boolean isValid(Object bean, Object fieldValue) {
    //obtain other field value
    Object otherValue = null;//  w  ww.j a  va2 s  .c  o  m

    try {
        otherValue = PropertyUtils.getSimpleProperty(bean, matchWithField);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
        throw new IllegalStateException("Invalid/inaccessible property \"" + matchWithField
                + "\" specified with matchWith validator in bean: " + bean.getClass().getName());
    }

    //if current field value is null
    if (fieldValue == null) {
        return (otherValue == null);
    }

    return fieldValue.equals(otherValue);
}

From source file:com.panemu.tiwulfx.control.DetailPanel.java

private void displayValues() {
    for (String prop : propertyNames) {
        try {/*from   w ww. java2s .  c  o  m*/
            Label lblValue = componentMap.get(prop);
            Object obj = PropertyUtils.getSimpleProperty(objectToDisplay, prop);
            if (obj != null) {
                /**
                 * There is a bug in Label control. When the text's length
                 * is 1 and the wrapText property is true, the label display
                 * nothing. Workaround, don't wrap is the text's length is 1
                 */
                if (obj.toString().length() == 1) {
                    lblValue.setWrapText(false);
                }

                lblValue.setText(obj.toString());
            } else {
                lblValue.setText("-- undefined --");
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new RuntimeException(ex.getMessage());
        }
    }
}

From source file:com.afeng.common.utils.reflection.MyBeanUtils.java

private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
        throw new IllegalArgumentException("No destination bean specified");
    }/* w w  w  . j a v a2  s .c o m*/
    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }

    // Copy the properties, converting as necessary
    if (orig instanceof DynaBean) {
        DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((DynaBean) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else if (orig instanceof Map) {
        Iterator names = ((Map) orig).keySet().iterator();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((Map) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else
    /* if (orig is a standard JavaBean) */
    {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            //              String type = origDescriptors[i].getPropertyType().toString();
            if ("class".equals(name)) {
                continue; // No point in trying to set an object's class
            }
            if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(orig, name);
                    copyProperty(dest, name, value);
                } catch (IllegalArgumentException ie) {
                    ; // Should not happen
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    }

}

From source file:com.eryansky.common.utils.reflection.MyBeanUtils.java

private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
        throw new IllegalArgumentException("No destination bean specified");
    }// w  ww.  ja  v  a2  s .c  om
    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }

    // Copy the properties, converting as necessary
    if (orig instanceof DynaBean) {
        DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((DynaBean) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else if (orig instanceof Map) {
        Iterator names = ((Map) orig).keySet().iterator();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((Map) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else
    /* if (orig is a standard JavaBean) */
    {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            //              String type = origDescriptors[i].getPropertyType().toString();
            if ("class".equals(name)) {
                continue; // No point in trying to set an object's class
            }
            if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(orig, name);
                    copyProperty(dest, name, value);
                } catch (java.lang.IllegalArgumentException ie) {
                    ; // Should not happen
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    }

}

From source file:com.eryansky.common.utils.reflection.BeanUtils.java

private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
        throw new IllegalArgumentException("No destination bean specified");
    }/*w w w.  j ava 2  s.co  m*/
    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }

    // Copy the properties, converting as necessary
    if (orig instanceof DynaBean) {
        DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((DynaBean) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else if (orig instanceof Map) {
        Iterator names = ((Map) orig).keySet().iterator();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((Map) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else
    /* if (orig is a standard JavaBean) */ {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            //              String type = origDescriptors[i].getPropertyType().toString();
            if ("class".equals(name)) {
                continue; // No point in trying to set an object's class
            }
            if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(orig, name);
                    copyProperty(dest, name, value);
                } catch (IllegalArgumentException ie) {
                    ; // Should not happen
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    }

}

From source file:com.erinors.hpb.server.handler.HibernateEmbeddedObjectHandler.java

@Override
public Object merge(MergingContext context, Object object) {
    if (object == null || !object.getClass().isAnnotationPresent(Embeddable.class)) {
        return null;
    }/*from   ww w  .java2s .  c om*/

    boolean empty = true;
    try {
        for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(object)) {
            String propertyName = pd.getName();

            if (!"class".equals(propertyName) && !pd.getReadMethod().isAnnotationPresent(Transient.class)) {
                if (PropertyUtils.getSimpleProperty(object, propertyName) != null) {
                    empty = false;
                    break;
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error while checking embedded object: " + object, e);
    }

    if (empty) {
        context.addProcessedObject(object, null);
        return ProcessedToNull;
    } else {
        return null;
    }
}

From source file:com.kuzumeji.framework.enterprise.component.persistence.bak.DefaultUniqueConstraintsListener.java

/**
 * {@inheritDoc}//from  ww w .j  av a  2 s  .c o m
 * <dl>
 * <dt>?
 * <dd>?????????
 * </dl>
 */
@Override
public Map<String, Object> filter(final P object) {
    final Map<String, Object> filter = new HashMap<>();
    for (final String field : fields) {
        try {
            filter.put(field, PropertyUtils.getSimpleProperty(object, field));
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new EnterpriseRuntimeException(e.getLocalizedMessage());
        }
    }
    return filter;
}