List of usage examples for java.beans PropertyDescriptor getWriteMethod
public synchronized Method getWriteMethod()
From source file:com.nortal.petit.beanmapper.BeanMappingUtils.java
private static boolean isPropertyReadableAndWritable(PropertyDescriptor pd) { if (pd == null || pd.getReadMethod() == null || pd.getWriteMethod() == null) { return false; }/*from w w w . j a v a2 s .c o m*/ return true; }
From source file:nl.strohalm.cyclos.utils.SpringHelper.java
/** * Injects beans on setters using the Inject annotation *//*from w ww .j a va 2 s . c o m*/ public static void injectBeans(final BeanFactory beanFactory, final Object target) { final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(target); for (final PropertyDescriptor descriptor : propertyDescriptors) { final Method setter = descriptor.getWriteMethod(); if (setter != null) { final Inject inject = setter.getAnnotation(Inject.class); if (inject != null) { String beanName = inject.value(); // The bean name defaults to the property name if (StringUtils.isEmpty(beanName)) { beanName = descriptor.getName(); } // Retrieve the bean from spring Object bean = beanFactory.getBean(beanName); ensureSecurityService(bean, target); try { bean = CoercionHelper.coerce(descriptor.getPropertyType(), bean); } catch (final ConversionException e) { throw new IllegalStateException("Bean " + beanName + " is not of the expected type type: " + descriptor.getPropertyType().getName()); } // Set the bean try { setter.invoke(target, bean); } catch (final Exception e) { throw new IllegalStateException("Error setting bean " + bean + " on action " + target + " by injecting property " + descriptor.getName() + ": " + e, e); } } } } if (target instanceof InitializingBean) { try { ((InitializingBean) target).afterPropertiesSet(); } catch (final Exception e) { throw new IllegalStateException( String.format("Error after properties set on %1$s: %2$s", target, e.getMessage()), e); } } }
From source file:org.apache.niolex.commons.bean.BeanUtil.java
/** * Parse all the write methods and prepare them to the hash map. * * @param toClass the class to be introspected * @return the hash map// w ww. java 2 s . c o m * @throws IntrospectionException */ public static Map<String, Method> prepareWriteMethodMap(Class<?> toClass) throws IntrospectionException { BeanInfo toInfo = Introspector.getBeanInfo(toClass); HashMap<String, Method> writeMap = Maps.newHashMap(); // Iterate over all the attributes of to, prepare write methods. for (PropertyDescriptor descriptor : toInfo.getPropertyDescriptors()) { Method writeMethod = descriptor.getWriteMethod(); if (writeMethod == null) { continue; } writeMap.put(descriptor.getName(), writeMethod); } return writeMap; }
From source file:org.caratarse.auth.model.util.BeanUtils.java
/** * Copy the not-null property values of the given source bean into the given target bean. * <p>//from w ww. j ava2s.co m * Note: The source and target classes do not have to match or even be derived from each other, * as long as the properties match. Any bean properties that the source bean exposes but the * target bean does not will silently be ignored. * * @param source the source bean * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @param ignoreProperties array of property names to ignore * @throws BeansException if the copying failed * @see BeanWrapper */ private static void copyNotNullProperties(Object source, Object target, Class<?> editable, String... ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); } actualEditable = editable; } PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) { PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils .getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (value == null) { continue; } if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new FatalBeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }
From source file:org.jaxygen.typeconverter.util.BeanUtil.java
/** * Copies bean content from one bean to another. The bean is copied using the * set and get methods. Method invokes all getXX methods on the from object, * and result pass to the corresponding setXX methods in the to object. Note * that it is not obvious that both object are of the same type. * If get and set methods do not match then it tries to use appropriate * converter registered in default type converter factory. * * @param from data provider object.// w ww. j a v a2s . c o m * @param to data acceptor object. */ public static void translateBean(Object from, Object to) { PropertyDescriptor[] fromGetters = null; fromGetters = PropertyUtils.getPropertyDescriptors(from.getClass()); for (PropertyDescriptor pd : fromGetters) { if (pd.getReadMethod().isAnnotationPresent(BeanTransient.class) == false) { if (PropertyUtils.isWriteable(to, pd.getName())) { try { PropertyDescriptor wd = PropertyUtils.getPropertyDescriptor(to, pd.getName()); if (!wd.getWriteMethod().isAnnotationPresent(BeanTransient.class)) { Object copyVal = PropertyUtils.getProperty(from, pd.getName()); if (wd.getPropertyType().isAssignableFrom(pd.getPropertyType())) { PropertyUtils.setProperty(to, wd.getName(), copyVal); } else { try { Object convertedCopyVal = TypeConverterFactory.instance().convert(copyVal, wd.getPropertyType()); PropertyUtils.setProperty(to, wd.getName(), convertedCopyVal); } catch (ConversionError ex) { Logger.getAnonymousLogger().log(Level.WARNING, "Method {0}.{1} of type {2} is not compatible to {3}.{4} of type{5}", new Object[] { from.getClass().getName(), pd.getName(), pd.getPropertyType(), to.getClass().getName(), wd.getName(), wd.getPropertyType() }); } } } } catch (IllegalAccessException ex) { throw new java.lang.IllegalArgumentException("Could not translate bean", ex); } catch (InvocationTargetException ex) { throw new java.lang.IllegalArgumentException("Could not translate bean", ex); } catch (NoSuchMethodException ex) { throw new java.lang.IllegalArgumentException("Could not translate bean", ex); } } } } }
From source file:org.force66.beantester.utils.InjectionUtils.java
public static void injectValues(Object bean, ValueGeneratorFactory valueGeneratorFactory, boolean reportExceptions) { ValueGenerator<?> generator;// w w w .j a va 2s.c o m Object[] generatedValues = null; for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(bean)) { try { generator = valueGeneratorFactory.forClass(descriptor.getPropertyType()); if (generator != null && descriptor.getWriteMethod() != null) { generatedValues = generator.makeValues(); descriptor.getWriteMethod().invoke(bean, generatedValues[0]); } } catch (Exception e) { if (reportExceptions) { throw new BeanTesterException("Error setting property value", e) .addContextValue("class", bean.getClass().getName()) .addContextValue("field", descriptor.getName()).addContextValue("value", generatedValues != null && generatedValues.length > 0 ? generatedValues[0] : null); } } } }
From source file:org.psnively.scala.beans.ScalaBeanInfo.java
private static void addScalaSetter(Map<String, PropertyDescriptor> propertyDescriptors, Method writeMethod) { String propertyName = writeMethod.getName().substring(0, writeMethod.getName().length() - SCALA_SETTER_SUFFIX.length()); PropertyDescriptor pd = propertyDescriptors.get(propertyName); if (pd != null && pd.getWriteMethod() == null) { try {//from www. j a v a 2 s. c o m pd.setWriteMethod(writeMethod); } catch (IntrospectionException ex) { logger.debug("Could not add write method [" + writeMethod + "] for " + "property [" + propertyName + "]: " + ex.getMessage()); } } else if (pd == null) { try { pd = new PropertyDescriptor(propertyName, null, writeMethod); propertyDescriptors.put(propertyName, pd); } catch (IntrospectionException ex) { logger.debug("Could not create new PropertyDescriptor for " + "writeMethod [" + writeMethod + "] property [" + propertyName + "]: " + ex.getMessage()); } } }
From source file:nl.strohalm.cyclos.utils.PropertyHelper.java
/** * Copies all possible properties from source to dest, ignoring the given properties list. Exceptions are ignored *///ww w. j a v a2s .com public static void copyProperties(final Object source, final Object dest, final String... ignored) { if (source == null || dest == null) { return; } final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(source); for (final PropertyDescriptor sourceDescriptor : propertyDescriptors) { try { final String name = sourceDescriptor.getName(); // Check for ignored properties if (ArrayUtils.contains(ignored, name)) { continue; } final PropertyDescriptor destProperty = PropertyUtils.getPropertyDescriptor(dest, name); if (destProperty.getWriteMethod() == null) { // Ignore read-only properties continue; } final Object value = CoercionHelper.coerce(destProperty.getPropertyType(), get(source, name)); set(dest, name, value); } catch (final Exception e) { // Ignore this property } } }
From source file:org.zht.framework.util.ZBeanUtil.java
private static void copy(Object source, Object target, Boolean ignorNull, Class<?> editable, String... ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); }/* w w w .j av a 2 s . c o m*/ actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (ignorNull != null && ignorNull) { if (value != null && (!"[]".equals(value.toString()))) {// ? if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } else { if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } catch (Throwable ex) { throw new FatalBeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }
From source file:org.shept.util.BeanUtilsExtended.java
/** * Merge the property values of the given source bean into the given target bean. * <p>Note: Only not-null values are merged into the given target bean. * Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * @param source the source bean/*from www . j a va2 s . co m*/ * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @param ignoreProperties array of property names to ignore * @throws BeansException if the copying failed * @see BeanWrapper */ private static void mergeProperties(Object source, Object target, Class<?> editable, String[] ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); } actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); 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 = 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); } Object value = readMethod.invoke(source); if (value != null) { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } catch (Throwable ex) { throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } }