List of usage examples for java.beans PropertyDescriptor getWriteMethod
public synchronized Method getWriteMethod()
From source file:com.mawujun.utils.bean.BeanUtils.java
/** * ????// w w w .j av a 2s. co m * null * @param source * @param target * @throws BeansException * @throws IntrospectionException */ public static void copyExcludeNull(Object source, Object target) throws IntrospectionException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { 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) {//?? continue; } Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new RuntimeException("Could not copy properties from source to target", ex); } } } } }
From source file:org.apache.activemq.artemis.utils.uri.URISchema.java
public static String getData(List<String> ignored, Object... beans) throws Exception { StringBuilder sb = new StringBuilder(); synchronized (beanUtils) { for (Object bean : beans) { if (bean != null) { PropertyDescriptor[] descriptors = beanUtils.getPropertyUtils().getPropertyDescriptors(bean); for (PropertyDescriptor descriptor : descriptors) { if (descriptor.getReadMethod() != null && descriptor.getWriteMethod() != null && isWriteable(descriptor, ignored)) { String value = beanUtils.getProperty(bean, descriptor.getName()); if (value != null) { sb.append("&").append(descriptor.getName()).append("=").append(value); }/*from w w w . j a v a 2 s . c o m*/ } } } } } return sb.toString(); }
From source file:kelly.util.BeanUtils.java
/** * Copy the property values of the given source bean into the given target bean. * <p>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 . ja v a 2 s. c om * @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 copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties) { Validate.notNull(source, "Source must not be null"); Validate.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) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new BeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }
From source file:com.zigbee.framework.common.util.BeanCopyUtil.java
/** * Override copyProperties Method/*from ww w . jav a 2 s. c o m*/ * @Date : 2011-8-5 * @param source source bean instance * @param target destination bean instance */ public static void copyProperties(Object source, Object target) { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { try { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); //Check whether the value is empty, only copy the properties which are not empty if (value != null) { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } writeMethod.invoke(target, value); } } } catch (BeansException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } catch (SecurityException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } catch (IllegalArgumentException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } catch (IllegalAccessException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } catch (InvocationTargetException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } } } }
From source file:com.sparkplatform.api.core.PropertyAsserter.java
/** * See {@link #assertBasicGetterSetterBehavior(Object,String)} method. Big difference here is that we try to * automatically introspect the target object, finding read/write properties, and automatically testing the getter * and setter. Note specifically that read-only properties are ignored, as there is no way for us to know how to set * the value (since there isn't a public setter). * <p/>/*w ww .j av a2s. com*/ * Any property names contained in the blacklist will be skipped. * <p/> * * @param target the object on which to invoke the getter and setter * @param propertyNames the list of property names that should not be tested */ public static void assertBasicGetterSetterBehaviorWithBlacklist(Object target, String... propertyNames) { List<String> blacklist = Arrays.asList(propertyNames); try { BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass()); PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor descriptor : descriptors) { if (descriptor.getWriteMethod() == null) { continue; } if (!blacklist.contains(descriptor.getDisplayName())) { assertBasicGetterSetterBehavior(target, descriptor.getDisplayName()); } } } catch (IntrospectionException e) { fail("Failed while introspecting target " + target.getClass()); } }
From source file:com.xpfriend.fixture.cast.temp.PojoUtil.java
public static Class<?> getPropertyComponentType(Object object, Column column, Table table, Row row) { String propertyName = column.getName(); try {//from www . j av a2 s .c om String name = find(propertyName, object); PropertyDescriptor descriptor = TypeConverter.getBeanUtils().getPropertyUtils() .getPropertyDescriptor(object, name); Method method = descriptor.getWriteMethod(); Type genericType = method.getGenericParameterTypes()[0]; if (genericType != null) { if (genericType instanceof Class) { genericType = ((Class<?>) genericType).getGenericSuperclass(); } if (genericType instanceof ParameterizedType) { Type[] es = ((ParameterizedType) genericType).getActualTypeArguments(); if (es != null && es.length > 0 && es[0] instanceof Class) { return (Class<?>) es[0]; } } } return String.class; } catch (NoSuchMethodException e) { throw createNoSuchPropertyException(object, propertyName, table, row); } catch (InvocationTargetException e) { throw new ConfigException(e.getCause()); } catch (IllegalAccessException e) { throw new ConfigException(e); } }
From source file:org.snowfk.util.ObjectUtil.java
@Deprecated public static void copyNotNull(Object src, Object dest) { try {/*from ww w . j a v a 2 s . c om*/ PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(src); for (PropertyDescriptor pd : pds) { String name = pd.getName(); if (pd.getWriteMethod() != null) { Object value = PropertyUtils.getProperty(src, name); if (value != null) { PropertyUtils.setProperty(dest, name, value); } } } } catch (Exception e) { logger.error(e.getMessage()); throw (new RuntimeException(e)); } }
From source file:org.eclipse.scada.da.core.VariantBeanHelper.java
private static boolean applyValueAsObject(final Object target, final PropertyDescriptor pd, final Object value) throws IllegalAccessException, InvocationTargetException, OperationException { if (value == null) { pd.getWriteMethod().invoke(target, new Object[] { null }); return true; }/*from w w w . j av a 2s. co m*/ final Object targetType = converter.convert(value, pd.getPropertyType()); if (targetType == null) { throw new OperationException( String.format("Unable to convert '%s' to '%s'", value.getClass(), pd.getPropertyType())); } pd.getWriteMethod().invoke(target, targetType); return true; }
From source file:org.paxml.bean.BeanTag.java
static List<PropertyDescriptor> findSettableProperties(Class<? extends BeanTag> clazz) { List<PropertyDescriptor> list = new ArrayList<PropertyDescriptor>(0); for (PropertyDescriptor pd : ReflectUtils.getPropertyDescriptors(clazz, PropertyDescriptorType.SETTER)) { Class<?> ownerClass = pd.getWriteMethod().getDeclaringClass(); if (BeanTag.class.equals(ownerClass) || ReflectUtils.isSubClass(ownerClass, BeanTag.class, true)) { list.add(pd);//from ww w . ja v a2 s. c om } } return list; }
From source file:no.abmu.finances.domain.DomainTestHelper.java
public static Object populateBean(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; if (propertyDescriptor.getName().equals("id")) { continue; }/*from w w w . j av a 2 s. c o m*/ if (P_TYPES.contains(propertyDescriptor.getPropertyType()) && propertyDescriptor.getWriteMethod() != null) { Object obje; obje = ConvertUtils.convert( String.valueOf((int) (System.currentTimeMillis() + (int) (Math.random() * 100d))), propertyDescriptor.getPropertyType()); PropertyUtils.setProperty(obj, propertyDescriptor.getName(), obje); } } return obj; }