Example usage for java.beans PropertyDescriptor getWriteMethod

List of usage examples for java.beans PropertyDescriptor getWriteMethod

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor getWriteMethod.

Prototype

public synchronized Method getWriteMethod() 

Source Link

Document

Gets the method that should be used to write the property value.

Usage

From source file:com.amazonaws.services.dynamodbv2.datamodeling.DynamoDbPropertyMarshaller.java

public static <T extends Item> void setValue(final T item, final PropertyDescriptor propertyDescriptor,
        final AttributeValue attributeValue) {
    if (attributeValue != null) {
        final DynamoDBReflectorUtil reflector = new DynamoDBReflectorUtil();

        final Method writeMethod = propertyDescriptor.getWriteMethod();
        Object argument = null;//from ww  w  .  j a v  a2  s  . c  o m
        if (writeMethod != null) {
            try {
                final ArgumentUnmarshaller unmarshaller = reflector.getArgumentUnmarshaller(item,
                        propertyDescriptor.getReadMethod(), writeMethod, null);
                argument = unmarshaller.unmarshall(attributeValue);
            } catch (final DynamoDBMappingException | ParseException mappingException) {
                try {
                    final JsonParser jsonParser = jsonFactory
                            .createParser(new StringReader(attributeValue.getS()));
                    argument = jsonParser.readValueAs(writeMethod.getParameterTypes()[0]);
                } catch (final Exception e) {
                    throw new IllegalStateException("Could not parse attribute value: " + attributeValue, e);
                }
            }
            try {
                writeMethod.invoke(item, argument);
            } catch (final Exception e) {
                throw new IllegalStateException(e);
            }
        }
    }
}

From source file:org.jkcsoft.java.util.Beans.java

public static void set(Object bean, String propName, Object value) throws Exception {
    PropertyDescriptor pd = getPropertyDescriptor(bean, propName);

    if (pd == null) {
        throw new NoSuchFieldException("Unknown property: " + propName);
    }// w ww.j ava 2s .c  o m

    Method setter = pd.getWriteMethod();
    if (setter == null) {
        throw new NoSuchMethodException("No write method for: " + propName);
    }

    setter.invoke(bean, new Object[] { value });
}

From source file:io.fabric8.devops.ProjectConfigs.java

/**
 * Configures the given {@link ProjectConfig} with a map of key value pairs from
 * something like a JBoss Forge command//from  w w  w .jav  a2  s  .c  o m
 */
public static void configureProperties(ProjectConfig config, Map map) {
    Class<? extends ProjectConfig> clazz = config.getClass();
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException e) {
        LOG.warn("Could not introspect " + clazz.getName() + ". " + e, e);
    }
    if (beanInfo != null) {
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor descriptor : propertyDescriptors) {
            Method writeMethod = descriptor.getWriteMethod();
            if (writeMethod != null) {
                String name = descriptor.getName();
                Object value = map.get(name);
                if (value != null) {
                    Object safeValue = null;
                    Class<?> propertyType = descriptor.getPropertyType();
                    if (propertyType.isInstance(value)) {
                        safeValue = value;
                    } else {
                        PropertyEditor editor = descriptor.createPropertyEditor(config);
                        if (editor == null) {
                            editor = PropertyEditorManager.findEditor(propertyType);
                        }
                        if (editor != null) {
                            String text = value.toString();
                            editor.setAsText(text);
                            safeValue = editor.getValue();
                        } else {
                            LOG.warn("Cannot update property " + name + " with value " + value + " of type "
                                    + propertyType.getName() + " on " + clazz.getName());
                        }
                    }
                    if (safeValue != null) {
                        try {
                            writeMethod.invoke(config, safeValue);
                        } catch (Exception e) {
                            LOG.warn("Failed to set property " + name + " with value " + value + " on "
                                    + clazz.getName() + " " + config + ". " + e, e);
                        }

                    }
                }
            }
        }
    }
    String flow = null;
    Object flowValue = map.get("pipeline");
    if (flowValue == null) {
        flowValue = map.get("flow");
    }
    if (flowValue != null) {
        flow = flowValue.toString();
    }
    config.setPipeline(flow);
}

From source file:org.springframework.data.solr.server.support.SolrServerUtils.java

/**
 * Solr property names do not match the getters/setters used for them. Check on any write method, try to find the
 * according property and set the value for it. Will ignore all other, and nested properties
 * //from   w w  w.  j av  a  2  s  .com
 * @param source
 * @param target
 */
private static void copyProperties(SolrServer source, SolrServer target) {
    BeanWrapperImpl wrapperImpl = new BeanWrapperImpl(source);
    for (PropertyDescriptor pd : wrapperImpl.getPropertyDescriptors()) {
        Method writer = pd.getWriteMethod();
        if (writer != null) {
            try {
                Field property = ReflectionUtils.findField(source.getClass(), pd.getName());
                if (property != null) {
                    ReflectionUtils.makeAccessible(property);
                    Object o = ReflectionUtils.getField(property, source);
                    if (o != null) {
                        writer.invoke(target, o);
                    }
                }
            } catch (Exception e) {
                logger.warn("Could not copy property value for: " + pd.getName(), e);
            }
        }
    }
}

