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.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor.java

public void setDataValue(R rowObj, int columnIndex, Object newValue) {
    try {/*from w  w  w.ja  v a  2s . c o m*/
        PropertyDescriptor propertyDesc = getPropertyDescriptor(rowObj, columnIndex);
        Method writeMethod = propertyDesc.getWriteMethod();
        if (writeMethod == null) {
            throw new RuntimeException(
                    "Setter method not found in backing bean for value at column index: " + columnIndex); //$NON-NLS-1$
        }
        writeMethod.invoke(rowObj, newValue);
    } catch (IllegalArgumentException ex) {
        log.error("Data type being set does not match the data type of the setter method in the backing bean", //$NON-NLS-1$
                ex);
    } catch (Exception e) {
        log.error(e);
        throw new RuntimeException("Error while setting data value"); //$NON-NLS-1$
    }
}

From source file:com.iorga.webappwatcher.util.BasicParameterHandler.java

private void initAccessorMethods(final PropertyDescriptor fieldPropertyDescriptor) {
    this.writeMethod = fieldPropertyDescriptor.getWriteMethod();
    this.readMethod = fieldPropertyDescriptor.getReadMethod();
    if (this.writeMethod == null) {
        throw new IllegalStateException("Couldn't find setter for " + ownerClass + "." + fieldName);
    }/*from w  w w.  j a  va2  s. co  m*/
    if (this.readMethod == null) {
        throw new IllegalStateException("Couldn't find getter for " + ownerClass + "." + fieldName);
    }
}

From source file:org.intermine.task.DynamicAttributeTask.java

/**
 * Look at set methods on a target object and lookup values in project
 * properties.  If no value found property will not be set but no error
 * will be thrown.// w  ww.  j  a v  a2  s.  co m
 * @param bean an object to search for setter methods
 */
protected void configureDynamicAttributes(Object bean) {
    Project antProject = getProject();
    Hashtable<?, ?> projectProps = antProject.getProperties();
    PropertyDescriptor[] props = PropertyUtils.getPropertyDescriptors(bean);
    for (int i = 0; i < props.length; i++) {
        PropertyDescriptor desc = props[i];
        Method setter = desc.getWriteMethod();
        if (setter != null) {
            Class<?> propType = desc.getPropertyType();
            String propName = setter.getName().substring(3).toLowerCase();
            Object propValue = projectProps.get(propName);

            if (propValue == null) {
                // there is not all-lowercase property in projectProps, so try the camelCase
                // version
                String setterName = setter.getName();
                String camelCasePropName = setterName.substring(3, 4).toLowerCase() + setterName.substring(4);
                propName = camelCasePropName;
                propValue = projectProps.get(camelCasePropName);
            }

            if (propValue == null) {
                // still not found, try replacing each capital (after first) in camelCase
                // to be a dot - i.e. setSrcDataDir -> src.data.dir
                String setterName = setter.getName();
                String camelCasePropName = setterName.substring(3, 4).toLowerCase() + setterName.substring(4);
                String dotName = "";
                for (int j = 0; j < camelCasePropName.length(); j++) {
                    if (Character.isUpperCase(camelCasePropName.charAt(j))) {
                        dotName += "." + camelCasePropName.substring(j, j + 1).toLowerCase();
                    } else {
                        dotName += camelCasePropName.substring(j, j + 1);
                    }
                }
                propValue = projectProps.get(dotName);
            }

            if (propValue != null) {
                try {
                    if (propType.equals(File.class)) {

                        String filePropValue = (String) propValue;
                        //First check to see if we were given a complete file path, if so then
                        // we can use it directly instead of trying to search for it.
                        File maybeFile = new File(filePropValue);
                        if (maybeFile.exists()) {
                            propValue = maybeFile;
                            LOG.info("Configuring task to use file:" + filePropValue);
                        } else {
                            propValue = getProject().resolveFile(filePropValue);
                        }
                    }
                    PropertyUtils.setProperty(bean, propName, propValue);
                } catch (Exception e) {
                    throw new BuildException(
                            "failed to set value for " + propName + " to " + propValue + " in " + bean, e);
                }
            }
        }
    }
}

From source file:org.beangle.spring.bind.AutoConfigProcessor.java

