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

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

Introduction

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

Prototype

public static void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Set 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.

Usage

From source file:com.sammyun.entity.Member.java

/**
 * //from ww  w. j  a  v  a  2s.  c om
 */
@Transient
public void removeAttributeValue() {
    setGender(null);
    setBirth(null);
    setArea(null);
    setAddress(null);
    setZipCode(null);
    setPhone(null);
    setMobile(null);
    for (int i = 0; i < ATTRIBUTE_VALUE_PROPERTY_COUNT; i++) {
        String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + i;
        try {
            PropertyUtils.setProperty(this, propertyName, null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

From source file:gov.nih.nci.caarray.test.api.external.v1_0.grid.SearchApiTest.java

private <T extends AbstractCaArrayEntity> void testExampleProperty(ExampleSearchCriteria<T> xsc,
        String property, Object value, int count) throws Exception {
    T example = xsc.getExample();//from   w  w w . j av a  2 s.  c  o m
    logForSilverCompatibility(TEST_NAME,
            "testSearchByExample " + example.getClass().getName() + "." + property);
    if (property != null) {
        PropertyUtils.setProperty(example, property, value);
    }
    LimitOffset lo = new LimitOffset();
    ArrayList<T> all = new ArrayList<T>(count + 1);
    SearchResult<T> sr = gridClient.searchByExample(xsc, lo);
    all.addAll(sr.getResults());
    while (!sr.isFullResult()) {
        lo.setOffset(all.size());
        sr = gridClient.searchByExample(xsc, lo);
        all.addAll(sr.getResults());
    }
    assertEquals("count for search by " + property, count, all.size());

    if (property != null) {
        for (T t : sr.getResults()) {
            Object got = PropertyUtils.getProperty(t, property);
            assertTrue(property + " \nexpected :" + value + "\nbut was :" + got, verify(property, value, got));
        }
    } else {
        for (T t : sr.getResults()) {
            System.out.println(t.toString());
        }
    }
}

From source file:gov.nih.nci.caarray.test.api.external.v1_0.java.SearchServiceTest.java

private <T extends AbstractCaArrayEntity> void testExampleProperty(ExampleSearchCriteria<T> xsc,
        String property, Object value, int count) throws Exception {
    T example = xsc.getExample();/*www .  j a va 2 s. c om*/
    logForSilverCompatibility(TEST_NAME,
            "testSearchByExample " + example.getClass().getName() + "." + property);

    if (property != null) {
        PropertyUtils.setProperty(example, property, value);
    }
    LimitOffset lo = new LimitOffset();
    ArrayList<T> all = new ArrayList<T>(count + 1);
    SearchResult<T> sr = service.searchByExample(xsc, lo);
    all.addAll(sr.getResults());
    while (!sr.isFullResult()) {
        lo.setOffset(all.size());
        sr = service.searchByExample(xsc, lo);
        all.addAll(sr.getResults());
    }
    assertEquals("count for search by " + property, count, all.size());

    if (property != null) {
        for (T t : sr.getResults()) {
            Object got = PropertyUtils.getProperty(t, property);
            assertTrue(property + " \nexpected :" + value + "\nbut was :" + got, verify(property, value, got));
        }
    } else {
        for (T t : all) {
            System.out.println(t.toString());
        }
    }
}

From source file:com.jk.framework.util.FakeRunnable.java

/**
 * Sets the peoperty value.//from ww w .  j  ava  2  s .c  o  m
 *
 * @param source
 *            the source
 * @param fieldName
 *            the field name
 * @param value
 *            the value
 */
public static void setPeopertyValue(final Object source, final String fieldName, final Object value) {
    try {
        PropertyUtils.setProperty(source, fieldName, value);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        JKExceptionUtil.handle(e);
    }
}

From source file:com.dp2345.entity.Product.java

/**
 * ?/* ww  w .java 2  s.  co  m*/
 * 
 * @param attribute
 *            ?
 * @param value
 *            ?
 */
@Transient
public void setAttributeValue(Attribute attribute, String value) {
    if (attribute != null && attribute.getPropertyIndex() != null) {
        if (StringUtils.isEmpty(value)) {
            value = null;
        }
        if (value == null || (attribute.getOptions() != null && attribute.getOptions().contains(value))) {
            try {
                String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + attribute.getPropertyIndex();
                PropertyUtils.setProperty(this, propertyName, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.redhat.example.rules.unittest.CsvTestHelper.java

/**
 * set property//from  w  w w.j a  va2  s.  co m
 * @param obj target object
 * @param key property name
 * @param value value to set
 */
public static void setProperty(Object obj, String key, Object value) {
    try {
        PropertyUtils.setProperty(obj, key, value);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        e.printStackTrace();
        fail("fail to access setProperty(" + obj + ", " + key + ", " + value + ")");
    }
}

From source file:net.sourceforge.pmd.util.fxdesigner.util.beans.RestorePropertyVisitor.java

@Override
public void visit(SimpleBeanModelNode model, SettingsOwner target) {
    if (model == null) {
        return; // possibly it wasn't saved during the previous save cycle
    }//from  ww  w .  j  a  va2s  .co  m

    if (target == null) {
        throw new IllegalArgumentException();
    }

    if (target.getClass() != model.getNodeType()) {
        throw new IllegalArgumentException("Incorrect settings restoration target, expected "
                + model.getNodeType() + ", actual " + target.getClass());
    }

    Map<String, PropertyDescriptor> descriptors = Arrays.stream(PropertyUtils.getPropertyDescriptors(target))
            .filter(d -> d.getReadMethod() != null
                    && d.getReadMethod().isAnnotationPresent(PersistentProperty.class))
            .collect(Collectors.toMap(PropertyDescriptor::getName, d -> d));

    for (Entry<String, Object> saved : model.getSettingsValues().entrySet()) {
        if (descriptors.containsKey(saved.getKey())) {
            try {
                PropertyUtils.setProperty(target, saved.getKey(), saved.getValue());
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    for (BeanModelNodeSeq<?> seq : model.getSequenceProperties()) {
        this.visit(seq, target);
    }

    for (SettingsOwner child : target.getChildrenSettingsNodes()) {
        model.getChildrenByType().get(child.getClass()).accept(this, child);
    }
}

From source file:net.sourceforge.pmd.util.fxdesigner.util.beans.RestorePropertyVisitor.java

@Override
public void visit(BeanModelNodeSeq<?> model, SettingsOwner target) {
    if (model == null) {
        return; // possibly it wasn't saved during the previous save cycle
    }//w ww.  j av a2  s  .  co  m

    if (target == null) {
        throw new IllegalArgumentException();
    }

    Collection<SettingsOwner> container;
    try {
        @SuppressWarnings("unchecked")
        Collection<SettingsOwner> tmp = (Collection<SettingsOwner>) PropertyUtils.getProperty(target,
                model.getPropertyName());
        container = tmp;
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        e.printStackTrace();
        return;
    }

    Iterator<SettingsOwner> existingItems = container.iterator();
    Class<?> itemType = null;
    // use a buffer to avoid concurrent modification
    List<SettingsOwner> itemsToAdd = new ArrayList<>();

    for (SimpleBeanModelNode child : model.getChildrenNodes()) {
        SettingsOwner item;
        if (existingItems.hasNext()) {
            item = existingItems.next();
        } else {
            if (itemType == null) {
                itemType = child.getNodeType();
            }

            try {
                item = (SettingsOwner) itemType.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
                continue; // try hard
            }
        }

        child.accept(this, item);
        itemsToAdd.add(item);
    }

    container.addAll(itemsToAdd);

    try {
        PropertyUtils.setProperty(target, model.getPropertyName(), container);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        e.printStackTrace();
    }
}

From source file:net.sourceforge.vulcan.core.support.StateManagerImpl.java

private void updateProfileNameIfNecessary(PluginConfigDto buildToolConfig, PluginProfileDto profile) {
    final String projectConfigProfilePropertyName = profile.getProjectConfigProfilePropertyName();
    try {//from   w ww.  j a  v  a2s.  com
        final String projectSetting = (String) PropertyUtils.getProperty(buildToolConfig,
                projectConfigProfilePropertyName);

        if (profile.getOldName().equals(projectSetting)) {
            PropertyUtils.setProperty(buildToolConfig, projectConfigProfilePropertyName, profile.getName());
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:net.sourceforge.vulcan.web.struts.forms.PluginConfigForm.java

private void hidePassword(String propertyName) {
    try {/*from  ww w  .j  av a  2 s .co m*/
        final String password = (String) PropertyUtils.getProperty(this, propertyName);
        if (!StringUtils.isBlank(password)) {
            hiddenPasswords.put(propertyName, password);
            PropertyUtils.setProperty(this, propertyName, HIDDEN_PASSWORD_VALUE);
        }
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new RuntimeException(e);
    }
}