From source file:org.jkcsoft.java.util.Beans.java

/**
 * Doesn't work yet!/*from   ww w. ja  v  a  2 s. c om*/
 *
 * @param bean
 */
public static void cleanBean(Object bean) {
    if (bean == null)
        return;

    try {
        BeanInfo bi = Introspector.getBeanInfo(bean.getClass());
        PropertyDescriptor pds[] = bi.getPropertyDescriptors();
        PropertyDescriptor pd = null;
        for (int i = 0; i < pds.length; i++) {
            pd = pds[i];
            //Method getter = pd.getReadMethod();
            Method setter = pd.getWriteMethod();
            if (pd.getPropertyType() == Integer.class) {
                setter.invoke(bean, new Object[] { new Integer(0) });
            } else if (pd.getPropertyType() == Double.class) {
                setter.invoke(bean, new Object[] { new Double(0) });
            } else {
                try {
                    setter.invoke(bean, new Object[] { null });
                } catch (Throwable e) {
                    log.warn("cleanBean()", e);
                }
            }
        }
    } catch (Throwable e) {
        log.warn("cleanBean()", e);
    }
}

From source file:org.saiku.adhoc.utils.TemplateUtils.java

public static void mergeElementFormats(SaikuElementFormat source, SaikuElementFormat target) throws Exception {

    BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass());

    for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {

        if (descriptor.getWriteMethod() != null) {

            Object sourceValue = descriptor.getReadMethod().invoke(source);
            if (sourceValue != null) {
                descriptor.getWriteMethod().invoke(target, sourceValue);
            }//from ww  w . j a v  a 2 s  .co  m

        }
    }

}

From source file:com.frank.search.solr.server.support.SolrClientUtils.java

/**
 * Solr property names do not match the getters/setters used for them. Check
 * on any write method, try to find the according property and set the value
 * for it. Will ignore all other, and nested properties
 * // w w  w  . ja va2s  .  c  om
 * @param source
 * @param target
 */
private static void copyProperties(SolrClient source, SolrClient target) {
    BeanWrapperImpl wrapperImpl = new BeanWrapperImpl(source);
    for (PropertyDescriptor pd : wrapperImpl.getPropertyDescriptors()) {
        Method writer = pd.getWriteMethod();
        if (writer != null) {
            try {
                Field property = ReflectionUtils.findField(source.getClass(), pd.getName());
                if (property != null) {
                    ReflectionUtils.makeAccessible(property);
                    Object o = ReflectionUtils.getField(property, source);
                    if (o != null) {
                        writer.invoke(target, o);
                    }
                }
            } catch (Exception e) {
                logger.warn("Could not copy property value for: " + pd.getName(), e);
            }
        }
    }
}

From source file:com.espertech.esper.event.bean.PropertyHelper.java

private static void addIntrospectPropertiesWritable(Class clazz, Set<WriteablePropertyDescriptor> result) {
    PropertyDescriptor properties[] = introspect(clazz);
    for (int i = 0; i < properties.length; i++) {
        PropertyDescriptor property = properties[i];
        String propertyName = property.getName();
        Method writeMethod = property.getWriteMethod();

        if (writeMethod == null) {
            continue;
        }/* w w w  .ja  v a2s .  com*/

        result.add(
                new WriteablePropertyDescriptor(propertyName, writeMethod.getParameterTypes()[0], writeMethod));
    }
}

From source file:com.github.sevntu.checkstyle.internal.ChecksTest.java

private static Set<String> getProperties(Class<?> clss) {
    final Set<String> result = new TreeSet<>();
    final PropertyDescriptor[] map = PropertyUtils.getPropertyDescriptors(clss);

    for (PropertyDescriptor p : map) {
        if (p.getWriteMethod() != null) {
            result.add(p.getName());/*from  w  w w. j  a v  a2  s .c om*/
        }
    }

    return result;
}

From source file:org.eclipse.scada.da.core.VariantBeanHelper.java

public static void applyValue(final Object target, final PropertyDescriptor pd, final Variant value)
        throws OperationException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // ensure for the following calls that the PD has a write method
    final Method m = pd.getWriteMethod();
    if (m == null) {
        throw new OperationException(String.format("Property '%s' is write protected", pd.getName()));
    }/*from  ww w .  j  av a  2  s.  co m*/

    final Class<?> targetType = pd.getPropertyType();
    if (targetType.isAssignableFrom(Variant.class)) {
        // direct set using variant type
        m.invoke(target, value);
        return;
    }

    // now we need to convert
    if (!applyValueAsObject(target, pd, value.getValue())) {
        throw new OperationException(
                String.format("There is no way to convert '%s' to '%s'", value, targetType));
    }
}