Skip to content

Commit 8f08f09

Browse files
committed
HV-1450 Some minor editorialization
1 parent 6b24ba8 commit 8f08f09

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

engine/src/main/java/org/hibernate/validator/internal/metadata/BeanMetaDataManager.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static org.hibernate.validator.internal.util.ConcurrentReferenceHashMap.ReferenceType.SOFT;
1212
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;
1313

14+
import java.util.ArrayList;
1415
import java.util.EnumSet;
1516
import java.util.List;
1617

@@ -28,12 +29,14 @@
2829
import org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider;
2930
import org.hibernate.validator.internal.metadata.provider.MetaDataProvider;
3031
import org.hibernate.validator.internal.metadata.raw.BeanConfiguration;
32+
import org.hibernate.validator.internal.util.CollectionHelper;
3133
import org.hibernate.validator.internal.util.ConcurrentReferenceHashMap;
3234
import org.hibernate.validator.internal.util.Contracts;
3335
import org.hibernate.validator.internal.util.ExecutableHelper;
3436
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
3537
import org.hibernate.validator.internal.util.TypeResolutionHelper;
3638
import org.hibernate.validator.internal.util.classhierarchy.ClassHierarchyHelper;
39+
import org.hibernate.validator.internal.util.stereotypes.Immutable;
3740

3841
/**
3942
* This manager is in charge of providing all constraint related meta data
@@ -74,6 +77,7 @@ public class BeanMetaDataManager {
7477
* Additional metadata providers used for meta data retrieval if
7578
* the XML and/or programmatic configuration is used.
7679
*/
80+
@Immutable
7781
private final List<MetaDataProvider> metaDataProviders;
7882

