Skip to content

Commit 4628c49

Browse files
committed
HV-621 Treating Default group (which is probably most often used) special to avoid expensive and potentially locking reflection calls
1 parent 9d49d5f commit 4628c49

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

engine/src/main/java/org/hibernate/validator/internal/engine/groups/ValidationOrderGenerator.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.concurrent.ConcurrentHashMap;
2323
import java.util.concurrent.ConcurrentMap;
2424
import javax.validation.GroupSequence;
25+
import javax.validation.groups.Default;
2526

2627
import org.hibernate.validator.internal.util.logging.Log;
2728
import org.hibernate.validator.internal.util.logging.LoggerFactory;
@@ -38,6 +39,13 @@ public class ValidationOrderGenerator {
3839

3940
private final ConcurrentMap<Class<?>, Sequence> resolvedSequences = new ConcurrentHashMap<Class<?>, Sequence>();
4041

42+
private final DefaultValidationOrder validationOrderForDefaultGroup;
43+
44+
public ValidationOrderGenerator() {
45+
validationOrderForDefaultGroup = new DefaultValidationOrder();
46+
validationOrderForDefaultGroup.insertGroup( new Group( Default.class ) );
47+
}
48+
4149
/**
4250
* Generates a order of groups and sequences for the specified validation groups.
4351
*
@@ -50,25 +58,35 @@ public ValidationOrder getValidationOrder(Collection<Class<?>> groups) {
5058
throw log.getAtLeastOneGroupHasToBeSpecifiedException();
5159
}
5260

61+
// HV-621 - if we deal with the Default group we return the default ValidationOrder. No need to
62+
// process Default as other groups which saves several reflection calls (HF)
63+
if ( groups.size() == 1 && groups.contains( Default.class ) ) {
64+
return validationOrderForDefaultGroup;
65+
}
66+
5367
for ( Class<?> clazz : groups ) {
5468
if ( !clazz.isInterface() ) {
5569
throw log.getGroupHasToBeAnInterfaceException( clazz.getName() );
5670
}
5771
}
5872

59-
DefaultValidationOrder order = new DefaultValidationOrder();
73+
DefaultValidationOrder validationOrder = new DefaultValidationOrder();
6074
for ( Class<?> clazz : groups ) {
61-
if ( isGroupSequence( clazz ) ) {
62-
insertSequence( clazz, order );
75+
if ( Default.class.equals( clazz ) ) { // HV-621
76+
Group group = new Group( clazz );
77+
validationOrder.insertGroup( group );
78+
}
79+
else if ( isGroupSequence( clazz ) ) {
80+
insertSequence( clazz, validationOrder );
6381
}
6482
else {
6583
Group group = new Group( clazz );
66-
order.insertGroup( group );
67-
insertInheritedGroups( clazz, order );
84+
validationOrder.insertGroup( group );
85+
insertInheritedGroups( clazz, validationOrder );
6886
}
6987
}
7088

71-
return order;
89+
return validationOrder;
7290
}
7391

7492
private boolean isGroupSequence(Class<?> clazz) {

0 commit comments

Comments
 (0)