public static void add(Composable source, Composable target, double mnoznik) { BeanWrapper wrapperSource = new BeanWrapperImpl(source); BeanWrapper wrapperTarget = new BeanWrapperImpl(target); for (int i = 0; i < wrapperSource.getPropertyDescriptors().length; ++i) { String propName = wrapperSource.getPropertyDescriptors()[i].getName(); if (wrapperSource.isReadableProperty(propName)) { Object propValue = wrapperSource.getPropertyValue(propName); if (propValue != null) { if (propValue instanceof Composable) { if ((Composable) wrapperTarget.getPropertyValue(propName) == null) System.out.println(propName); add( (Composable) propValue, (Composable) wrapperTarget.getPropertyValue(propName), mnoznik); } else if (propValue instanceof Double) { if (wrapperTarget.getPropertyValue(propName) == null) wrapperTarget.setPropertyValue(propName, 0.0); wrapperTarget.setPropertyValue( propName, ((Double) wrapperTarget.getPropertyValue(propName)) + mnoznik * (Double) propValue); } } } } }
/** * Find all occurrences of targetClass in targetObject. Be careful. This stuff may be recursive ! * Should be improved to prevent endless recursive loops. * * @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; }
private void copyBoundProperties(User src, User dst) { BeanWrapper srcW = new BeanWrapperImpl(src); BeanWrapper dstW = new BeanWrapperImpl(dst); for (PropertyDescriptor srcProp : srcW.getPropertyDescriptors()) { if (srcProp.getReadMethod() == null || srcProp.getWriteMethod() == null) { continue; } Object srcValue = srcW.getPropertyValue(srcProp.getName()); if (srcValue != null) { dstW.setPropertyValue(srcProp.getName(), srcValue); } } }
protected void init(Object entity) { this.entity = entity; beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(entity); PropertyDescriptor[] descriptors = beanWrapper.getPropertyDescriptors(); if (descriptors.length > 1) { for (PropertyDescriptor descriptor : descriptors) { if (beanWrapper.isReadableProperty(descriptor.getName())) { privateFields.add(descriptor.getName()); } } } Field[] fields = BeanFields.get(entity.getClass()); if (fields != null) { for (Field field : fields) { publicFeilds.put(field.getName(), field); } } }
/** Bind controls following the same name convention or annotated with Property annotation. */ public void autobind() { BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(getModel()); PropertyAccessor viewPropertyAccessor = new DirectFieldAccessor(this); // Parse Property annotations List<AnnotatedElement> elements = AnnotatedElementAccessor.findAnnotatedElements(Property.class, getClass()); for (AnnotatedElement ae : elements) { Property p = ae.getAnnotation(Property.class); InitializationConfig config = getInitializationConfig(ae.getAnnotation(Initializer.class)); bindAndInitializeControl( p.value(), viewPropertyAccessor.getPropertyValue(((Field) ae).getName()), config); this.ignoredProperties.add(p.value()); } // Iterate on model properties for (PropertyDescriptor pd : bw.getPropertyDescriptors()) { String propertyName = pd.getName(); if (!ignoredProperties.contains(propertyName) && viewPropertyAccessor.isReadableProperty(propertyName)) { Object control = viewPropertyAccessor.getPropertyValue(propertyName); if (control != null) { if (log.isDebugEnabled()) log.debug( "Found control: " + control.getClass().getSimpleName() + " for property: " + propertyName); TypeDescriptor descriptor = viewPropertyAccessor.getPropertyTypeDescriptor(propertyName); InitializationConfig config = getInitializationConfig(descriptor.getAnnotation(Initializer.class)); // do bind bindAndInitializeControl(propertyName, control, config); } } } }
@Override public UserProfile getProfileForDisplay(UserProfile userProfile, boolean showPrivateFields) { UserProfile display = new UserProfile(); copyFields(userProfile, display); if (!showPrivateFields) { log.debug("Removing private fields for display on user: {}", userProfile.getDisplayName()); display.setOrganizationName(null); display.setOrganizationType(null); display.setPostalAddress(null); display.setPositionType(null); } // escape html in all string fields BeanWrapper wrapper = new BeanWrapperImpl(display); for (PropertyDescriptor property : wrapper.getPropertyDescriptors()) { if (String.class.isAssignableFrom(property.getPropertyType())) { String name = property.getName(); wrapper.setPropertyValue( name, TextUtils.escapeHtml((String) wrapper.getPropertyValue(name))); } } return display; }
private static void assertDepthEquals( final int depth, final String propertyName, final Object expected, Object actual) { if (expected == null && actual == null) { return; } else if (expected == null) { fail("expected " + propertyName + " was null but actual was not!"); } else if (actual == null) { fail("actual " + propertyName + " was null but expected was not!"); } final String assertionMessage = propertyName == null ? ("Top-level objects (" + expected.getClass().getName() + ") do not match.") : ("Properties " + propertyName + " do not match."); if (expected.getClass().getName().startsWith("java") || actual.getClass().getName().startsWith("java")) { // java primitives, just do assertEquals if (expected instanceof Object[] || actual instanceof Object[]) { assertTrue(assertionMessage, Arrays.equals((Object[]) expected, (Object[]) actual)); } else { assertEquals(assertionMessage, expected, actual); } return; } final BeanWrapper expectedWrapper = new BeanWrapperImpl(expected); final BeanWrapper actualWrapper = new BeanWrapperImpl(actual); final Set<String> properties = new TreeSet<String>(); for (final PropertyDescriptor descriptor : expectedWrapper.getPropertyDescriptors()) { properties.add(descriptor.getName()); } for (final PropertyDescriptor descriptor : actualWrapper.getPropertyDescriptors()) { properties.add(descriptor.getName()); } properties.remove("class"); for (final String property : properties) { final PropertyDescriptor expectedDescriptor = expectedWrapper.getPropertyDescriptor(property); final PropertyDescriptor actualDescriptor = actualWrapper.getPropertyDescriptor(property); if (expectedDescriptor != null && actualDescriptor != null) { // both have descriptors, so walk the sub-objects Object expectedValue = null; Object actualValue = null; try { expectedValue = expectedWrapper.getPropertyValue(property); } catch (final Exception e) { } try { actualValue = actualWrapper.getPropertyValue(property); } catch (final Exception e) { } assertDepthEquals(depth + 1, property, expectedValue, actualValue); } else if (expectedDescriptor != null) { fail("Should have '" + property + "' property on actual object, but there was none!"); } else if (actualDescriptor != null) { fail("Should have '" + property + "' property on expected object, but there was none!"); } } if (expected instanceof Object[] || actual instanceof Object[]) { final Object[] expectedArray = (Object[]) expected; final Object[] actualArray = (Object[]) actual; assertTrue(assertionMessage, Arrays.equals(expectedArray, actualArray)); } else { assertEquals(assertionMessage, expected, actual); } }