Skip to content

Commit

Permalink
HV-1264 Some adjustments: naming, toString() method, creating maps la…
Browse files Browse the repository at this point in the history
…zily
  • Loading branch information
gsmet committed Mar 1, 2017
1 parent d8264a4 commit 0a471fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -38,8 +39,8 @@ public class ConstraintValidatorContextImpl implements HibernateConstraintValida

private static final Log log = LoggerFactory.make();

private final Map<String, Object> messageParameters = newHashMap();
private final Map<String, Object> expressionVariables = newHashMap();
private Map<String, Object> messageParameters;
private Map<String, Object> expressionVariables;
private final List<String> methodParameterNames;
private final TimeProvider timeProvider;
private final List<ConstraintViolationCreationContext> constraintViolationCreationContexts = newArrayList( 3 );
Expand Down Expand Up @@ -87,13 +88,23 @@ public <T> T unwrap(Class<T> type) {
@Override
public HibernateConstraintValidatorContext addExpressionVariable(String name, Object value) {
Contracts.assertNotNull( name, "null is not a valid value for an expression variable name" );

if ( expressionVariables == null ) {
expressionVariables = newHashMap();
}

this.expressionVariables.put( name, value );
return this;
}

@Override
public HibernateConstraintValidatorContext addMessageParameter(String name, Object value) {
Contracts.assertNotNull( name, "null is not a valid value for a parameter name" );

if ( messageParameters == null ) {
messageParameters = newHashMap();
}

this.messageParameters.put( name, value );
return this;
}
Expand Down Expand Up @@ -126,8 +137,8 @@ public final List<ConstraintViolationCreationContext> getConstraintViolationCrea
new ConstraintViolationCreationContext(
getDefaultConstraintMessageTemplate(),
basePath,
newHashMap( messageParameters ),
newHashMap( expressionVariables ),
messageParameters != null ? newHashMap( messageParameters ) : Collections.<String, Object>emptyMap(),
expressionVariables != null ? newHashMap( expressionVariables ) : Collections.<String, Object>emptyMap(),
dynamicPayload
)
);
Expand All @@ -153,8 +164,8 @@ public ConstraintValidatorContext addConstraintViolation() {
new ConstraintViolationCreationContext(
messageTemplate,
propertyPath,
newHashMap( messageParameters ),
newHashMap( expressionVariables ),
messageParameters != null ? newHashMap( messageParameters ) : Collections.<String, Object>emptyMap(),
expressionVariables != null ? newHashMap( expressionVariables ) : Collections.<String, Object>emptyMap(),
dynamicPayload
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public String toString() {
sb.append( "message='" ).append( message ).append( '\'' );
sb.append( ", propertyPath=" ).append( propertyPath );
sb.append( ", messageParameters=" ).append( messageParameters );
sb.append( ", expressionVariables=" ).append( expressionVariables );
sb.append( ", dynamicPayload=" ).append( dynamicPayload );
sb.append( '}' );
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,18 @@ private ValueExpression bindContextValues(String messageTemplate, MessageInterpo
);
elContext.setVariable( RootResolver.FORMATTER, valueExpression );

// map the expression variables provided by the annotation values and the parameters added to the context
addExpressionVariablesToElContext( elContext, messageInterpolatorContext.getConstraintDescriptor().getAttributes() );
// map the parameters provided by the annotation values and the parameters + expression variables explicitly
// added to the context
addVariablesToElContext( elContext, messageInterpolatorContext.getConstraintDescriptor().getAttributes() );
if ( messageInterpolatorContext instanceof HibernateMessageInterpolatorContext ) {
addExpressionVariablesToElContext( elContext, ( (HibernateMessageInterpolatorContext) messageInterpolatorContext ).getExpressionVariables() );
addVariablesToElContext( elContext, ( (HibernateMessageInterpolatorContext) messageInterpolatorContext ).getExpressionVariables() );
}

return expressionFactory.createValueExpression( elContext, messageTemplate, String.class );
}

private void addExpressionVariablesToElContext(SimpleELContext elContext, Map<String, Object> expressionVariables) {
for ( Map.Entry<String, Object> entry : expressionVariables.entrySet() ) {
private void addVariablesToElContext(SimpleELContext elContext, Map<String, Object> variables) {
for ( Map.Entry<String, Object> entry : variables.entrySet() ) {
ValueExpression valueExpression = expressionFactory.createValueExpression( entry.getValue(), Object.class );
elContext.getVariableMapper().setVariable( entry.getKey(), valueExpression );
}
Expand Down

0 comments on commit 0a471fb

Please sign in to comment.