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:org.neovera.jdiablo.environment.SpringEnvironment.java

private void setValue(PropertyDescriptor pd, Object target, String value) {
    Method writeMethod = pd.getWriteMethod();
    if (writeMethod == null) {
        throw new RuntimeException("No write method found for property " + pd.getName());
    }/* ww w .java  2  s .c  o m*/
    try {
        if (writeMethod.getParameterTypes()[0].equals(Boolean.class)
                || "boolean".equals(writeMethod.getParameterTypes()[0].getName())) {
            writeMethod.invoke(target, value == null ? false : "true".equals(value));
        } else if (writeMethod.getParameterTypes()[0].equals(Integer.class)
                || "int".equals(writeMethod.getParameterTypes()[0].getName())) {
            writeMethod.invoke(target, value == null ? 0 : Integer.parseInt(value));
        } else if (writeMethod.getParameterTypes()[0].equals(Long.class)
                || "long".equals(writeMethod.getParameterTypes()[0].getName())) {
            writeMethod.invoke(target, value == null ? 0 : Long.parseLong(value));
        } else if (writeMethod.getParameterTypes()[0].equals(BigDecimal.class)) {
            writeMethod.invoke(target, value == null ? null : new BigDecimal(value));
        } else if (writeMethod.getParameterTypes()[0].equals(String.class)) {
            writeMethod.invoke(target, value);
        } else if (writeMethod.getParameterTypes()[0].isArray()
                && writeMethod.getParameterTypes()[0].getName().contains(String.class.getName())) {
            writeMethod.invoke(target, (Object[]) value.split(","));
        } else {
            throw new RuntimeException("Could not resolve parameter type for " + writeMethod.toString());
        }
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hengyi.japp.sap.convert.impl.FieldCopyBase.java

protected void initBeanPropertyWriteMethod(Object bean, JCoRecord record) throws Exception {
    PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(bean, beanPropertyName);
    beanPropertyWriteMethod = descriptor.getWriteMethod();
    if (beanPropertyWriteMethod == null) {
        return;/*from  w  w  w .ja v  a2  s .c o m*/
    }
    String methodName = beanPropertyWriteMethod.getName();
    String expectedTypeString = record.getMetaData().getClassNameOfField(sapFieldName);
    Class<?> sapFieldType = Class.forName(expectedTypeString);
    beanPropertyWriteMethod = MethodUtils.getMatchingAccessibleMethod(bean.getClass(), methodName,
            sapFieldType);
}

From source file:org.mule.module.netsuite.api.model.expression.filter.FilterExpressionBuilder.java

private void convertAndSet(Object argument, String propertyName, Object attribute) throws Exception {
    PropertyDescriptor descriptor = newDescriptor(propertyName, attribute);

    try {//from w ww .  j  a  v a  2  s  .c o  m
        descriptor.getWriteMethod().invoke(attribute,
                convert(argument, descriptor.getPropertyType(), attribute.getClass()));
    } catch (Exception e) {
        throw new IllegalArgumentException(String.format("Can not set property %s of class %s with value %s",
                propertyName, attribute.getClass().getSimpleName(), argument), e);
    }
}

From source file:org.springframework.data.jdbc.support.oracle.BeanPropertyStructMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * @param mappedClass the mapped class./*from w  ww  .ja v  a  2s .  com*/
 */
protected void initialize(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<String, PropertyDescriptor>();
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor pd = pds[i];
        if (pd.getWriteMethod() != null) {
            this.mappedFields.put(pd.getName().toLowerCase(), pd);
            String underscoredName = underscoreName(pd.getName());
            if (!pd.getName().toLowerCase().equals(underscoredName)) {
                this.mappedFields.put(underscoredName, pd);
            }
        }
    }
}

From source file:net.paoding.rose.jade.core.BeanPropertyRowMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * //from  ww w .  j a  va  2 s . c  o  m
 */
protected void initialize() {
    this.mappedFields = new HashMap<String, PropertyDescriptor>();
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor pd = pds[i];
        if (pd.getWriteMethod() != null) {
            this.mappedProperties.add(pd.getName());
            this.mappedFields.put(pd.getName().toLowerCase(), pd);
            for (String underscoredName : underscoreName(pd.getName())) {
                if (!pd.getName().toLowerCase().equals(underscoredName)) {
                    this.mappedFields.put(underscoredName, pd);
                }
            }
        }
    }
}

