Skip to content

Commit cc606a2

Browse files
committed
HV-1363 Improve the documentation
1 parent c8700bc commit cc606a2

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

documentation/src/main/asciidoc/ch12.asciidoc

+34-27
Original file line numberDiff line numberDiff line change
@@ -677,59 +677,66 @@ validator factory still is referenced by application code.
677677
====
678678

679679
[[section-getter-property-selection-strategy]]
680-
=== Getter property selection strategy configuration
680+
=== Customizing the getter property selection strategy
681681

682-
When a regular validation operation is performed on an object it's properties get validated. Per
683-
JavaBean specification, a property can either be a field or a getter. Hibernate Validator uses
684-
next set of rules to determine if a method is a getter or not:
682+
When a bean is validated by Hibernate Validator, its properties get validated. A property can either
683+
be a field or a getter.
684+
By default, Hibernate Validator respects the JavaBeans specification and considers a method as a getter as soon
685+
as one of the conditions below is true:
685686

686-
- a method name starts with `get`, it has a nonvoid return type and has no parameters
687-
- a method name starts with `is`, has a return type of `boolean` and has no parameters
688-
- a method name starts with `has`, has a return type of `boolean` and has no parameters (this
689-
is a Hibernate Validator specific rule and is not mandated by the JavaBeans specification)
687+
- the method name starts with `get`, it has a non-void return type and has no parameters;
688+
- the method name starts with `is`, has a return type of `boolean` and has no parameters;
689+
- the method name starts with `has`, has a return type of `boolean` and has no parameters (this rule
690+
is specific to Hibernate Validator and is not mandated by the JavaBeans specification)
690691

691-
If any of the statements above are true - method is considered a getter.
692+
While these rules are usually appropriate when following the classic JavaBeans convention, it might happen,
693+
especially with code generators, that the JavaBeans naming convention is not followed and that the getters'
694+
names are following a different convention.
692695

693-
When working with objects that do not follow JavaBean specification rules on getters. These rules
694-
should either be redefined or extended, in order to be able to fully validate the object. An example
695-
of such objects can be the ones constructed from classes that follow fluent design pattern (i.e. <<example-using-fluent-api-pattern>> ).
696+
In this case, the strategy for detecting getters should be redefined in order to fully validate the object.
697+
698+
A classic example of this requirement is when the classes follow a fluent naming convention,
699+
as illustrated in <<example-using-fluent-api-pattern>>.
696700

697701
[[example-using-fluent-api-pattern]]
698-
.A data class that uses nonstandard getters
702+
.A class that uses non-standard getters
699703
====
700704
[source, JAVA, indent=0]
701705
----
702706
include::{sourcedir}/org/hibernate/validator/referenceguide/chapter12/getterselectionstrategy/User.java[tags=include]
703707
----
704708
====
705709

706-
If such object gets validated no validation will be performed on the methods:
710+
If such object gets validated, no validation will be performed on the getters as they are not detected
711+
by the standard strategy.
707712

708-
.Validating a class with nonstandard getters and default getter property selection strategy
713+
.Validating a class with non-standard getters using the default getter property selection strategy
709714
====
710715
[source, JAVA, indent=0]
711716
----
712717
include::{sourcedir}/org/hibernate/validator/referenceguide/chapter12/getterselectionstrategy/GetterPropertySelectionStrategyTest.java[tags=no-strategy]
713718
----
714719
====
715720

716-
To make Hibernate Validator treat such methods as getter properties a custom `GetterPropertySelectionStrategy`
717-
should be configured. In particular case the strategy can be implemeted like:
721+
To make Hibernate Validator treat such methods as properties, a custom `GetterPropertySelectionStrategy`
722+
should be configured.
723+
In this particular case, a possible implementation of the strategy would be:
718724

719-
.Custom GetterPropertySelectionStrategy implementation
725+
.Custom `GetterPropertySelectionStrategy` implementation
720726
====
721727
[source, JAVA, indent=0]
722728
----
723729
include::{sourcedir}/org/hibernate/validator/referenceguide/chapter12/getterselectionstrategy/NoPrefixGetterPropertySelectionStrategy.java[tags=include]
724730
----
725731
====
726732