7983
/**
@@ -140,17 +144,20 @@ public BeanMetaDataManager(ConstraintHelper constraintHelper,
140144

141145
AnnotationProcessingOptions annotationProcessingOptions = getAnnotationProcessingOptionsFromNonDefaultProviders( optionalMetaDataProviders );
142146
AnnotationMetaDataProvider defaultProvider = new AnnotationMetaDataProvider(
143-
constraintHelper,
144-
typeResolutionHelper,
145-
valueExtractorManager,
146-
annotationProcessingOptions
147-
);
148-
this.metaDataProviders = newArrayList();
149-
// default annotation based metadata provider should go first in the list of providers to prevent possible issues
150-
// similar to HV-1450. This way after the first run we will have all possible constrained elements in the list and
151-
// should only merge in additional constraint information from other providers.
152-
this.metaDataProviders.add( defaultProvider );
153-
this.metaDataProviders.addAll( optionalMetaDataProviders );
147+
constraintHelper,
148+
typeResolutionHelper,
149+
valueExtractorManager,
150+
annotationProcessingOptions
151+
);
152+
List<MetaDataProvider> tmpMetaDataProviders = new ArrayList<>( optionalMetaDataProviders.size() + 1 );
153+
// We add the annotation based metadata provider at the first position so that the entire metadata model is assembled
154+
// first.
155+
// The other optional metadata providers will then contribute their additional metadata to the preexisting model.
156+
// This helps to mitigate issues like HV-1450.
157+
tmpMetaDataProviders.add( defaultProvider );
158+
tmpMetaDataProviders.addAll( optionalMetaDataProviders );
159+
160+
this.metaDataProviders = CollectionHelper.toImmutableList( tmpMetaDataProviders );
154161
}
155162

156163
@SuppressWarnings("unchecked")

engine/src/main/java/org/hibernate/validator/internal/metadata/aggregated/ExecutableMetaData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,15 @@ public boolean accepts(ConstrainedElement constrainedElement) {
310310

311311
//are the locations equal (created by different builders) or
312312
//does one of the executables override the other one?
313-
return resolveToSameMethodInHierarchy( executable, candidate );
313+
return isResolvedToSameMethodInHierarchy( executable, candidate );
314314
}
315315

316-
private boolean resolveToSameMethodInHierarchy(Executable first, Executable other) {
316+
private boolean isResolvedToSameMethodInHierarchy(Executable first, Executable other) {
317317
if ( first instanceof Constructor || other instanceof Constructor ) {
318318
return first.equals( other );
319319
}
320320

321-
return executableHelper.resolveToSameMethodInHierarchy( getBeanClass(), (Method) first, (Method) other );
321+
return executableHelper.isResolvedToSameMethodInHierarchy( getBeanClass(), (Method) first, (Method) other );
322322
}
323323

324324
private boolean overrides(Executable first, Executable other) {

engine/src/main/java/org/hibernate/validator/internal/util/ExecutableHelper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public boolean overrides(Method subTypeMethod, Method superTypeMethod) {
9191
return false;
9292
}
9393

94-
if ( !isOneMethodVisibleToAnother( subTypeMethod, superTypeMethod ) ) {
94+
if ( !isMethodVisibleTo( superTypeMethod, subTypeMethod ) ) {
9595
return false;
9696
}
9797

@@ -110,7 +110,7 @@ public boolean overrides(Method subTypeMethod, Method superTypeMethod) {
110110
* override another one in the class hierarchy with {@code mainSubType} at the bottom,
111111
* {@code false} otherwise.
112112
*/
113-
public boolean resolveToSameMethodInHierarchy(Class<?> mainSubType, Method left, Method right) {
113+
public boolean isResolvedToSameMethodInHierarchy(Class<?> mainSubType, Method left, Method right) {
114114
Contracts.assertValueNotNull( mainSubType, "mainSubType" );
115115
Contracts.assertValueNotNull( left, "left" );
116116
Contracts.assertValueNotNull( right, "right" );
@@ -148,7 +148,7 @@ public boolean resolveToSameMethodInHierarchy(Class<?> mainSubType, Method left,
148148
return false;
149149
}
150150

151-
if ( !isOneMethodVisibleToAnother( left, right ) || !isOneMethodVisibleToAnother( right, left ) ) {
151+
if ( !isMethodVisibleTo( right, left ) || !isMethodVisibleTo( left, right ) ) {
152152
return false;
153153
}
154154

@@ -163,9 +163,9 @@ public boolean resolveToSameMethodInHierarchy(Class<?> mainSubType, Method left,
163163
);
164164
}
165165

166-
private static boolean isOneMethodVisibleToAnother(Method left, Method right) {
167-
return Modifier.isPublic( right.getModifiers() ) || Modifier.isProtected( right.getModifiers() )
168-
|| right.getDeclaringClass().getPackage().equals( left.getDeclaringClass().getPackage() );
166+
private static boolean isMethodVisibleTo(Method visibleMethod, Method otherMethod) {
167+
return Modifier.isPublic( visibleMethod.getModifiers() ) || Modifier.isProtected( visibleMethod.getModifiers() )
168+
|| visibleMethod.getDeclaringClass().getPackage().equals( otherMethod.getDeclaringClass().getPackage() );
169169
}
170170

171171
public static String getSimpleName(Executable executable) {

engine/src/test/java/org/hibernate/validator/test/internal/engine/methodvalidation/MethodParameterConstraintsInParallelHierarchyTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public class MethodParameterConstraintsInParallelHierarchyTest {
2828

2929
/**
30-
* NOTE: prior to the changes where this test was added it would fail randomly.
30+
* NOTE: prior to the changes made for HV-1450, this test was failing randomly.
3131
*/
3232
@Test
3333
@TestForIssue(jiraKey = "HV-1450")
@@ -37,13 +37,14 @@ public void testDeepParallelHierarchyIsProcessedCorrectly() throws Exception {
3737
Method method = WebServiceImpl.class.getMethod( "getEntityVersion", Long.class );
3838
Object[] params = new Object[] { null };
3939

40-
Validator validator = Validation.byDefaultProvider().configure()
41-
.buildValidatorFactory().getValidator();
40+
for ( int i = 0; i < 100; i++ ) {
41+
Validator validator = Validation.byDefaultProvider().configure()
42+
.buildValidatorFactory().getValidator();
4243

43-
Set<ConstraintViolation<WebServiceImpl>> violations =
44-
validator.forExecutables().validateParameters( service, method, params );
44+
Set<ConstraintViolation<WebServiceImpl>> violations = validator.forExecutables().validateParameters( service, method, params );
4545

46-
assertThat( violations ).containsOnlyViolations( violationOf( NotNull.class ) );
46+
assertThat( violations ).containsOnlyViolations( violationOf( NotNull.class ) );
47+
}
4748
}
4849

4950
private class WebServiceImpl extends AbstractWebService implements ExtendedWebService {

0 commit comments

Comments
 (0)