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:de.erdesignerng.dialect.ModelItemProperties.java

public void initializeFrom(T aObject) {
    ModelProperties theProperties = aObject.getProperties();

    try {// w w  w.  ja va 2 s.  c o m
        for (PropertyDescriptor theDescriptor : PropertyUtils.getPropertyDescriptors(this)) {
            if (theDescriptor.getReadMethod() != null && theDescriptor.getWriteMethod() != null) {
                String theValue = theProperties.getProperty(theDescriptor.getName());
                if (!StringUtils.isEmpty(theValue)) {
                    Class theType = theDescriptor.getPropertyType();

                    if (theType.isEnum()) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(),
                                Enum.valueOf(theType, theValue));
                    }
                    if (String.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), theValue);
                    }
                    if (Long.class.equals(theType) || long.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), Long.parseLong(theValue));
                    }
                    if (Integer.class.equals(theType) || int.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), Integer.parseInt(theValue));
                    }
                    if (Boolean.class.equals(theType) || boolean.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(),
                                Boolean.parseBoolean(theValue));
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:stroom.util.AbstractCommandLineTool.java

private void doTraceArguments(final PrintStream printStream) throws Exception {
    final BeanInfo beanInfo = Introspector.getBeanInfo(this.getClass());
    for (final PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
        // Only do properties with getters
        if (pd.getWriteMethod() != null) {
            // Simple getter ?
            String suffix = " (default)";
            if (map.containsKey(pd.getName())) {
                suffix = " (arg)";
            }/*from w w  w .ja v a2s .c  om*/
            String value = "";

            if (pd.getReadMethod() != null && pd.getReadMethod().getParameterTypes().length == 0) {
                value = String.valueOf(pd.getReadMethod().invoke(this));
            } else {
                // No simple getter
                Field field = null;
                try {
                    field = this.getClass().getDeclaredField(pd.getName());
                } catch (final NoSuchFieldException nsfex) {
                    // Ignore
                }
                if (field != null) {
                    field.setAccessible(true);
                    value = String.valueOf(field.get(this));
                } else {
                    value = "?";

                }
            }
            printStream.println(StringUtils.rightPad(pd.getName(), maxPropLength) + " = " + value + suffix);
        }
    }
}

From source file:com.expedia.seiso.domain.service.impl.ItemMerger.java

@SneakyThrows
private void mergeSimpleProperty(Item src, Item dest, PropertyDescriptor propDesc) {
    val getter = propDesc.getReadMethod();
    val setter = propDesc.getWriteMethod();

    if (getter == null || setter == null) {
        log.trace("Skipping simple property: {}", propDesc.getName());
        return;//from  w  ww .  j a v  a  2  s.  com
    }

    val propValue = getter.invoke(src);
    setter.invoke(dest, propValue);
}

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

private void setOperation(String operationName, Object attribute) throws Exception {
    PropertyDescriptor descriptor = newDescriptor("operator", attribute);
    descriptor.getWriteMethod().invoke(attribute, parseOperation(operationName, descriptor));
}

From source file:org.apache.shiro.guice.BeanTypeListener.java

public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) {
    PropertyDescriptor propertyDescriptors[] = PropertyUtils.getPropertyDescriptors(type.getRawType());
    final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(
            propertyDescriptors.length);
    final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        if (propertyDescriptor.getWriteMethod() != null
                && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) {
            Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0];
            propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType));
        }//from   ww  w . jav a2  s  .  co m
    }
    encounter.register(new MembersInjector<I>() {
        public void injectMembers(I instance) {
            for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) {
                try {
                    final Injector injector = injectorProvider.get();

                    Object value = injector.getInstance(getMappedKey(injector, dependency.getValue()));
                    dependency.getKey().getWriteMethod().invoke(instance, value);

                } catch (ConfigurationException e) {
                    // This is ok, it simply means that we can't fulfill this dependency.
                    // Is there a better way to do this?
                } catch (InvocationTargetException e) {
                    throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(),
                            e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(
                            "We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.",
                            e);
                }
            }

        }
    });
}