From source file:com.bradmcevoy.property.BeanPropertySource.java

@Override
public void setProperty(QName name, Object value, Resource r)
        throws NotAuthorizedException, PropertySetException {
    log.debug("setProperty: " + name + " = " + value);
    PropertyDescriptor pd = getPropertyDescriptor(r, name.getLocalPart());
    try {/* www  . j a  v  a 2s .c om*/
        pd.getWriteMethod().invoke(r, value);
    } catch (PropertySetException e) {
        throw e;
    } catch (Exception ex) {
        if (ex.getCause() instanceof NotAuthorizedException) {
            NotAuthorizedException na = (NotAuthorizedException) ex.getCause();
            throw na;
        } else if (ex.getCause() instanceof PropertySetException) {
            PropertySetException na = (PropertySetException) ex.getCause();
            throw na;
        } else {
            if (value == null) {
                log.error("Exception setting property: " + name.toString() + " to null");
            } else {
                log.error("Exception setting property: " + name.toString() + " to value: " + value + " class:"
                        + value.getClass());
            }
            throw new RuntimeException(name.toString(), ex);
        }
    }
}

From source file:org.grails.datastore.mapping.engine.EntityAccess.java

public void setPropertyNoConversion(String name, Object value) {
    final PropertyDescriptor pd = beanWrapper.getPropertyDescriptor(name);
    if (pd == null) {
        return;/*from   www  .j  a  va 2 s  .c  o  m*/
    }
    final Method writeMethod = pd.getWriteMethod();
    if (writeMethod != null) {
        ReflectionUtils.invokeMethod(writeMethod, beanWrapper.getWrappedInstance(), value);
    }
}

From source file:com.manydesigns.elements.reflection.JavaPropertyAccessor.java

public JavaPropertyAccessor(PropertyDescriptor propertyDescriptor) {
    this.propertyDescriptor = propertyDescriptor;
    getter = propertyDescriptor.getReadMethod();
    setter = propertyDescriptor.getWriteMethod();
    if (setter == null) {
        logger.debug("Setter not available for: {}", propertyDescriptor.getName());
    }/*from   w ww  . ja v a 2 s  . co m*/
}

From source file:at.molindo.notify.model.BeanParams.java

@Override
public Iterator<ParamValue> iterator() {
    return new Iterator<ParamValue>() {

        private final Iterator<PropertyDescriptor> _iter = getDescriptorsIter();
        private ParamValue _next = findNext();

        private ParamValue findNext() {
            while (_iter.hasNext()) {
                PropertyDescriptor pd = _iter.next();
                if (pd.getWriteMethod() == null || pd.getReadMethod() == null) {
                    continue;
                }// ww  w. java2s.co m
                Object value = invoke(pd.getReadMethod());
                if (value == null) {
                    continue;
                }
                return Param.p(pd.getPropertyType(), pd.getName()).paramValue(value);
            }
            return null;
        }

        @Override
        public boolean hasNext() {
            return _next != null;
        }

        @Override
        public ParamValue next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            ParamValue next = _next;
            _next = findNext();
            return next;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.echocat.jemoni.jmx.reflection.ReflectionBasedAttributeDefinition.java

@Nonnull
protected AccessMode getAccessModeBy(@Nonnull PropertyDescriptor descriptor) {
    final Method readMethod = descriptor.getReadMethod();
    final Method writeMethod = descriptor.getWriteMethod();
    final AccessMode accessMode;
    if (readMethod != null && writeMethod != null) {
        accessMode = AccessMode.readWrite;
    } else if (readMethod != null) {
        accessMode = AccessMode.readOnly;
    } else if (writeMethod != null) {
        accessMode = AccessMode.writeOnly;
    } else {//from   ww w.jav a2s .  co  m
        throw new IllegalStateException("There is no read and no write method?");
    }
    return accessMode;
}