List of usage examples for org.springframework.beans BeanWrapper isReadableProperty
boolean isReadableProperty(String propertyName);
From source file:org.terasoluna.gfw.web.el.ObjectToMapConverter.java
/** * Convert the given object to the flattened map. * <p>/* w w w .j av a2 s .com*/ * Return empty map if the object is null. * </p> * @param prefix prefix of the key * @param object object to convert * @return converted map. all keys are prefixed with the given key * @see ObjectToMapConverter */ public Map<String, String> convert(String prefix, Object object) { Map<String, String> map = new LinkedHashMap<String, String>(); // at first, try to flatten the given object if (flatten(map, "", prefix, object, null)) { return map; } // the given object is a Java Bean BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(object); PropertyDescriptor[] pds = beanWrapper.getPropertyDescriptors(); // flatten properties in the given object for (PropertyDescriptor pd : pds) { String name = pd.getName(); if ("class".equals(name) || !beanWrapper.isReadableProperty(name)) { continue; } Object value = beanWrapper.getPropertyValue(name); TypeDescriptor sourceType = beanWrapper.getPropertyTypeDescriptor(name); if (!flatten(map, prefix, name, value, sourceType)) { // the property can be a Java Bean // convert recursively Map<String, String> subMap = this.convert(name, value); map.putAll(subMap); } } return map; }
From source file:org.brushingbits.jnap.common.bean.visitor.BeanPropertyVisitor.java
protected void handleBean(Object source, Class<?> type) { context.nextLevel();// w w w. j a va 2 s . co m visitBean(source, type); BeanWrapper sourceBean = PropertyAccessorFactory.forBeanPropertyAccess(source); PropertyDescriptor[] beanProperties = BeanUtils.getPropertyDescriptors(type); for (PropertyDescriptor propertyDescriptor : beanProperties) { String name = propertyDescriptor.getName(); context.pushPath(name); if (sourceBean.isReadableProperty(name)) { Object value = sourceBean.getPropertyValue(name); visit(value); } context.popPath(); } context.prevLevel(); }
From source file:net.solarnetwork.web.support.JSONView.java
private void generateJavaBeanObject(JsonGenerator json, String key, Object bean, PropertyEditorRegistrar registrar) throws JsonGenerationException, IOException { if (key != null) { json.writeFieldName(key);/*from www . j a v a2s. co m*/ } if (bean == null) { json.writeNull(); return; } BeanWrapper wrapper = getPropertyAccessor(bean, registrar); PropertyDescriptor[] props = wrapper.getPropertyDescriptors(); json.writeStartObject(); for (PropertyDescriptor prop : props) { String name = prop.getName(); if (this.getJavaBeanIgnoreProperties() != null && this.getJavaBeanIgnoreProperties().contains(name)) { continue; } if (wrapper.isReadableProperty(name)) { Object propVal = wrapper.getPropertyValue(name); if (propVal != null) { // test for SerializeIgnore Method getter = prop.getReadMethod(); if (getter != null && getter.isAnnotationPresent(SerializeIgnore.class)) { continue; } if (getPropertySerializerRegistrar() != null) { propVal = getPropertySerializerRegistrar().serializeProperty(name, propVal.getClass(), bean, propVal); } else { // Spring does not apply PropertyEditors on read methods, so manually handle PropertyEditor editor = wrapper.findCustomEditor(null, name); if (editor != null) { editor.setValue(propVal); propVal = editor.getAsText(); } } if (propVal instanceof Enum<?> || getJavaBeanTreatAsStringValues() != null && getJavaBeanTreatAsStringValues().contains(propVal.getClass())) { propVal = propVal.toString(); } writeJsonValue(json, name, propVal, registrar); } } } json.writeEndObject(); }
From source file:net.solarnetwork.web.support.SimpleCsvView.java
private List<String> getCSVFields(Object row, final Collection<String> fieldOrder) { assert row != null; List<String> result = new ArrayList<String>(); if (row instanceof Map) { Map<?, ?> map = (Map<?, ?>) row; if (fieldOrder != null) { for (String key : fieldOrder) { result.add(key);/*www.j av a 2 s . c o m*/ } } else { for (Object key : map.keySet()) { result.add(key.toString()); } } } else { // use bean properties if (getPropertySerializerRegistrar() != null) { // try whole-bean serialization first Object o = getPropertySerializerRegistrar().serializeProperty("row", row.getClass(), row, row); if (o != row) { if (o != null) { result = getCSVFields(o, fieldOrder); return result; } } } BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(row); PropertyDescriptor[] props = wrapper.getPropertyDescriptors(); Set<String> resultSet = new LinkedHashSet<String>(); for (PropertyDescriptor prop : props) { String name = prop.getName(); if (getJavaBeanIgnoreProperties() != null && getJavaBeanIgnoreProperties().contains(name)) { continue; } if (wrapper.isReadableProperty(name)) { // test for SerializeIgnore Method getter = prop.getReadMethod(); if (getter != null && getter.isAnnotationPresent(SerializeIgnore.class)) { continue; } resultSet.add(name); } } if (fieldOrder != null && fieldOrder.size() > 0) { for (String key : fieldOrder) { if (resultSet.contains(key)) { result.add(key); } } } else { result.addAll(resultSet); } } return result; }
From source file:org.brushingbits.jnap.common.bean.cloning.BeanCloner.java
private Object cloneBean(Object bean, Class<?> type) { BeanWrapper source = PropertyAccessorFactory.forBeanPropertyAccess(bean); BeanWrapper copy = PropertyAccessorFactory.forBeanPropertyAccess(BeanUtils.instantiate(type)); // keep instance for circular and multiple references context.setAsVisited(bean);/*w w w . j a v a 2s .com*/ alreadyCloned.put(bean, copy.getWrappedInstance()); PropertyDescriptor[] beanProperties = copy.getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : beanProperties) { String name = propertyDescriptor.getName(); context.pushPath(name); if (copy.isReadableProperty(name) && copy.isWritableProperty(name)) { Object value = source.getPropertyValue(name); copy.setPropertyValue(name, clone(value)); } context.popPath(); } Object beanCopy = copy.getWrappedInstance(); source = null; copy = null; return beanCopy; }
From source file:com.emc.ecs.sync.service.SyncJobService.java
protected void copyProperties(Object source, Object target, String... ignoredProperties) { List<String> ignoredList = Collections.emptyList(); if (ignoredProperties != null) ignoredList = Arrays.asList(ignoredProperties); BeanWrapper wSource = new BeanWrapperImpl(source), wTarget = new BeanWrapperImpl(target); wSource.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(ISO_8601_FORMAT), true)); wTarget.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(ISO_8601_FORMAT), true)); for (PropertyDescriptor descriptor : wSource.getPropertyDescriptors()) { if (ignoredList.contains(descriptor.getName())) continue; if (!wSource.isReadableProperty(descriptor.getName())) continue; if (wTarget.isWritableProperty(descriptor.getName())) { // property is readable from source, writeable on target, and not in the list of ignored props Object value = wSource.getPropertyValue(descriptor.getName()); if (value != null) wTarget.setPropertyValue(descriptor.getName(), value); }//from w w w. j a v a 2 s. com } }
From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java
private Object getPropertyValueForPath(Object target, String[] propertyNames) { BeanWrapper wrapper = new BeanWrapperImpl(target); Object obj = target;//from w ww . ja va 2s . c o m for (int i = 0; i < propertyNames.length - 1; i++) { String propertyName = propertyNames[i]; if (wrapper.isReadableProperty(propertyName)) { obj = wrapper.getPropertyValue(propertyName); if (obj == null) break; wrapper = new BeanWrapperImpl(obj); } } return obj; }
From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java
@Test public void testIsReadablePropertyNotReadable() { NoRead nr = new NoRead(); BeanWrapper bw = new JuffrouSpringBeanWrapper(nr); assertFalse(bw.isReadableProperty("age")); }
From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java
/** * Shouldn't throw an exception: should just return false *///from w w w. j a v a 2s .c o m @Test public void testIsReadablePropertyNoSuchProperty() { NoRead nr = new NoRead(); BeanWrapper bw = new JuffrouSpringBeanWrapper(nr); assertFalse(bw.isReadableProperty("xxxxx")); }
From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java
@Test public void testIsReadablePropertyNull() { NoRead nr = new NoRead(); BeanWrapper bw = new JuffrouSpringBeanWrapper(nr); try {/*w w w . ja v a2s . c om*/ bw.isReadableProperty(null); fail("Can't inquire into readability of null property"); } catch (IllegalArgumentException ex) { // expected } }