List of usage examples for org.springframework.beans BeanWrapperImpl getPropertyType
@Override @Nullable public Class<?> getPropertyType(String propertyName) throws BeansException
From source file:org.springjutsu.validation.ValidationErrorMessageHandler.java
/** * If we're trying to resolve the message key for a path on the model, * this method will unwrap that message key. * For instance, consider our model is a Account instance, which has a * field accountOwner of type User, and that User object has a * username field of type String://from w w w .j a v a 2 s.c om * If rulePath was "accountOwner.username", then it would return a * message key of "user.username", which is the simple classname of the * owning object of the failed validation path, and the field name. * This is so we can display the label of the field that failed validation * in the error message. For instance "User Name must be 8 chars" instead * of something cryptic like "accountOwner.username must be 8 chars". * @param rulePath Validation rule path to the failed field. * @param rootModel The root model owning the field that failed. * @return A message key used to resolve a message describing the field * that failed. */ protected String getModelMessageKey(String rawRulePath, Object rootModel) { if (rawRulePath == null || rawRulePath.length() < 1) { return rawRulePath; } // clean up any collection and/or map indexing paths from last path segment. String rulePath = rawRulePath.trim().replaceAll("\\[[^\\]]+\\]$", ""); Class<?> parentType = null; String fieldPath = null; if (rulePath.contains(".")) { fieldPath = rulePath.substring(rulePath.lastIndexOf(".") + 1); String parentPath = rulePath.substring(0, rulePath.lastIndexOf(".")); BeanWrapperImpl beanWrapper = new BeanWrapperImpl(rootModel); parentType = beanWrapper.getPropertyType(parentPath); } else { fieldPath = rulePath; parentType = rootModel.getClass(); } if (enableSuperclassFieldLabelLookup) { MessageSourceAccessor messageSourceAccessor = new MessageSourceAccessor(messageSource); Class<?> messageBearingType = parentType; while (messageBearingType != null) { if (!messageSourceAccessor .getMessage(buildMessageKey(messageBearingType, fieldPath), "MessageNotFound") .equals("MessageNotFound")) { break; } else { messageBearingType = messageBearingType.getSuperclass(); } } if (messageBearingType != null) { parentType = messageBearingType; } } return buildMessageKey(parentType, fieldPath); }
From source file:org.archive.crawler.restlet.BeanBrowseResource.java
/** * Constructs a nested Map data structure with the information represented * by this Resource. The result is particularly suitable for use with with * {@link XmlMarshaller}.// ww w .ja va 2 s . c om * * @return the nested Map data structure */ protected BeansModel makeDataModel() { Object bean = null; String problem = null; boolean editable = false; Object target = null; if (StringUtils.isNotBlank(beanPath)) { try { int firstDot = beanPath.indexOf("."); String beanName = firstDot < 0 ? beanPath : beanPath.substring(0, firstDot); Object namedBean = appCtx.getBean(beanName); if (firstDot < 0) { target = namedBean; bean = makePresentableMapFor(null, target, beanPath); } else { BeanWrapperImpl bwrap = new BeanWrapperImpl(namedBean); String propPath = beanPath.substring(firstDot + 1); target = bwrap.getPropertyValue(propPath); Class<?> type = bwrap.getPropertyType(propPath); if (bwrap.isWritableProperty(propPath) && (bwrap.getDefaultEditor(type) != null || type == String.class) && !Collection.class.isAssignableFrom(type)) { editable = true; bean = makePresentableMapFor(null, target); } else { bean = makePresentableMapFor(null, target, beanPath); } } } catch (BeansException e) { problem = e.toString(); } } Collection<Object> nestedNames = new LinkedList<Object>(); Set<Object> alreadyWritten = new HashSet<Object>(); addPresentableNestedNames(nestedNames, appCtx.getBean("crawlController"), alreadyWritten); for (String name : appCtx.getBeanDefinitionNames()) { addPresentableNestedNames(nestedNames, appCtx.getBean(name), alreadyWritten); } return new BeansModel(cj.getShortName(), new Reference(getRequest().getResourceRef().getBaseRef(), "..").getTargetRef().toString(), beanPath, bean, editable, problem, target, nestedNames); }
From source file:com.siberhus.tdfl.mapping.BeanWrapLineMapper.java
private Object getPropertyValue(Object bean, String nestedName) { BeanWrapperImpl wrapper = new BeanWrapperImpl(bean); Object nestedValue = wrapper.getPropertyValue(nestedName); if (nestedValue == null) { try {/*from www. jav a 2 s.c o m*/ nestedValue = wrapper.getPropertyType(nestedName).newInstance(); wrapper.setPropertyValue(nestedName, nestedValue); } catch (InstantiationException e) { ReflectionUtils.handleReflectionException(e); } catch (IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } } return nestedValue; }
From source file:org.archive.spring.Sheet.java
/** * Ensure any properties targetted by this Sheet know to * check the right property paths for overrides at lookup time, * and that the override values are compatible types for their * destination properties. /*from w w w. jav a 2s . c o m*/ * * Should be done as soon as all possible targets are * constructed (ApplicationListener ContextRefreshedEvent) * * TODO: consider if an 'un-priming' also needs to occur to * prevent confusing side-effects. * TODO: consider if priming should move to another class */ public void prime() { for (String fullpath : map.keySet()) { int lastDot = fullpath.lastIndexOf("."); String beanPath = fullpath.substring(0, lastDot); String terminalProp = fullpath.substring(lastDot + 1); Object value = map.get(fullpath); int i = beanPath.indexOf("."); Object bean; HasKeyedProperties hkp; if (i < 0) { bean = beanFactory.getBean(beanPath); } else { String beanName = beanPath.substring(0, i); String propPath = beanPath.substring(i + 1); BeanWrapperImpl wrapper = new BeanWrapperImpl(beanFactory.getBean(beanName)); bean = wrapper.getPropertyValue(propPath); } try { hkp = (HasKeyedProperties) bean; } catch (ClassCastException cce) { // targetted bean has no overridable properties throw new TypeMismatchException(bean, HasKeyedProperties.class, cce); } // install knowledge of this path hkp.getKeyedProperties().addExternalPath(beanPath); // verify type-compatibility BeanWrapperImpl wrapper = new BeanWrapperImpl(hkp); Class<?> requiredType = wrapper.getPropertyType(terminalProp); try { // convert for destination type map.put(fullpath, wrapper.convertForProperty(value, terminalProp)); } catch (TypeMismatchException tme) { TypeMismatchException tme2 = new TypeMismatchException( new PropertyChangeEvent(hkp, fullpath, wrapper.getPropertyValue(terminalProp), value), requiredType, tme); throw tme2; } } }
From source file:org.cloudfoundry.identity.uaa.config.YamlBindingTests.java
private BindingResult bind(Object target, String values) { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource[] { new ByteArrayResource(values.getBytes()) }); Map<Object, Object> map = factory.getObject(); DataBinder binder = new DataBinder(target) { @Override//from ww w .j av a 2 s . c om protected void doBind(MutablePropertyValues mpvs) { modifyProperties(mpvs, getTarget()); super.doBind(mpvs); } private void modifyProperties(MutablePropertyValues mpvs, Object target) { List<PropertyValue> list = mpvs.getPropertyValueList(); BeanWrapperImpl bw = new BeanWrapperImpl(target); for (int i = 0; i < list.size(); i++) { PropertyValue pv = list.get(i); String name = pv.getName(); StringBuilder builder = new StringBuilder(); for (String key : StringUtils.delimitedListToStringArray(name, ".")) { if (builder.length() != 0) { builder.append("."); } builder.append(key); String base = builder.toString(); Class<?> type = bw.getPropertyType(base); if (type != null && Map.class.isAssignableFrom(type)) { String suffix = name.substring(base.length()); Map<String, Object> nested = new LinkedHashMap<String, Object>(); if (bw.getPropertyValue(base) != null) { @SuppressWarnings("unchecked") Map<String, Object> existing = (Map<String, Object>) bw.getPropertyValue(base); nested = existing; } else { bw.setPropertyValue(base, nested); } Map<String, Object> value = nested; String[] tree = StringUtils.delimitedListToStringArray(suffix, "."); for (int j = 1; j < tree.length - 1; j++) { String subtree = tree[j]; value.put(subtree, nested); value = nested; } String refName = base + suffix.replaceAll("\\.([a-zA-Z0-9]*)", "[$1]"); mpvs.setPropertyValueAt(new PropertyValue(refName, pv.getValue()), i); break; } } } } }; binder.setIgnoreUnknownFields(false); LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.afterPropertiesSet(); binder.setValidator(validatorFactoryBean); binder.bind(new MutablePropertyValues(map)); binder.validate(); return binder.getBindingResult(); }