From source file:com.zero.service.impl.BaseServiceImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void copyProperties(Object source, Object target) throws BeansException {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");

    PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
    for (PropertyDescriptor targetPd : targetPds) {
        if (targetPd.getWriteMethod() != null) {
            PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(),
                    targetPd.getName());
            if (sourcePd != null && sourcePd.getReadMethod() != null) {
                try {
                    Method readMethod = sourcePd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }//from  w  ww .  ja va 2s  .  c  om
                    Object sourceValue = readMethod.invoke(source);
                    Object targetValue = readMethod.invoke(target);
                    if (sourceValue != null && targetValue != null && targetValue instanceof Collection) {
                        Collection collection = (Collection) targetValue;
                        collection.clear();
                        collection.addAll((Collection) sourceValue);
                    } else {
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, sourceValue);
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target", ex);
                }
            }
        }
    }
}

From source file:com.bstek.dorado.config.xml.SubNodeToPropertyParser.java

public void execute(Object object, CreationContext context) throws Exception {
    if (object instanceof Definition) {
        if (element instanceof Operation) {
            if (element instanceof DefinitionInitOperation) {
                ((DefinitionInitOperation) element).execute(object, context);
            } else if (object instanceof ObjectDefinition) {
                ((ObjectDefinition) object).addInitOperation((Operation) element);
            }// w w  w . j  a va  2 s.  c om
        } else {
            PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(object, property);
            if (propertyDescriptor != null && propertyDescriptor.getWriteMethod() != null
                    && !NATIVE_DEFINITION_PROPERTIES.contains(property)) {
                PropertyUtils.setSimpleProperty(object, property, element);
            } else if (object instanceof ObjectDefinition) {
                ((ObjectDefinition) object).setProperty(property, element);
            }
        }
    } else {
        PropertyUtils.setSimpleProperty(object, property, element);
    }
}

From source file:org.obm.sync.metadata.DatabaseTruncationServiceImpl.java

@Override
public <T> T getTruncatingEntity(T entity) throws SQLException {
    if (entity == null) {
        return null;
    }// w w w  . java 2 s .  com

    try {
        T newInstance = (T) entity.getClass().newInstance();
        PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(entity.getClass());

        for (PropertyDescriptor descriptor : descriptors) {
            Method writeMethod = descriptor.getWriteMethod(), readMethod = descriptor.getReadMethod();

            if (writeMethod != null) {
                String propertyName = descriptor.getName();
                Object value = PropertyUtils.getProperty(entity, propertyName);

                if (readMethod.isAnnotationPresent(DatabaseEntity.class)) {
                    if (value instanceof Collection) {
                        value = copyEntityCollection(value);
                    } else if (value instanceof Map) {
                        value = copyEntityMap(value);
                    } else {
                        value = copyEntity(value);
                    }
                } else {
                    DatabaseField dbField = readMethod.getAnnotation(DatabaseField.class);

                    if (dbField != null && value instanceof String) {
                        value = truncate((String) value, dbField.table(), dbField.column());
                    }
                }

                PropertyUtils.setProperty(newInstance, propertyName, value);
            }
        }

        return newInstance;
    } catch (Exception e) {
        Throwables.propagateIfInstanceOf(e, SQLException.class);

        throw Throwables.propagate(e);
    }
}

From source file:net.groupbuy.service.impl.BaseServiceImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void copyProperties(Object source, Object target, String[] ignoreProperties) throws BeansException {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");

    PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
    List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
    for (PropertyDescriptor targetPd : targetPds) {
        if (targetPd.getWriteMethod() != null
                && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
            PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(),
                    targetPd.getName());
            if (sourcePd != null && sourcePd.getReadMethod() != null) {
                try {
                    Method readMethod = sourcePd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }/*from  www.  jav  a 2s .  c om*/
                    Object sourceValue = readMethod.invoke(source);
                    Object targetValue = readMethod.invoke(target);
                    if (sourceValue != null && targetValue != null && targetValue instanceof Collection) {
                        Collection collection = (Collection) targetValue;
                        collection.clear();
                        collection.addAll((Collection) sourceValue);
                    } else {
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, sourceValue);
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target", ex);
                }
            }
        }
    }
}

From source file:com.trigonic.utils.spring.cmdline.CommandLineMetaData.java

private <T extends Annotation> void checkWriteableProperty(T annotation, Class<?> beanClass, Field field) {
    PropertyDescriptor property = BeanUtils.getPropertyDescriptor(beanClass, field.getName());
    if (property == null || property.getWriteMethod() == null) {
        throw new BeanDefinitionStoreException("@" + annotation.getClass().getSimpleName()
                + " annotation cannot be applied to fields without matching setters");
    }//  w w  w .ja v  a 2  s.c o  m
}