727-
There are multiple ways to configuring Hibernate Validator to use such strategy. It can either be done
728-
programmatically (<<custom-getter-strategy-programmatically>>) or via the
729-
`hibernate.validator.getter_property_selection_strategy` property using XML configuration (<<custom-getter-strategy-xml>>).
733+
There are multiple ways to configure Hibernate Validator to use this strategy. It can either be done
734+
programmatically (see <<custom-getter-strategy-programmatically>>) or by using the
735+
`hibernate.validator.getter_property_selection_strategy` property in the XML configuration
736+
(see <<custom-getter-strategy-xml>>).
730737

731738
[[custom-getter-strategy-programmatically]]
732-
.Configuring the custom `GetterPropertySelectionStrategy` programmatically
739+
.Configuring a custom `GetterPropertySelectionStrategy` programmatically
733740
====
734741
[source, JAVA, indent=0]
735742
----
@@ -738,7 +745,7 @@ include::{sourcedir}/org/hibernate/validator/referenceguide/chapter12/gettersele
738745
====
739746

740747
[[custom-getter-strategy-xml]]
741-
.Configuring the custom `GetterPropertySelectionStrategy` using XML property
748+
.Configuring a custom `GetterPropertySelectionStrategy` using an XML property
742749
====
743750
[source, XML, indent=0]
744751
----
@@ -748,8 +755,8 @@ include::{resourcesdir}/org/hibernate/validator/referenceguide/chapter12/getter-
748755

749756
[WARNING]
750757
====
751-
It is important to mention that in cases when any programmatic constraints are added using
752-
`HibernateValidatorConfiguration#addMapping(ConstraintMapping)` it should be done after the
753-
required getter property selection strategy is configured. Otherwise, the default one will be
754-
used.
758+
It is important to mention that in cases where programmatic constraints are added using
759+
`HibernateValidatorConfiguration#addMapping(ConstraintMapping)`, adding mappings should
760+
always be done after the required getter property selection strategy is configured.
761+
Otherwise, the default strategy will be used for the mappings added before defining the strategy.
755762
====

documentation/src/test/java/org/hibernate/validator/referenceguide/chapter12/getterselectionstrategy/GetterPropertySelectionStrategyTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void defaultStrategyUsed() {
2626

2727
Set<ConstraintViolation<User>> constraintViolations = validator.validate( user );
2828

29-
// as User has nonstandard getters no violations are triggered
29+
// as User has non-standard getters no violations are triggered
3030
assertEquals( 0, constraintViolations.size() );
3131
//end::no-strategy[]
3232
}
@@ -41,11 +41,11 @@ public void customNoPrefixStrategyUsed() {
4141
.buildValidatorFactory()
4242
.getValidator();
4343

44-
User user = new User( "first", "last", "not an email" );
44+
User user = new User( "", "", "not an email" );
4545

4646
Set<ConstraintViolation<User>> constraintViolations = validator.validate( user );
4747

48-
assertEquals( 1, constraintViolations.size() );
48+
assertEquals( 3, constraintViolations.size() );
4949
//end::custom-strategy[]
5050
}
5151
}

documentation/src/test/java/org/hibernate/validator/referenceguide/chapter12/getterselectionstrategy/NoPrefixGetterPropertySelectionStrategy.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class NoPrefixGetterPropertySelectionStrategy implements GetterPropertySe
1313

1414
@Override
1515
public boolean isGetter(ConstrainableExecutable executable) {
16-
// We check that the method has non void return type and no parameters.
17-
// And we do not care about method name at all.
16+
// We check that the method has a non-void return type and no parameters.
17+
// And we do not care about the method name.
1818
return executable.getReturnType() != void.class
1919
&& executable.getParameterTypes().length == 0;
2020
}
@@ -26,7 +26,7 @@ public String getPropertyName(ConstrainableExecutable method) {
2626

2727
@Override
2828
public Set<String> getGetterMethodNameCandidates(String propertyName) {
29-
// As method name == property name there always is just one possible name for a method
29+
// As method name == property name, there always is just one possible name for a method
3030
return Collections.singleton( propertyName );
3131
}
3232
}

documentation/src/test/java/org/hibernate/validator/referenceguide/chapter12/getterselectionstrategy/User.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
//tag::include[]
99
public class User {
1010

11-
private final String firstName;
12-
private final String lastName;
13-
private final String email;
11+
private String firstName;
12+
private String lastName;
13+
private String email;
14+
15+
// [...]
1416

1517
//end::include[]
1618
public User(String firstName, String lastName, String email) {

0 commit comments

Comments
 (0)