@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException(getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forField(propertyName, getRootClass()); throw new NotWritablePropertyException( getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }
private static Set<String> detectPotentialMatches(String propertyName, Class<?> type) { Set<String> result = new HashSet<>(); result.addAll(Arrays.asList(PropertyMatches.forField(propertyName, type).getPossibleMatches())); result.addAll(Arrays.asList(PropertyMatches.forProperty(propertyName, type).getPossibleMatches())); return result; } }
@Test public void simpleBeanPropertyTypo() { PropertyMatches matches = PropertyMatches.forProperty("naem", SampleBeanProperties.class); assertThat(matches.getPossibleMatches(), hasItemInArray("name")); }
@Test public void complexFieldPropertyTypo() { PropertyMatches matches = PropertyMatches.forField("desriptn", SampleFieldProperties.class); assertThat(matches.getPossibleMatches(), emptyArray()); }
@Test public void simpleBeanPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forProperty("naem", SampleBeanProperties.class); String msg = matches.buildErrorMessage(); assertThat(msg, containsString("naem")); assertThat(msg, containsString("name")); assertThat(msg, containsString("setter")); assertThat(msg, not(containsString("field"))); }
@Test public void simpleFieldPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forField("naem", SampleFieldProperties.class); String msg = matches.buildErrorMessage(); assertThat(msg, containsString("naem")); assertThat(msg, containsString("name")); assertThat(msg, containsString("field")); assertThat(msg, not(containsString("setter"))); }
/** * Create PropertyMatches for the given bean property. * @param propertyName the name of the property to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forProperty(String propertyName, Class<?> beanClass) { return forProperty(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
/** * Create PropertyMatches for the given field property. * @param propertyName the name of the field to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forField(String propertyName, Class<?> beanClass) { return forField(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
/** * Create PropertyMatches for the given bean property. * @param propertyName the name of the property to find possible matches for * @param beanClass the bean class to search for matches * @param maxDistance the maximum property distance allowed for matches */ public static PropertyMatches forProperty(String propertyName, Class beanClass, int maxDistance) { return new PropertyMatches(propertyName, beanClass, maxDistance); }
/** * Create a new PropertyMatches instance for the given property. */ private PropertyMatches(String propertyName, Class beanClass, int maxDistance) { this.propertyName = propertyName; this.possibleMatches = calculateMatches(BeanUtils.getPropertyDescriptors(beanClass), maxDistance); }
/** * Detects all potential matches for the given property name and type. * * @param propertyName must not be {@literal null} or empty. * @param type must not be {@literal null}. * @return */ private static Set<String> detectPotentialMatches(String propertyName, Class<?> type) { Set<String> result = new HashSet<>(); result.addAll(Arrays.asList(PropertyMatches.forField(propertyName, type).getPossibleMatches())); result.addAll(Arrays.asList(PropertyMatches.forProperty(propertyName, type).getPossibleMatches())); return result; } }
@Test public void unknownBeanProperty() { PropertyMatches matches = PropertyMatches.forProperty("unknown", SampleBeanProperties.class); assertThat(matches.getPossibleMatches(), emptyArray()); }
@Test public void simpleFieldPropertyTypo() { PropertyMatches matches = PropertyMatches.forField("naem", SampleFieldProperties.class); assertThat(matches.getPossibleMatches(), hasItemInArray("name")); }
@Test public void complexBeanPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forProperty("counter", SampleBeanProperties.class); String msg = matches.buildErrorMessage(); assertThat(msg, containsString("counter")); assertThat(msg, containsString("counter1")); assertThat(msg, containsString("counter2")); assertThat(msg, containsString("counter3")); }
@Test public void complexFieldPropertyErrorMessage() { PropertyMatches matches = PropertyMatches.forField("counter", SampleFieldProperties.class); String msg = matches.buildErrorMessage(); assertThat(msg, containsString("counter")); assertThat(msg, containsString("counter1")); assertThat(msg, containsString("counter2")); assertThat(msg, containsString("counter3")); }
/** * Create PropertyMatches for the given bean property. * @param propertyName the name of the property to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forProperty(String propertyName, Class<?> beanClass) { return forProperty(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
/** * Create PropertyMatches for the given field property. * @param propertyName the name of the field to find possible matches for * @param beanClass the bean class to search for matches */ public static PropertyMatches forField(String propertyName, Class<?> beanClass) { return forField(propertyName, beanClass, DEFAULT_MAX_DISTANCE); }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException(getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }
@Override protected NotWritablePropertyException createNotWritablePropertyException(String propertyName) { PropertyMatches matches = PropertyMatches.forField(propertyName, getRootClass()); throw new NotWritablePropertyException( getRootClass(), getNestedPath() + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); }