List of usage examples for java.beans PropertyDescriptor getName
public String getName()
From source file:com.ksmpartners.ernie.util.TestUtil.java
/** * Checks equality of two objects based on the equality of their fields, items, or .equals() method. * @param obj1/* w w w. ja v a 2 s . com*/ * @param obj2 * @return */ public static <T> boolean equal(T obj1, T obj2) { if (obj1 == null || obj2 == null) { // If they're both null, we call this equal if (obj1 == null && obj2 == null) return true; else return false; } if (!obj1.getClass().equals(obj2.getClass())) return false; if (obj1.equals(obj2)) return true; List<Pair> vals = new ArrayList<Pair>(); // If obj1 and obj2 are Collections, get the objects in them if (Collection.class.isAssignableFrom(obj1.getClass())) { Collection c1 = (Collection) obj1; Collection c2 = (Collection) obj2; if (c1.size() != c2.size()) return false; Iterator itr1 = c1.iterator(); Iterator itr2 = c2.iterator(); while (itr1.hasNext() && itr2.hasNext()) { vals.add(new Pair(itr1.next(), itr2.next())); } } // Get field values from obj1 and obj2 PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(obj1); for (PropertyDescriptor property : properties) { // ignore getClass() and isEmpty() if (property.getName().equals("class") || property.getName().equals("empty")) continue; Object val1 = invokeMethod(obj1, property.getReadMethod(), null, property.getName()); Object val2 = invokeMethod(obj2, property.getReadMethod(), null, property.getName()); vals.add(new Pair(val1, val2)); } if (vals.isEmpty()) return false; for (Pair pair : vals) { if (!equal(pair.left, pair.right)) return false; } return true; }
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 w w w . jav a 2s . c o 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 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:org.kmnet.com.fw.web.el.Functions.java
/** * build query string from map or bean./*from www.ja v a2 s . c o m*/ * <p> * query string is encoded with "UTF-8". * </p> * @param params map or bean * @return query string. returns empty string if <code>params</code> is <code>null</code> or empty string or * {@link Iterable} or {@link BeanUtils#isSimpleValueType(Class)}. */ @SuppressWarnings("unchecked") public static String query(Object params) { if (params == null) { return ""; } Class<?> clazz = params.getClass(); if (clazz.isArray() || params instanceof Iterable || BeanUtils.isSimpleValueType(clazz)) { return ""; } String query; if (params instanceof Map) { query = mapToQuery((Map<String, Object>) params); } else { Map<String, Object> map = new LinkedHashMap<String, Object>(); BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(params); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(clazz); for (PropertyDescriptor pd : pds) { String name = pd.getName(); if (!"class".equals(name)) { Object value = beanWrapper.getPropertyValue(name); map.put(name, value); } } query = mapToQuery(map, beanWrapper); } return query; }
From source file:com.twinsoft.convertigo.beans.CheckBeans.java
private static PropertyDescriptor isBeanProperty(String fieldName, MySimpleBeanInfo dboBeanInfo) { if (dboBeanInfo == null) return null; PropertyDescriptor[] propertyDescriptors = dboBeanInfo.getLocalPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getName().equals(fieldName)) return propertyDescriptor; }//from ww w. j a v a 2 s .c om return null; }
From source file:cn.fql.utility.ClassUtility.java
/** * Export property value of specified object to a map * * @param obj object instance * @param isWithNullable denote whether it is required to skip null value * @return map instance includes property value */// www.j a va 2s . com public static Map collectAsMap(Object obj, boolean isWithNullable) { Map mapOutput = new HashMap(); PropertyDescriptor[] pds; try { pds = exportPropertyDesc(obj.getClass()); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; Method getter = pd.getReadMethod(); if (getter != null && !pd.getName().equals("class")) { Object value = getter.invoke(obj); if (value == null) { if (!isWithNullable) { continue; } } if (pd.getPropertyType().equals(String.class) && value == null) { value = ""; } mapOutput.put(pd.getName(), value); } } } catch (Exception e) { System.out.println(e.getMessage()); } return mapOutput; }
From source file:com.zigbee.framework.common.util.BeanCopyUtil.java
/** * Override copyProperties Method// w w w . jav a2 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.github.sevntu.checkstyle.internal.ChecksTest.java
private static Set<String> getProperties(Class<?> clss) { final Set<String> result = new TreeSet<>(); final PropertyDescriptor[] map = PropertyUtils.getPropertyDescriptors(clss); for (PropertyDescriptor p : map) { if (p.getWriteMethod() != null) { result.add(p.getName()); }/*from w ww. j a va2s. c o m*/ } return result; }
From source file:com.fantasia.snakerflow.engine.SnakerHelper.java
private static String getProperty(NodeModel node) { StringBuffer buffer = new StringBuffer(); buffer.append("props:{"); try {/*from w ww.j a v a2 s . c o m*/ 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(); }
From source file:org.shept.util.BeanUtilsExtended.java
/** * Find all occurrences of targetClass in targetObject. * Be careful. This stuff may be recursive ! * Should be improved to prevent endless recursive loops. * //from ww w .java 2 s. c o m * @param * @return * * @param targetObject * @param targetClass * @param nestedPath * @return * @throws Exception */ public static Map<String, ?> findNestedPaths(Object targetObject, Class<?> targetClass, String nestedPath, List<String> ignoreList, Set<Object> visited, int maxDepth) throws Exception { Assert.notNull(targetObject); Assert.notNull(targetClass); Assert.notNull(ignoreList); HashMap<String, Object> nestedPaths = new HashMap<String, Object>(); if (maxDepth <= 0) { return nestedPaths; } nestedPath = (nestedPath == null ? "" : nestedPath); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(targetObject); PropertyDescriptor[] props = bw.getPropertyDescriptors(); for (PropertyDescriptor pd : props) { Class<?> clazz = pd.getPropertyType(); if (clazz != null && !clazz.equals(Class.class) && !clazz.isAnnotation() && !clazz.isPrimitive()) { Object value = null; String pathName = pd.getName(); if (!ignoreList.contains(pathName)) { try { value = bw.getPropertyValue(pathName); } catch (Exception e) { } // ignore any exceptions here if (StringUtils.hasText(nestedPath)) { pathName = nestedPath + PropertyAccessor.NESTED_PROPERTY_SEPARATOR + pathName; } // TODO break up this stuff into checking and excecution a la ReflectionUtils if (targetClass.isAssignableFrom(clazz)) { nestedPaths.put(pathName, value); } // exclude objects already visited from further inspection to prevent circular references // unfortunately this stuff isn't fool proof as there are ConcurrentModificationExceptions // when adding objects to the visited list if (value != null && !isInstanceVisited(visited, value)) { nestedPaths.putAll( findNestedPaths(value, targetClass, pathName, ignoreList, visited, maxDepth - 1)); } } } } return nestedPaths; }
From source file:com.zigbee.framework.common.util.BeanCopyUtil.java
/** * ?//from www. ja v a2s . 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()); } } } }