List of usage examples for java.beans PropertyDescriptor getWriteMethod
public synchronized Method getWriteMethod()
From source file:org.snowfk.util.ObjectUtil.java
/** * Copy all the properties are that not null, or not empty (for List and * array objects) to the dest object.// ww w. j a va 2 s . c o m * * @param src * @param dest */ @Deprecated public static void copyNotNullNotEmpty(Object src, Object dest) { try { PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(src); for (PropertyDescriptor pd : pds) { String name = pd.getName(); if (pd.getWriteMethod() != null) { Object value = PropertyUtils.getProperty(src, name); boolean copy = true; if (value == null) { copy = false; } else if (value instanceof List && ((List) value).size() < 1) { copy = false; } else if (value.getClass().isArray() && ((Object[]) value).length < 1) { copy = false; } if (copy) { PropertyUtils.setProperty(dest, name, value); } } } // BeanUtils.copyProperties(dest, src); } catch (Exception e) { logger.error(e.getMessage()); throw (new RuntimeException(e)); } }
From source file:com.zigbee.framework.common.util.BeanCopyUtil.java
/** * ?//from ww w. ja v a2 s . c o m * @author tun.tan * @Date 2011-9-29 ?11:18:42 */ public static void copyProps(Object source, Object target, String[] igonre) { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); Object methodName = null; // Object targetValue=null; //? for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { try { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (igonre != null) { boolean jump = false; // for (int i = 0; i < igonre.length; i++) { if (igonre[i] == null) {// continue; } if (targetPd.getName().equals(igonre[i])) {//? logger.info(targetPd.getName()); jump = true; } } if (jump) {// continue; } } 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 // targetValue=value; if (value != null) {// Method writeMethod = targetPd.getWriteMethod(); methodName = writeMethod.getName(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } writeMethod.invoke(target, value); } } } catch (BeansException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception! methodName=" + methodName + " ;" + e.getMessage(); logger.error(errMsg); throw new ZigbeeSystemException("beanCopyException", errMsg, e.getCause()); } catch (SecurityException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception! methodName=" + methodName + " ;" + e.getMessage(); logger.error(errMsg); throw new ZigbeeSystemException("beanCopyException", errMsg, e.getCause()); } catch (IllegalArgumentException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception! methodName=" + methodName + " ;" + e.getMessage(); logger.error(errMsg); throw new ZigbeeSystemException("beanCopyException", errMsg, e.getCause()); } catch (IllegalAccessException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception! methodName=" + methodName + " ;" + e.getMessage(); logger.error(errMsg); throw new ZigbeeSystemException("beanCopyException", errMsg, e.getCause()); } catch (InvocationTargetException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception! methodName=" + methodName + " ;" + e.getMessage(); logger.error(errMsg); throw new ZigbeeSystemException("beanCopyException", errMsg, e.getCause()); } } } }
From source file:BeanUtil.java
/** * <p>Sets the specified attribute on the specified object. For example, * <code>getObjectAttribute(o, "address.line1", value)</code> will call * <code>o.getAddress().setLine1(value)</code>.<p> * * <p>The attribute specified may contain as many levels as you like. If at * any time a null reference is acquired by calling one of the successive * getter methods, then a <code>NullPointerException</code> is thrown.</p> * * @param bean the bean to call the getters on * @param propertyNames the name of the attribute(s) to set. If this is * null or the empty string, then an exception is thrown. * @param value the value of the object to set on the bean property * * @throws PropertyNotFoundException indicates the the given property * could not be found on the bean * @throws IllegalArgumentException if the supplied parameter is not of * a valid type/*from w ww. j a v a2 s . c o m*/ * @throws NoSuchMethodException never * @throws IllegalAccessException if a getter or setter method is * not public or property is read-only. * @throws InvocationTargetException if a specified getter method throws an * exception. */ public static void setObjectAttribute(Object bean, String propertyNames, Object value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException { Object result = bean; String propertyName = propertyNames; // Check if this has some embedded properties. If it does, use the // getObjectAttribute to get the proper object to call this on. int indexOfLastPropertySeparator = propertyName.lastIndexOf(PROPERTY_SEPARATOR); if (indexOfLastPropertySeparator >= 0) { String embeddedProperties = propertyName.substring(0, indexOfLastPropertySeparator); // Grab the final property name after the last property separator propertyName = propertyName.substring(indexOfLastPropertySeparator + PROPERTY_SEPARATOR.length()); result = getObjectAttribute(result, embeddedProperties); } Class resultClass = result.getClass(); PropertyDescriptor propertyDescriptor = getPropertyDescriptor(propertyName, resultClass); Method writeMethod = propertyDescriptor.getWriteMethod(); if (writeMethod == null) { throw new IllegalAccessException( "User is attempting to write " + "to a property that has no write method. This is likely " + "a read-only bean property. Caused by property [" + propertyName + "] on class [" + resultClass + "]"); } writeMethod.invoke(result, new Object[] { value }); }
From source file:index.IndexUtils.java
/** * ??/*w w w .j a v a 2s . c o m*/ * @param doc * @param entityPath * @return * @throws ClassNotFoundException * @throws java.beans.IntrospectionException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws java.lang.reflect.InvocationTargetException * @throws InstantiationException * @throws NoSuchMethodException */ public static Object getIndexResult(Document doc, String entityPath) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException { // ?? Class c2 = Class.forName(entityPath); // Object obj = c2.getConstructor(new Class[] {}).newInstance(); // Field[] fields = c2.getDeclaredFields(); // ?? for (Field field : fields) { // ?? PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c2); // setget Method method = pd.getWriteMethod(); // method.invoke(obj, new Object[] { doc.get(field.getName()) }); } return obj; }
From source file:com.evolveum.midpoint.provisioning.ucf.api.UcfUtil.java
public static boolean hasAnnotation(PropertyDescriptor prop, Class<? extends Annotation> annotationClass) { Method readMethod = prop.getReadMethod(); if (readMethod != null && readMethod.getAnnotation(annotationClass) != null) { return true; }//from www. j a v a 2 s.co m Method writeMethod = prop.getWriteMethod(); if (writeMethod != null && writeMethod.getAnnotation(annotationClass) != null) { return true; } Class<?> propertyType = prop.getPropertyType(); if (propertyType.isAnnotationPresent(annotationClass)) { return true; } return false; }
From source file:eagle.storage.jdbc.entity.JdbcEntitySerDeserHelper.java
/** * * @param row//ww w . jav a 2 s. com * @param entityDefinition * @param <E> * @return * @throws IllegalAccessException * @throws InstantiationException * @throws InvocationTargetException * @throws NoSuchMethodException */ public static <E extends TaggedLogAPIEntity> E buildEntity(Map<String, Object> row, JdbcEntityDefinition entityDefinition) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { EntityDefinition ed = entityDefinition.getInternal(); Class<? extends TaggedLogAPIEntity> clazz = ed.getEntityClass(); if (clazz == null) { throw new NullPointerException("Entity class of service " + ed.getService() + " is null"); } TaggedLogAPIEntity obj = clazz.newInstance(); Map<String, Qualifier> map = ed.getDisplayNameMap(); for (Map.Entry<String, Object> entry : row.entrySet()) { // timestamp; if (JdbcConstants.TIMESTAMP_COLUMN_NAME.equals(entry.getKey())) { obj.setTimestamp((Long) entry.getValue()); continue; } // set metric as prefix for generic metric if (entityDefinition.getInternal().getService().equals(GenericMetricEntity.GENERIC_METRIC_SERVICE) && JdbcConstants.METRIC_NAME_COLUMN_NAME.equals(entry.getKey())) { obj.setPrefix((String) entry.getValue()); continue; } // rowkey: uuid if (JdbcConstants.ROW_KEY_COLUMN_NAME.equals(entry.getKey())) { obj.setEncodedRowkey((String) entry.getValue()); continue; } Qualifier q = map.get(entry.getKey().toLowerCase()); if (q == null) { // if it's not pre-defined qualifier, it must be tag unless it's a bug if (obj.getTags() == null) { obj.setTags(new HashMap<String, String>()); } obj.getTags().put(entry.getKey(), (String) entry.getValue()); continue; } // parse different types of qualifiers String fieldName = q.getDisplayName(); // PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(obj, fieldName); PropertyDescriptor pd = getPropertyDescriptor(obj, fieldName); if (entry.getValue() != null) { pd.getWriteMethod().invoke(obj, entry.getValue()); } } if (!entityDefinition.getInternal().getService().equals(GenericMetricEntity.GENERIC_METRIC_SERVICE)) { obj.setPrefix(entityDefinition.getInternal().getPrefix()); } return (E) obj; }
From source file:org.apache.activemq.artemis.tests.integration.jms.connection.ConnectionFactorySerializationTest.java
private static void checkEquals(Object factory, Object factory2) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { BeanUtilsBean bean = new BeanUtilsBean(); PropertyDescriptor[] descriptors = bean.getPropertyUtils().getPropertyDescriptors(factory); for (PropertyDescriptor descriptor : descriptors) { if (descriptor.getWriteMethod() != null && descriptor.getReadMethod() != null) { Assert.assertEquals(descriptor.getName() + " incorrect", bean.getProperty(factory, descriptor.getName()), bean.getProperty(factory2, descriptor.getName())); }//w ww.j a v a2 s. com } }
From source file:org.shaman.rpg.editor.dbviewer.TreeTableModelFactory.java
private static String getFieldName(Method method) { try {/*www. j av a2 s.c o m*/ Class<?> clazz = method.getDeclaringClass(); BeanInfo info = Introspector.getBeanInfo(clazz); PropertyDescriptor[] props = info.getPropertyDescriptors(); for (PropertyDescriptor pd : props) { if (method.equals(pd.getWriteMethod()) || method.equals(pd.getReadMethod())) { // System.out.println(pd.getDisplayName()); return pd.getName(); } } } catch (Exception e) { LOG.log(Level.FINE, "unable to get field name for method " + method, e); } return method.getName(); }
From source file:PropertyUtils.java
/** * Set specified property value/* w w w.j a v a 2 s .co m*/ */ public static void setProperty(Object bean, String property, Object value) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(), property); if (descriptor == null) throw new NoSuchMethodException("Cannot find property " + bean.getClass().getName() + "." + property); Method method = descriptor.getWriteMethod(); if (method == null) throw new NoSuchMethodException("Cannot find setter for " + bean.getClass().getName() + "." + property); method.invoke(bean, new Object[] { value }); }
From source file:com.fantasia.snakerflow.engine.SnakerHelper.java
private static String getProperty(NodeModel node) { StringBuffer buffer = new StringBuffer(); buffer.append("props:{"); try {/* ww w .j a v a2s . c om*/ PropertyDescriptor[] beanProperties = PropertyUtils.getPropertyDescriptors(node); for (PropertyDescriptor propertyDescriptor : beanProperties) { if (propertyDescriptor.getReadMethod() == null || propertyDescriptor.getWriteMethod() == null) continue; String name = propertyDescriptor.getName(); String value = ""; if (propertyDescriptor.getPropertyType() == String.class) { value = (String) BeanUtils.getProperty(node, name); } else { continue; } if (value == null || value.equals("")) continue; buffer.append(name); buffer.append(":{value:'"); buffer.append(convert(value)); buffer.append("'},"); } } catch (Exception e) { e.printStackTrace(); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("}}"); return buffer.toString(); }