protected Map<String, PropertyDescriptor> unsatisfiedNonSimpleProperties(BeanDefinition mbd) {
    Map<String, PropertyDescriptor> properties = CollectUtils.newHashMap();
    PropertyValues pvs = mbd.getPropertyValues();
    Class<?> clazz = null;/*from   w  ww .  jav a2  s  .  c om*/
    try {
        clazz = Class.forName(mbd.getBeanClassName());
    } catch (ClassNotFoundException e) {
        logger.error("Class " + mbd.getBeanClassName() + "not found", e);
        return properties;
    }
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(clazz);
    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName())
                && !BeanUtils.isSimpleProperty(pd.getPropertyType())) {
            properties.put(pd.getName(), pd);
        }
    }
    return properties;
}

From source file:org.snaker.engine.access.jdbc.BeanPropertyHandler.java

/**
 * ?beanclass??// w w w.  j  ava  2s . c o m
 * @param mappedClass
 */
protected void initialize(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<String, PropertyDescriptor>();
    PropertyDescriptor[] pds = null;
    try {
        /**
         * bean??
         */
        pds = propertyDescriptors(mappedClass);
    } catch (SQLException e) {
        throw new SnakerException(e.getMessage(), e.getCause());
    }
    for (PropertyDescriptor pd : pds) {
        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:com.link_intersystems.beans.PropertyDescriptor2AccessorsTransformer.java

public Object transform(Object input) {
    Object transformed = input;/*  w w w.j  a  v a 2  s .  c  om*/
    if (input instanceof PropertyDescriptor) {
        PropertyDescriptor descriptor = PropertyDescriptor.class.cast(input);
        Method readMethod = descriptor.getReadMethod();
        Method writeMethod = descriptor.getWriteMethod();
        List<Method> methods = new ArrayList<Method>();
        if (readMethod != null) {
            methods.add(readMethod);
        }

        if (writeMethod != null) {
            methods.add(writeMethod);
        }
        transformed = methods.iterator();
    }
    return transformed;
}

From source file:de.erdesignerng.dialect.ModelItemProperties.java

public void copyTo(T aObject) {

    ModelProperties theProperties = aObject.getProperties();

    try {//from  w ww  .  jav  a  2 s  .c o  m
        for (PropertyDescriptor theDescriptor : PropertyUtils.getPropertyDescriptors(this)) {
            if (theDescriptor.getReadMethod() != null && theDescriptor.getWriteMethod() != null) {
                Object theValue = PropertyUtils.getProperty(this, theDescriptor.getName());
                if (theValue != null) {
                    theProperties.setProperty(theDescriptor.getName(), theValue.toString());
                } else {
                    theProperties.setProperty(theDescriptor.getName(), null);
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.avanza.ymer.MongoQueryFactory.java

private boolean isNotTemplatableMethod(PropertyDescriptor pd) {
    return pd.getReadMethod() == null || pd.getReadMethod().getDeclaringClass() == Object.class
            || pd.getWriteMethod() == null || pd.getName().equals("versionID");
}

From source file:org.jdal.dao.BeanFilter.java

/**
 * {@inheritDoc}/*from  w  w w . ja  v a  2 s  . c om*/
 */
public void clear() {
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(getClass());

    for (PropertyDescriptor pd : pds) {
        if (!ignoredProperties.contains(pd.getName())) {
            try {
                pd.getWriteMethod().invoke(this, (Object) null);
            } catch (Exception e) {
                log.error("Error nullifing property: [" + pd.getName() + "]", e);
            }
        }
    }
}

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

private static boolean writePropertyValueNamed0(String propertyName, Object bean, Object newValue) {
    PropertyDescriptor pd = findPropertyDescriptorFor(bean.getClass(), propertyName);
    if (pd == null) {
        if (L.isDebugEnabled()) {
            Map<String, Object> replace = new HashMap<String, Object>(2);
            replace.put("class", bean.getClass().getName());
            replace.put("property", propertyName);
            L.debug(Strings.substitute(R.getString("D-UTIL#0017"), replace));
        }//from  w w  w .j  ava2 s .com
        return false;
    }
    Method writer = pd.getWriteMethod();
    if (writer == null) {
        if (L.isDebugEnabled()) {
            Map<String, Object> replace = new HashMap<String, Object>(2);
            replace.put("class", bean.getClass().getName());
            replace.put("property", propertyName);
            L.debug(Strings.substitute(R.getString("D-UTIL#0018"), replace));
        }
        return false;
    }
    Reflects.invoke(writer, bean, newValue);
    return true;
}