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

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

Introduction

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

Prototype

public static boolean isWriteable(Object bean, String name) 

Source Link

Document

Return true if the specified property name identifies a writeable property on the specified bean; otherwise, return false.

For more details see PropertyUtilsBean.

Usage

From source file:com.eviware.soapui.monitor.PropertySupport.java

public static void applySystemProperties(Object target, String scope, ModelItem modelItem) {
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(target);
    DefaultPropertyExpansionContext context = new DefaultPropertyExpansionContext(modelItem);
    Properties properties = System.getProperties();

    for (PropertyDescriptor descriptor : descriptors) {
        String name = descriptor.getName();
        String key = scope + "." + name;
        if (PropertyUtils.isWriteable(target, name) && properties.containsKey(key)) {
            try {
                String value = context.expand(String.valueOf(properties.get(key)));
                BeanUtils.setProperty(target, name, value);
                SoapUI.log.info("Set property [" + name + "] to [" + value + "] in scope [" + scope + "]");
            } catch (Throwable e) {
                SoapUI.logError(e);//from w  w w.  j a  va  2 s. c o m
            }
        }
    }
}

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 a 2 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");
    }//from ww  w. j  a v a 2  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 (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");
    }//from  w  w  w .  j a  v  a  2 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:de.iritgo.simplelife.bean.BeanTools.java

/**
 * Copy all attributes from the given map to the given bean
 * //from  w  w  w  .j  a  v a  2  s .c o m
 * @param map The map The map
 * @param object The bean The bean
 */
