@Override public PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException { BeanWrapperImpl nestedBw = (BeanWrapperImpl) getPropertyAccessorForPropertyPath(propertyName); String finalPath = getFinalPath(nestedBw, propertyName); PropertyDescriptor pd = nestedBw.getCachedIntrospectionResults().getPropertyDescriptor(finalPath); if (pd == null) { throw new InvalidPropertyException(getRootClass(), getNestedPath() + propertyName, "No property '" + propertyName + "' found"); } return pd; }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forField(propertyName, getRootClass()); throw new NotWritablePropertyException( getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException(getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }
/** * Overloaded version of {@code addPropertyValue} that takes * a property name and a property value. * <p>Note: As of Spring 3.0, we recommend using the more concise * and chaining-capable variant {@link #add}. * @param propertyName name of the property * @param propertyValue value of the property * @see #addPropertyValue(PropertyValue) */ public void addPropertyValue(String propertyName, Object propertyValue) { addPropertyValue(new PropertyValue(propertyName, propertyValue)); }
@Override public void setPropertyValue(PropertyValue pv) throws BeansException { setPropertyValue(pv.getName(), pv.getValue()); }
/** * Create a new accessor, wrapping a new instance of the specified class. * @param clazz class to instantiate and wrap */ protected AbstractNestablePropertyAccessor(Class<?> clazz) { registerDefaultEditors(); setWrappedInstance(BeanUtils.instantiateClass(clazz)); }
@Override public void setPropertyValues(Map<?, ?> map) throws BeansException { setPropertyValues(new MutablePropertyValues(map)); }
/** * Return property values for this bean (never {@code null}). */ @Override public MutablePropertyValues getPropertyValues() { if (this.propertyValues == null) { this.propertyValues = new MutablePropertyValues(); } return this.propertyValues; }
@Test public void notWritablePropertyExceptionContainsAlternativeMatch() { IntelliBean target = new IntelliBean(); BeanWrapper bw = createAccessor(target); try { bw.setPropertyValue("names", "Alef"); } catch (NotWritablePropertyException ex) { assertNotNull("Possible matches not determined", ex.getPossibleMatches()); assertEquals("Invalid amount of alternatives", 1, ex.getPossibleMatches().length); } }
public BeanPropertyMatches(String propertyName, Class<?> beanClass, int maxDistance) { super(propertyName, calculateMatches(propertyName, BeanUtils.getPropertyDescriptors(beanClass), maxDistance)); }
private PropertyValue createDefaultPropertyValue(PropertyTokenHolder tokens) { TypeDescriptor desc = getPropertyTypeDescriptor(tokens.canonicalName); if (desc == null) { throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + tokens.canonicalName, "Could not determine property type for auto-growing a default value"); } Object defaultValue = newValue(desc.getType(), desc, tokens.canonicalName); return new PropertyValue(tokens.canonicalName, defaultValue); }
/** * Override default editor, if possible (since that's what we really mean to do here); * otherwise register as a custom editor. */ private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) { if (registry instanceof PropertyEditorRegistrySupport) { ((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor); } else { registry.registerCustomEditor(requiredType, editor); } }
/** * Obtain a lazily initialized CachedIntrospectionResults instance * for the wrapped object. */ private CachedIntrospectionResults getCachedIntrospectionResults() { if (this.cachedIntrospectionResults == null) { this.cachedIntrospectionResults = CachedIntrospectionResults.forClass(getWrappedClass()); } return this.cachedIntrospectionResults; }
@Override @Nullable protected BeanPropertyHandler getLocalPropertyHandler(String propertyName) { PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(propertyName); return (pd != null ? new BeanPropertyHandler(pd) : null); }
/** * Returns the canonical property name. * @see org.springframework.beans.PropertyAccessorUtils#canonicalPropertyName */ @Override protected String canonicalFieldName(String field) { return PropertyAccessorUtils.canonicalPropertyName(field); }
@Override public PropertyDescriptor[] getPropertyDescriptors() { return getCachedIntrospectionResults().getPropertyDescriptors(); }
public SimpleTypeConverter() { this.typeConverterDelegate = new TypeConverterDelegate(this); registerDefaultEditors(); }
/** * Create a new empty accessor. Wrapped instance needs to be set afterwards. * @param registerDefaultEditors whether to register default editors * (can be suppressed if the accessor won't need any type conversion) * @see #setWrappedInstance */ protected AbstractNestablePropertyAccessor(boolean registerDefaultEditors) { if (registerDefaultEditors) { registerDefaultEditors(); } this.typeConverterDelegate = new TypeConverterDelegate(this); }
@Test public void notWritablePropertyExceptionContainsAlternativeMatches() { IntelliBean target = new IntelliBean(); BeanWrapper bw = createAccessor(target); try { bw.setPropertyValue("mystring", "Arjen"); } catch (NotWritablePropertyException ex) { assertNotNull("Possible matches not determined", ex.getPossibleMatches()); assertEquals("Invalid amount of alternatives", 3, ex.getPossibleMatches().length); } }