Skip to content

Commit dd51f80

Browse files
committed
fixup! HV-1363 Build abstraction over reflection in ConstraintLocation and related code
1 parent 9b26462 commit dd51f80

File tree

15 files changed

+36
-28
lines changed

15 files changed

+36
-28
lines changed

engine/src/main/java/org/hibernate/validator/internal/cfg/context/ExecutableConstraintMappingContextImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private List<ConstrainedParameter> getParameters(ConstraintHelper constraintHelp
130130
new ConstrainedParameter(
131131
ConfigurationSource.API,
132132
callable,
133-
callable.typeOfParameter( i ),
133+
callable.getTypeOfParameter( i ),
134134
i
135135
)
136136
);

engine/src/main/java/org/hibernate/validator/internal/cfg/context/ParameterConstraintMappingContextImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public ContainerElementConstraintMappingContext containerElementType(int index,
121121

122122
public ConstrainedParameter build(ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper,
123123
ValueExtractorManager valueExtractorManager) {
124-
Type parameterType = executableContext.getCallable().typeOfParameter( parameterIndex );
124+
Type parameterType = executableContext.getCallable().getTypeOfParameter( parameterIndex );
125125

126126
return new ConstrainedParameter(
127127
ConfigurationSource.API,

engine/src/main/java/org/hibernate/validator/internal/cfg/context/TypeConstraintMappingContextImpl.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ protected ConstraintType getConstraintType() {
255255
}
256256

257257
/**
258-
* Returns the member with the given name and type.
258+
* Returns the property with the given name and type.
259259
*
260-
* @param clazz The class from which to retrieve the member. Cannot be {@code null}.
260+
* @param clazz The class from which to retrieve the property. Cannot be {@code null}.
261261
* @param property The property name without "is", "get" or "has". Cannot be {@code null} or empty.
262262
* @param elementType The element type. Either {@code ElementType.FIELD} or {@code ElementType METHOD}.
263263
*
264-
* @return the member which matching the name and type or {@code null} if no such member exists.
264+
* @return the property which matches the name and type or {@code null} if no such property exists.
265265
*/
266266
private Property getProperty(Class<?> clazz, String property, ElementType elementType) {
267267
Contracts.assertNotNull( clazz, MESSAGES.classCannotBeNull() );

engine/src/main/java/org/hibernate/validator/internal/engine/ConstraintViolationImpl.java

-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class ConstraintViolationImpl<T> implements HibernateConstraintViolation<
3939
private final Map<String, Object> messageParameters;
4040
private final Map<String, Object> expressionVariables;
4141
private final Class<T> rootBeanClass;
42-
private final ElementType elementType;
4342
private final Object[] executableParameters;
4443
private final Object executableReturnValue;
4544
private final Object dynamicPayload;
@@ -161,7 +160,6 @@ private ConstraintViolationImpl(String messageTemplate,
161160
this.leafBeanInstance = leafBeanInstance;
162161
this.constraintDescriptor = constraintDescriptor;
163162
this.rootBeanClass = rootBeanClass;
164-
this.elementType = elementType;
165163
this.executableParameters = executableParameters;
166164
this.executableReturnValue = executableReturnValue;
167165
this.dynamicPayload = dynamicPayload;
@@ -296,10 +294,6 @@ public boolean equals(Object o) {
296294
if ( constraintDescriptor != null ? !constraintDescriptor.equals( that.constraintDescriptor ) : that.constraintDescriptor != null ) {
297295
return false;
298296
}
299-
if ( elementType != null ? !elementType.equals( that.elementType ) : that.elementType != null ) {
300-
return false;
301-
}
302-
303297
return true;
304298
}
305299

@@ -331,7 +325,6 @@ private int createHashCode() {
331325
result = 31 * result + System.identityHashCode( value );
332326
result = 31 * result + ( constraintDescriptor != null ? constraintDescriptor.hashCode() : 0 );
333327
result = 31 * result + ( messageTemplate != null ? messageTemplate.hashCode() : 0 );
334-
result = 31 * result + ( elementType != null ? elementType.hashCode() : 0 );
335328
return result;
336329
}
337330
}

engine/src/main/java/org/hibernate/validator/internal/metadata/location/ParameterConstraintLocation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ParameterConstraintLocation implements ConstraintLocation {
2929
public ParameterConstraintLocation(Callable executable, int index) {
3030
this.executable = executable;
3131
this.index = index;
32-
this.typeForValidatorResolution = ReflectionHelper.boxedType( executable.typeOfParameter( index ) );
32+
this.typeForValidatorResolution = ReflectionHelper.boxedType( executable.getTypeOfParameter( index ) );
3333
}
3434

3535
@Override

engine/src/main/java/org/hibernate/validator/internal/metadata/location/ReturnValueConstraintLocation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class ReturnValueConstraintLocation implements ConstraintLocation {
2424

2525
private final Callable callable;
2626

27-
ReturnValueConstraintLocation(Callable executable) {
28-
this.callable = executable;
27+
ReturnValueConstraintLocation(Callable callable) {
28+
this.callable = callable;
2929
}
3030

3131
@Override

engine/src/main/java/org/hibernate/validator/internal/metadata/raw/ConstrainedProperty.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.hibernate.validator.internal.properties.Property;
1414

1515
/**
16-
* Represents a property of a Java type (either field or getter) and all its associated meta-data relevant
16+
* Represents a property of a Java type (either field or getter) and all its associated metadata relevant
1717
* in the context of bean validation, for instance its constraints.
1818
*
1919
* @author Marko Bekhta

engine/src/main/java/org/hibernate/validator/internal/properties/Callable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface Callable extends Constrainable {
3232

3333
String getSignature();
3434

35-
Type typeOfParameter(int parameterIndex);
35+
Type getTypeOfParameter(int parameterIndex);
3636

3737
boolean overrides(ExecutableHelper executableHelper, Callable superTypeMethod);
3838

engine/src/main/java/org/hibernate/validator/internal/properties/Constrainable.java

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public interface Constrainable {
2121

2222
Type getType();
2323

24+
@SuppressWarnings("unchecked")
2425
default <T> T as(Class<T> clazz) {
2526
return ( (T) this );
2627
}

engine/src/main/java/org/hibernate/validator/internal/properties/javabean/JavaBeanExecutable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public String getSignature() {
110110
}
111111

112112
@Override
113-
public Type typeOfParameter(int parameterIndex) {
113+
public Type getTypeOfParameter(int parameterIndex) {
114114
Type[] genericParameterTypes = executable.getGenericParameterTypes();
115115

116116
// getGenericParameterTypes() doesn't return synthetic parameters; in this case fall back to getParameterTypes()

engine/src/main/java/org/hibernate/validator/internal/properties/javabean/JavaBeanGetter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
public class JavaBeanGetter extends JavaBeanExecutable implements Property {
2525

26-
private static final Class[] PARAMETER_TYPES = new Class[0];
26+
private static final Class<?>[] PARAMETER_TYPES = new Class[0];
2727

2828
private final String name;
2929

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

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.List;
2525
import java.util.Map;
2626

27-
import org.hibernate.validator.internal.properties.Callable;
2827
import org.hibernate.validator.internal.util.logging.Log;
2928
import org.hibernate.validator.internal.util.logging.LoggerFactory;
3029

engine/src/main/java/org/hibernate/validator/internal/xml/mapping/ConstrainedParameterStaxBuilder.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package org.hibernate.validator.internal.xml.mapping;
88

99
import java.lang.invoke.MethodHandles;
10-
import java.lang.reflect.Executable;
1110
import java.lang.reflect.Type;
1211
import java.util.Optional;
1312
import java.util.Set;
@@ -24,7 +23,6 @@
2423
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
2524
import org.hibernate.validator.internal.metadata.raw.ConstrainedParameter;
2625
import org.hibernate.validator.internal.properties.Callable;
27-
import org.hibernate.validator.internal.util.ReflectionHelper;
2826
import org.hibernate.validator.internal.util.TypeResolutionHelper;
2927
import org.hibernate.validator.internal.util.logging.Log;
3028
import org.hibernate.validator.internal.util.logging.LoggerFactory;
@@ -72,7 +70,7 @@ public Class<?> getParameterType(Class<?> beanClass) {
7270
ConstrainedParameter build(Callable callable, int index) {
7371

7472
ConstraintLocation constraintLocation = ConstraintLocation.forParameter( callable, index );
75-
Type type = callable.typeOfParameter( index );
73+
Type type = callable.getTypeOfParameter( index );
7674

7775
Set<MetaConstraint<?>> metaConstraints = constraintTypeStaxBuilders.stream()
7876
.map( builder -> builder.build( constraintLocation, java.lang.annotation.ElementType.PARAMETER, null ) )

engine/src/test/java/org/hibernate/validator/test/internal/engine/failfast/FailFastTest.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ public void testFailFastMethodValidationSetOnValidatorFactory() {
162162
fail();
163163
}
164164
catch (ConstraintViolationException e) {
165-
assertThat( e.getConstraintViolations() ).containsOnlyViolations(
166-
violationOf( NotBlank.class )
165+
assertThat( e.getConstraintViolations() ).containsOneOfViolations(
166+
violationOf( NotBlank.class ),
167+
violationOf( Min.class )
167168
);
168169
}
169170
}
@@ -212,8 +213,9 @@ public void testFailFastMethodValidationSetWithProperty() {
212213
fail();
213214
}
214215
catch (ConstraintViolationException e) {
215-
assertThat( e.getConstraintViolations() ).containsOnlyViolations(
216-
violationOf( NotBlank.class )
216+
assertThat( e.getConstraintViolations() ).containsOneOfViolations(
217+
violationOf( NotBlank.class ),
218+
violationOf( Min.class )
217219
);
218220
}
219221
}

test-utils/src/main/java/org/hibernate/validator/testutil/ConstraintViolationAssert.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ public ConstraintViolationSetAssert describedAs(String description, Object... ar
262262
public void containsOnlyViolations(ViolationExpectation... expectedViolations) {
263263
isNotNull();
264264

265+
List<ViolationExpectation> actualViolations = getActualViolationExpectations( expectedViolations );
266+
267+
Assertions.assertThat( actualViolations ).containsExactlyInAnyOrder( expectedViolations );
268+
}
269+
270+
public void containsOneOfViolations(ViolationExpectation... expectedViolations) {
271+
isNotNull();
272+
273+
List<ViolationExpectation> actualViolations = getActualViolationExpectations( expectedViolations );
274+
275+
Assertions.assertThat( actualViolations ).hasSize( 1 );
276+
Assertions.assertThat( expectedViolations ).contains( actualViolations.get( 0 ) );
277+
}
278+
279+
private List<ViolationExpectation> getActualViolationExpectations(ViolationExpectation[] expectedViolations) {
265280
List<ViolationExpectation> actualViolations = new ArrayList<>();
266281

267282
ViolationExpectationPropertiesToTest referencePropertiesToTest;
@@ -282,7 +297,7 @@ public void containsOnlyViolations(ViolationExpectation... expectedViolations) {
282297
actualViolations.add( new ViolationExpectation( violation, referencePropertiesToTest ) );
283298
}
284299

285-
Assertions.assertThat( actualViolations ).containsExactlyInAnyOrder( expectedViolations );
300+
return actualViolations;
286301
}
287302

288303
public void containsOnlyPaths(PathExpectation... paths) {

0 commit comments

Comments
 (0)