static public void copyMap2Bean(Map<String, Object> map, Object object) {
    for (String name : map.keySet()) {
        if (PropertyUtils.isWriteable(object, name)) {
            try {
                PropertyUtils.setSimpleProperty(object, name, map.get(name));
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:ca.sqlpower.architect.TestUtils.java

/**
 * Sets all the settable properties on the given target object
 * which are not in the given ignore set.
 * /*from   w  w w .  j  a v  a  2  s.c om*/
 * @param target The object to change the properties of
 * @param propertiesToIgnore The properties of target not to modify or read
 * @return A Map describing the new values of all the non-ignored, readable 
 * properties in target.
 */
public static Map<String, Object> setAllInterestingProperties(Object target, Set<String> propertiesToIgnore)
        throws Exception {

    PropertyDescriptor props[] = PropertyUtils.getPropertyDescriptors(target);
    for (int i = 0; i < props.length; i++) {
        Object oldVal = null;
        if (PropertyUtils.isReadable(target, props[i].getName()) && props[i].getReadMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {
            oldVal = PropertyUtils.getProperty(target, props[i].getName());
        }
        if (PropertyUtils.isWriteable(target, props[i].getName()) && props[i].getWriteMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {

            NewValueMaker valueMaker = new ArchitectValueMaker(new SPObjectRoot());
            Object newVal = valueMaker.makeNewValue(props[i].getPropertyType(), oldVal, props[i].getName());

            System.out.println("Changing property \"" + props[i].getName() + "\" to \"" + newVal + "\"");
            PropertyUtils.setProperty(target, props[i].getName(), newVal);

        }
    }

    // read them all back at the end in case there were dependencies between properties
    return ca.sqlpower.testutil.TestUtils.getAllInterestingProperties(target, propertiesToIgnore);
}

From source file:edu.umn.msi.tropix.common.test.BeanTest.java

public static void testBeanProperties(final Object testBean, final HashMultimap<Class<?>, Object> typeObjects) {
    try {/*  w  ww .  j av a2s .co  m*/
        @SuppressWarnings("unchecked")
        final Map<String, ?> propertyMap = PropertyUtils.describe(testBean);
        for (final String propertyName : propertyMap.keySet()) {
            if (propertyName.equals("class") || !PropertyUtils.isWriteable(testBean, propertyName)) {
                continue;
            }
            final Class<?> type = PropertyUtils.getPropertyType(testBean, propertyName);
            Collection<?> objects = null;
            for (final Class<?> typeQuery : typeObjects.keySet()) {
                if (typeQuery.isAssignableFrom(type)) {
                    objects = typeObjects.get(typeQuery);
                }
            }
            boolean useEquals = true;
            if (objects == null) {
                useEquals = false;
                try {
                    objects = Lists.<Object>newArrayList(EasyMock.createMock(type));
                } catch (final Exception e) {
                    // Cannot instantiate mock of this type
                    continue;
                }
            }
            for (final Object expectedObject : objects) {
                PropertyUtils.setProperty(testBean, propertyName, expectedObject);
                final Object object = PropertyUtils.getProperty(testBean, propertyName);
                if (useEquals) {
                    assert object.equals(expectedObject) : "Expected " + expectedObject + " obtained " + object;
                } else {
                    assert object == expectedObject : "Expected " + expectedObject + " obtained " + object;
                }
            }
        }
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:io.github.moosbusch.lumpi.gui.form.editor.io.spi.ListButtonStoreValueDelegate.java

@SuppressWarnings("unchecked")
@Override/*www.jav  a 2s.c om*/
public void storeValue(Object context) {
    if (context != null) {
        ListButton listButton = getFormEditor().getComponent();
        String propertyName = listButton.getListDataKey();
        ListView.ListDataBindMapping bindMapping = listButton.getListDataBindMapping();
        Object newPropertyValue = bindMapping.valueOf(listButton.getListData());

        if (PropertyUtils.isWriteable(context, propertyName)) {
            listButton.store(context);
        } else {
            Object oldPropertyValue = null;

            try {
                oldPropertyValue = PropertyUtils.getProperty(context, propertyName);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
                Logger.getLogger(AbstractDynamicForm.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if ((newPropertyValue != null) && (oldPropertyValue != null)) {
                    if ((newPropertyValue instanceof java.util.Collection)
                            && (oldPropertyValue instanceof java.util.Collection)) {
                        java.util.Collection<Object> newColl = (java.util.Collection<Object>) newPropertyValue;
                        java.util.Collection<Object> oldColl = (java.util.Collection<Object>) oldPropertyValue;

                        newColl.stream().filter((obj) -> (!oldColl.contains(obj))).forEach((obj) -> {
                            oldColl.add(obj);
                        });
                    } else if ((newPropertyValue instanceof Sequence)
                            && (oldPropertyValue instanceof Sequence)) {
                        Sequence<Object> newSeq = (Sequence<Object>) newPropertyValue;
                        Sequence<Object> oldSeq = (Sequence<Object>) oldPropertyValue;

                        for (int cnt = 0; cnt < newSeq.getLength(); cnt++) {
                            Object obj = newSeq.get(cnt);

                            if (oldSeq.indexOf(obj) == -1) {
                                oldSeq.add(obj);
                            }
                        }
                    } else if ((newPropertyValue instanceof org.apache.pivot.collections.Set)
                            && (oldPropertyValue instanceof org.apache.pivot.collections.Set)) {
                        org.apache.pivot.collections.Set<Object> newColl = (org.apache.pivot.collections.Set<Object>) newPropertyValue;
                        org.apache.pivot.collections.Set<Object> oldColl = (org.apache.pivot.collections.Set<Object>) oldPropertyValue;

                        for (Object obj : newColl) {
                            if (!oldColl.contains(obj)) {
                                oldColl.add(obj);
                            }
                        }
                    } else if ((ObjectUtils.isArray(newPropertyValue))
                            && (ObjectUtils.isArray(oldPropertyValue))) {
                        Object[] newArray = (Object[]) newPropertyValue;
                        Object[] oldArray = (Object[]) oldPropertyValue;

                        for (Object obj : newArray) {
                            if (!ArrayUtils.contains(oldArray, obj)) {
                                oldArray = ArrayUtils.add(oldArray, obj);
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:com.roadmap.common.util.ObjectUtil.java

/**
 * Return true if the specified property name identifies a writeable 
 * property on the specified bean; otherwise, return false.
 *
 * @param Object object to be examined/*from  w w  w.  j a  v a2s.co  m*/
 * @param String property name to be evaluated
 * 
 * @return boolean
 */
public static boolean isWriteable(Object vo, String prop) {
    boolean retVal = false;
    try {
        if (vo instanceof Map) {
            retVal = vo != null;
        } else {
            retVal = PropertyUtils.isWriteable(vo, prop);
        }
    } catch (Exception e) {
        //No need to handle
    }
    return retVal;
}

From source file:ca.sqlpower.testutil.TestUtils.java

/**
 * Sets all the settable properties on the given target object which are not
 * in the given ignore set./*from   w w  w .  j a  va 2  s. co  m*/
 * <p>
 * TODO merge this with what is in Architect's TestUtils class. This was
 * originally refactored out of there.
 * 
 * @param target
 *            The object to change the properties of
 * @param propertiesToIgnore
 *            The properties of target not to modify or read
 * @return A Map describing the new values of all the non-ignored, readable
 *         properties in target.
 */
public static Map<String, Object> setAllInterestingProperties(Object target, Set<String> propertiesToIgnore,
        NewValueMaker valueMaker) throws Exception {

    PropertyDescriptor props[] = PropertyUtils.getPropertyDescriptors(target);
    for (int i = 0; i < props.length; i++) {
        Object oldVal = null;
        if (PropertyUtils.isReadable(target, props[i].getName()) && props[i].getReadMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {
            oldVal = PropertyUtils.getProperty(target, props[i].getName());
        }
        if (PropertyUtils.isWriteable(target, props[i].getName()) && props[i].getWriteMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {

            Object newVal = valueMaker.makeNewValue(props[i].getPropertyType(), oldVal, props[i].getName());

            System.out.println("Changing property \"" + props[i].getName() + "\" to \"" + newVal + "\"");
            PropertyUtils.setProperty(target, props[i].getName(), newVal);

        }
    }

    // read them all back at the end in case there were dependencies between properties
    return TestUtils.getAllInterestingProperties(target, propertiesToIgnore);
}