Skip to content

Commit fcbc2bc

Browse files
committed
Replace BeanUtils.getProperty() by BeanWrapperImpl from Spring Framework.
@see http://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/validation.html#beans-beans No functional changes.
1 parent b3ec658 commit fcbc2bc

File tree

4 files changed

+78
-84
lines changed

4 files changed

+78
-84
lines changed

pom.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616
<version>${thymeleaf.togglz.version}</version>
1717
</dependency>
1818

19-
<dependency>
20-
<groupId>commons-beanutils</groupId>
21-
<artifactId>commons-beanutils</artifactId>
22-
<version>${commons-beanutils.version}</version>
23-
</dependency>
24-
2519
<!-- Needed for IOUtils -->
2620
<dependency>
2721
<groupId>commons-io</groupId>
@@ -358,9 +352,6 @@
358352
<checkstyle.plugin.version>2.17</checkstyle.plugin.version>
359353
<clean.plugin.version>3.0.0</clean.plugin.version>
360354

361-
<!-- Redefine default value from spring-boot-dependencies (https://git.io/vKAiK) -->
362-
<commons-beanutils.version>1.9.2</commons-beanutils.version>
363-
364355
<!-- Redefine default value from spring-boot-dependencies (https://git.io/vKAiK) -->
365356
<commons-dbcp.version>1.4</commons-dbcp.version>
366357

src/main/java/ru/mystamps/web/validation/jsr303/FieldsMatchValidator.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
*/
1818
package ru.mystamps.web.validation.jsr303;
1919

20-
import java.lang.reflect.InvocationTargetException;
20+
import java.util.Objects;
2121

2222
import javax.validation.ConstraintValidator;
2323
import javax.validation.ConstraintValidatorContext;
2424

25-
import org.apache.commons.beanutils.BeanUtils;
2625
import org.apache.commons.lang3.StringUtils;
2726

27+
import org.springframework.beans.BeanWrapperImpl;
28+
import org.springframework.beans.PropertyAccessor;
29+
2830
public class FieldsMatchValidator implements ConstraintValidator<FieldsMatch, Object> {
2931

3032
private String firstFieldName;
@@ -43,31 +45,30 @@ public boolean isValid(Object value, ConstraintValidatorContext ctx) {
4345
return true;
4446
}
4547

46-
try {
47-
String firstFieldValue = BeanUtils.getProperty(value, firstFieldName);
48-
if (StringUtils.isEmpty(firstFieldValue)) {
49-
return true;
50-
}
51-
52-
String secondFieldValue = BeanUtils.getProperty(value, secondFieldName);
53-
if (StringUtils.isEmpty(secondFieldValue)) {
54-
return true;
55-
}
56-
57-
// TODO: check fields only when both fields are equals
58-
59-
if (!firstFieldValue.equals(secondFieldValue)) {
60-
// bind error message to 2nd field
61-
ConstraintViolationUtils.recreate(
62-
ctx,
63-
secondFieldName,
64-
ctx.getDefaultConstraintMessageTemplate()
65-
);
66-
return false;
67-
}
48+
PropertyAccessor bean = new BeanWrapperImpl(value);
49+
50+
Object firstField = bean.getPropertyValue(firstFieldName);
51+
String firstFieldValue = Objects.toString(firstField, "");
52+
if (StringUtils.isEmpty(firstFieldValue)) {
53+
return true;
54+
}
55+
56+
Object secondField = bean.getPropertyValue(secondFieldName);
57+
String secondFieldValue = Objects.toString(secondField, "");
58+
if (StringUtils.isEmpty(secondFieldValue)) {
59+
return true;
60+
}
61+
62+
// TODO: check fields only when both fields are equals
6863

69-
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
70-
throw new RuntimeException(ex); // NOPMD
64+
if (!firstFieldValue.equals(secondFieldValue)) {
65+
// bind error message to 2nd field
66+
ConstraintViolationUtils.recreate(
67+
ctx,
68+
secondFieldName,
69+
ctx.getDefaultConstraintMessageTemplate()
70+
);
71+
return false;
7172
}
7273

7374
return true;

src/main/java/ru/mystamps/web/validation/jsr303/FieldsMismatchValidator.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
*/
1818
package ru.mystamps.web.validation.jsr303;
1919

20-
import java.lang.reflect.InvocationTargetException;
20+
import java.util.Objects;
2121

2222
import javax.validation.ConstraintValidator;
2323
import javax.validation.ConstraintValidatorContext;
2424

25-
import org.apache.commons.beanutils.BeanUtils;
2625
import org.apache.commons.lang3.StringUtils;
2726

27+
import org.springframework.beans.BeanWrapperImpl;
28+
import org.springframework.beans.PropertyAccessor;
29+
2830
public class FieldsMismatchValidator implements ConstraintValidator<FieldsMismatch, Object> {
2931

3032
private String firstFieldName;
@@ -43,31 +45,30 @@ public boolean isValid(Object value, ConstraintValidatorContext ctx) {
4345
return true;
4446
}
4547

46-
try {
47-
String firstFieldValue = BeanUtils.getProperty(value, firstFieldName);
48-
if (StringUtils.isEmpty(firstFieldValue)) {
49-
return true;
50-
}
51-
52-
String secondFieldValue = BeanUtils.getProperty(value, secondFieldName);
53-
if (StringUtils.isEmpty(secondFieldValue)) {
54-
return true;
55-
}
56-
57-
// TODO: check fields only when both fields are equals
58-
59-
if (firstFieldValue.equals(secondFieldValue)) {
60-
// bind error message to 2nd field
61-
ConstraintViolationUtils.recreate(
62-
ctx,
63-
secondFieldName,
64-
ctx.getDefaultConstraintMessageTemplate()
65-
);
66-
return false;
67-
}
48+
PropertyAccessor bean = new BeanWrapperImpl(value);
49+
50+
Object firstField = bean.getPropertyValue(firstFieldName);
51+
String firstFieldValue = Objects.toString(firstField, "");
52+
if (StringUtils.isEmpty(firstFieldValue)) {
53+
return true;
54+
}
55+
56+
Object secondField = bean.getPropertyValue(secondFieldName);
57+
String secondFieldValue = Objects.toString(secondField, "");
58+
if (StringUtils.isEmpty(secondFieldValue)) {
59+
return true;
60+
}
61+
62+
// TODO: check fields only when both fields are equals
6863

69-
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
70-
throw new RuntimeException(ex); // NOPMD
64+
if (firstFieldValue.equals(secondFieldValue)) {
65+
// bind error message to 2nd field
66+
ConstraintViolationUtils.recreate(
67+
ctx,
68+
secondFieldName,
69+
ctx.getDefaultConstraintMessageTemplate()
70+
);
71+
return false;
7172
}
7273

7374
return true;

src/main/java/ru/mystamps/web/validation/jsr303/NotNullIfFirstFieldValidator.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
*/
1818
package ru.mystamps.web.validation.jsr303;
1919

20-
import java.lang.reflect.InvocationTargetException;
20+
import java.util.Objects;
2121

2222
import javax.validation.ConstraintValidator;
2323
import javax.validation.ConstraintValidatorContext;
2424

25-
import org.apache.commons.beanutils.BeanUtils;
2625
import org.apache.commons.lang3.StringUtils;
2726

27+
import org.springframework.beans.BeanWrapperImpl;
28+
import org.springframework.beans.PropertyAccessor;
29+
2830
public class NotNullIfFirstFieldValidator
2931
implements ConstraintValidator<NotNullIfFirstField, Object> {
3032

@@ -44,29 +46,28 @@ public boolean isValid(Object value, ConstraintValidatorContext ctx) {
4446
return true;
4547
}
4648

47-
try {
48-
String firstFieldValue = BeanUtils.getProperty(value, firstFieldName);
49-
if (StringUtils.isEmpty(firstFieldValue)) {
50-
return true;
51-
}
52-
53-
String secondFieldValue = BeanUtils.getProperty(value, secondFieldName);
54-
if (StringUtils.isNotEmpty(secondFieldValue)) {
55-
return true;
56-
}
57-
58-
// bind error message to 2nd field
59-
ConstraintViolationUtils.recreate(
60-
ctx,
61-
secondFieldName,
62-
ctx.getDefaultConstraintMessageTemplate()
63-
);
64-
65-
return false;
49+
PropertyAccessor bean = new BeanWrapperImpl(value);
6650

67-
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
68-
throw new RuntimeException(ex); // NOPMD: AvoidThrowingRawExceptionTypes
51+
Object firstField = bean.getPropertyValue(firstFieldName);
52+
String firstFieldValue = Objects.toString(firstField, "");
53+
if (StringUtils.isEmpty(firstFieldValue)) {
54+
return true;
6955
}
56+
57+
Object secondField = bean.getPropertyValue(secondFieldName);
58+
String secondFieldValue = Objects.toString(secondField, "");
59+
if (StringUtils.isNotEmpty(secondFieldValue)) {
60+
return true;
61+
}
62+
63+
// bind error message to 2nd field
64+
ConstraintViolationUtils.recreate(
65+
ctx,
66+
secondFieldName,
67+
ctx.getDefaultConstraintMessageTemplate()
68+
);
69+
70+
return false;
7071
}
7172

7273
}

0 commit comments

Comments
 (0)