Skip to content

Commit

Permalink
HV-822 added check of default message on annotation level; updated te…
Browse files Browse the repository at this point in the history
…st case for such situation
  • Loading branch information
marko-bekhta authored and gunnarmorling committed Oct 24, 2016
1 parent 7f3bb9b commit 38e1e9d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
Expand Up @@ -58,6 +58,7 @@ public ConstraintAnnotationVisitor(

constraintCheckFactory = new ConstraintCheckFactory(
processingEnvironment.getTypeUtils(),
processingEnvironment.getElementUtils(),
constraintHelper,
annotationApiHelper,
configuration.methodConstraintsSupported()
Expand Down
Expand Up @@ -10,6 +10,7 @@

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

import org.hibernate.validator.ap.checks.annotationparameters.AnnotationMessageCheck;
Expand Down Expand Up @@ -61,7 +62,7 @@ public class ConstraintCheckFactory {

private static final SingleValuedChecks NULL_CHECKS = new SingleValuedChecks();

public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper, AnnotationApiHelper annotationApiHelper, boolean methodConstraintsSupported) {
public ConstraintCheckFactory(Types typeUtils, Elements elementUtils, ConstraintHelper constraintHelper, AnnotationApiHelper annotationApiHelper, boolean methodConstraintsSupported) {
this.constraintHelper = constraintHelper;

parameterChecks = CollectionHelper.newHashMap();
Expand Down Expand Up @@ -106,7 +107,7 @@ public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper
new AnnotationParametersDigitsCheck( annotationApiHelper ),
new AnnotationParametersDecimalMinMaxCheck( annotationApiHelper ),
new AnnotationParametersGroupsCheck( annotationApiHelper ),
new AnnotationMessageCheck( annotationApiHelper )
new AnnotationMessageCheck( annotationApiHelper, elementUtils )
)
);
fieldChecks.put(
Expand All @@ -121,7 +122,7 @@ public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper
new AnnotationParametersDigitsCheck( annotationApiHelper ),
new AnnotationParametersDecimalMinMaxCheck( annotationApiHelper ),
new AnnotationParametersGroupsCheck( annotationApiHelper ),
new AnnotationMessageCheck( annotationApiHelper )
new AnnotationMessageCheck( annotationApiHelper, elementUtils )
)
);
fieldChecks.put(
Expand All @@ -144,7 +145,7 @@ public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper
new AnnotationParametersDigitsCheck( annotationApiHelper ),
new AnnotationParametersDecimalMinMaxCheck( annotationApiHelper ),
new AnnotationParametersGroupsCheck( annotationApiHelper ),
new AnnotationMessageCheck( annotationApiHelper )
new AnnotationMessageCheck( annotationApiHelper, elementUtils )
)
);
methodChecks.put(
Expand All @@ -161,7 +162,7 @@ public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper
new AnnotationParametersDigitsCheck( annotationApiHelper ),
new AnnotationParametersDecimalMinMaxCheck( annotationApiHelper ),
new AnnotationParametersGroupsCheck( annotationApiHelper ),
new AnnotationMessageCheck( annotationApiHelper )
new AnnotationMessageCheck( annotationApiHelper, elementUtils )
)
);
methodChecks.put(
Expand Down Expand Up @@ -205,7 +206,7 @@ public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper
new ConstraintValidatorCheck( constraintHelper, annotationApiHelper ),
new AnnotationTypeMemberCheck( annotationApiHelper, typeUtils ),
new CrossParameterConstraintCheck( annotationApiHelper, constraintHelper, typeUtils ),
new AnnotationMessageCheck( annotationApiHelper )
new AnnotationMessageCheck( annotationApiHelper, elementUtils )
)
);
annotationTypeChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );
Expand Down
Expand Up @@ -10,7 +10,12 @@
import java.util.Set;
import java.util.regex.Pattern;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

import org.hibernate.validator.ap.checks.ConstraintCheckIssue;
import org.hibernate.validator.ap.util.AnnotationApiHelper;
Expand All @@ -26,8 +31,11 @@ public class AnnotationMessageCheck extends AnnotationParametersAbstractCheck {
// for dots and no {} around
private static final Pattern MESSAGE_PATTERN = Pattern.compile( "(\\w)+(\\.(\\w)+)*" );

public AnnotationMessageCheck(AnnotationApiHelper annotationApiHelper) {
private final Elements elementUtils;

public AnnotationMessageCheck(AnnotationApiHelper annotationApiHelper, Elements elementUtils) {
super( annotationApiHelper );
this.elementUtils = elementUtils;
}

@Override
Expand All @@ -37,15 +45,33 @@ protected boolean canCheckThisAnnotation(AnnotationMirror annotation) {

@Override
protected Set<ConstraintCheckIssue> doCheck(Element element, AnnotationMirror annotation) {
String message = (String) annotationApiHelper.getAnnotationValueOrDefault( annotation, "message" ).getValue();

if ( MESSAGE_PATTERN.matcher( message ).matches() ) {
//check if default message on annotation is correct or not:
if ( ElementKind.ANNOTATION_TYPE.equals( element.getKind() ) ) {
for ( Element innerElement : elementUtils.getAllMembers( (TypeElement) element ) ) {
if ( ElementKind.METHOD.equals( innerElement.getKind() ) && "message".equals( innerElement.getSimpleName().toString() ) ) {
if ( MESSAGE_PATTERN.matcher( ( (ExecutableElement) innerElement ).getDefaultValue().getValue().toString() ).matches() ) {
return CollectionHelper.asSet(
ConstraintCheckIssue.warning(
innerElement, annotation, "INVALID_MESSAGE_VALUE_ANNOTATION_PARAMETERS"
)
);
}
}
}
}

// check if the redefined by user message is correct or not:
AnnotationValue value = annotationApiHelper.getAnnotationValue( annotation, "message" );

if ( value != null && MESSAGE_PATTERN.matcher( (String) value.getValue() ).matches() ) {
return CollectionHelper.asSet(
ConstraintCheckIssue.warning(
element, annotation, "INVALID_MESSAGE_VALUE_ANNOTATION_PARAMETERS"
)
);
}

return Collections.emptySet();
}
}
Expand Up @@ -283,7 +283,8 @@ public void testValidMessageParameter() {
assertTrue( compilationResult );
assertThatDiagnosticsMatch(
diagnostics,
new DiagnosticExpectation( Kind.WARNING, 78 )
new DiagnosticExpectation( Kind.WARNING, 56 ),
new DiagnosticExpectation( Kind.WARNING, 87 )

);
}
Expand Down
Expand Up @@ -84,6 +84,9 @@ public static class Case1 {
@CancellationCodeValid
private String string2;

@CancellationCodeValid(message = "some.bad.overridden.message.example")
private String string3;

}

}

0 comments on commit 38e1e9d

Please sign in to comment.