Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ public Void visitVariableAsField(VariableElement annotatedField, List<Annotation
return null;
}

/**
* <p>
* Checks whether the given annotations are correctly specified at the given
* method parameter. The following checks are performed:
* </p>
* <ul>
* <li>
* Constraint annotation parameter values are meaningful and valid.
* </li>
* </ul>
*/
@Override
public Void visitVariableAsParameter(VariableElement annotatedField, List<AnnotationMirror> mirrors) {
checkConstraints( annotatedField, mirrors );
return null;
}

/**
* <p>
* Checks whether the given annotations are correctly specified at the given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class ConstraintCheckFactory {
*/
private final Map<AnnotationType, ConstraintChecks> fieldChecks;

/**
* Holds the checks to be executed for method parameter elements.
*/
private final Map<AnnotationType, ConstraintChecks> parameterChecks;

/**
* Holds the checks to be executed for method elements.
*/
Expand All @@ -56,6 +61,34 @@ public class ConstraintCheckFactory {
public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper, AnnotationApiHelper annotationApiHelper, boolean methodConstraintsSupported) {
this.constraintHelper = constraintHelper;

parameterChecks = CollectionHelper.newHashMap();

parameterChecks.put(
AnnotationType.CONSTRAINT_ANNOTATION,
new SingleValuedChecks(
new AnnotationParametersSizeLengthCheck( annotationApiHelper ),
new AnnotationParametersPatternCheck( annotationApiHelper ),
new AnnotationParametersScriptAssertCheck( annotationApiHelper ),
new AnnotationParametersDigitsCheck( annotationApiHelper ),
new AnnotationParametersDecimalMinMaxCheck( annotationApiHelper )
)
);
parameterChecks.put(
AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION,
new MultiValuedChecks(
constraintHelper,
new AnnotationParametersSizeLengthCheck( annotationApiHelper ),
new AnnotationParametersPatternCheck( annotationApiHelper ),
new AnnotationParametersScriptAssertCheck( annotationApiHelper ),
new AnnotationParametersDigitsCheck( annotationApiHelper ),
new AnnotationParametersDecimalMinMaxCheck( annotationApiHelper )
)
);
parameterChecks.put(
AnnotationType.GRAPH_VALIDATION_ANNOTATION, NULL_CHECKS
);
parameterChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );

fieldChecks = CollectionHelper.newHashMap();
fieldChecks.put(
AnnotationType.CONSTRAINT_ANNOTATION,
Expand Down Expand Up @@ -209,6 +242,8 @@ public ConstraintChecks getConstraintChecks(Element annotatedElement, Annotation
AnnotationType annotationType = constraintHelper.getAnnotationType( annotation );

switch ( annotatedElement.getKind() ) {
case PARAMETER:
return parameterChecks.get( annotationType );
case FIELD:
return fieldChecks.get( annotationType );
case METHOD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ public Set<ConstraintCheckError> execute(Element element, AnnotationMirror annot
//the given element
for ( ConstraintCheck oneCheck : checks ) {

if ( element.getKind() == ElementKind.FIELD ) {
if ( element.getKind() == ElementKind.PARAMETER ) {
theValue.addAll( oneCheck.checkField( (VariableElement) element, annotation ) );
}
else if ( element.getKind() == ElementKind.FIELD ) {
theValue.addAll( oneCheck.checkField( (VariableElement) element, annotation ) );
}
else if ( element.getKind() == ElementKind.METHOD ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.hibernate.validator.ap.testmodel.annotationparameters.ValidScriptAssertParameters;
import org.hibernate.validator.ap.testmodel.annotationparameters.ValidSizeParameters;
import org.hibernate.validator.ap.util.DiagnosticExpectation;

import org.testng.annotations.Test;

import javax.tools.Diagnostic.Kind;
Expand Down Expand Up @@ -59,7 +60,14 @@ public void testInvalidSizeParameters() {
new DiagnosticExpectation( Kind.ERROR, 17 ),
new DiagnosticExpectation( Kind.ERROR, 20 ),
new DiagnosticExpectation( Kind.ERROR, 23 ),
new DiagnosticExpectation( Kind.ERROR, 26 )
new DiagnosticExpectation( Kind.ERROR, 26 ),
new DiagnosticExpectation( Kind.ERROR, 30 ),
new DiagnosticExpectation( Kind.ERROR, 31 ),
new DiagnosticExpectation( Kind.ERROR, 32 ),
new DiagnosticExpectation( Kind.ERROR, 38 ),
new DiagnosticExpectation( Kind.ERROR, 39 ),
new DiagnosticExpectation( Kind.ERROR, 40 ),
new DiagnosticExpectation( Kind.ERROR, 45 )
);
}

Expand Down Expand Up @@ -87,7 +95,14 @@ public void testInvalidLengthParameters() {
new DiagnosticExpectation( Kind.ERROR, 16 ),
new DiagnosticExpectation( Kind.ERROR, 19 ),
new DiagnosticExpectation( Kind.ERROR, 22 ),
new DiagnosticExpectation( Kind.ERROR, 25 )
new DiagnosticExpectation( Kind.ERROR, 25 ),
new DiagnosticExpectation( Kind.ERROR, 29 ),
new DiagnosticExpectation( Kind.ERROR, 30 ),
new DiagnosticExpectation( Kind.ERROR, 31 ),
new DiagnosticExpectation( Kind.ERROR, 37 ),
new DiagnosticExpectation( Kind.ERROR, 38 ),
new DiagnosticExpectation( Kind.ERROR, 39 ),
new DiagnosticExpectation( Kind.ERROR, 44 )
);
}

Expand Down Expand Up @@ -150,7 +165,14 @@ public void testInvalidPatternParameters() {
new DiagnosticExpectation( Kind.ERROR, 16 ),
new DiagnosticExpectation( Kind.ERROR, 19 ),
new DiagnosticExpectation( Kind.ERROR, 22 ),
new DiagnosticExpectation( Kind.ERROR, 25 )
new DiagnosticExpectation( Kind.ERROR, 25 ),
new DiagnosticExpectation( Kind.ERROR, 29 ),
new DiagnosticExpectation( Kind.ERROR, 30 ),
new DiagnosticExpectation( Kind.ERROR, 31 ),
new DiagnosticExpectation( Kind.ERROR, 37 ),
new DiagnosticExpectation( Kind.ERROR, 38 ),
new DiagnosticExpectation( Kind.ERROR, 39 ),
new DiagnosticExpectation( Kind.ERROR, 44 )
);
}

Expand Down Expand Up @@ -178,7 +200,14 @@ public void testInvalidDigitsParameters() {
new DiagnosticExpectation( Kind.ERROR, 17 ),
new DiagnosticExpectation( Kind.ERROR, 20 ),
new DiagnosticExpectation( Kind.ERROR, 23 ),
new DiagnosticExpectation( Kind.ERROR, 26 )
new DiagnosticExpectation( Kind.ERROR, 26 ),
new DiagnosticExpectation( Kind.ERROR, 30 ),
new DiagnosticExpectation( Kind.ERROR, 31 ),
new DiagnosticExpectation( Kind.ERROR, 32 ),
new DiagnosticExpectation( Kind.ERROR, 38 ),
new DiagnosticExpectation( Kind.ERROR, 39 ),
new DiagnosticExpectation( Kind.ERROR, 40 ),
new DiagnosticExpectation( Kind.ERROR, 45 )
);
}

Expand Down Expand Up @@ -210,7 +239,14 @@ public void testInvalidDecimalMinMaxParameters() {
new DiagnosticExpectation( Kind.ERROR, 26 ),
new DiagnosticExpectation( Kind.ERROR, 27 ),
new DiagnosticExpectation( Kind.ERROR, 30 ),
new DiagnosticExpectation( Kind.ERROR, 31 )
new DiagnosticExpectation( Kind.ERROR, 31 ),
new DiagnosticExpectation( Kind.ERROR, 35 ),
new DiagnosticExpectation( Kind.ERROR, 36 ),
new DiagnosticExpectation( Kind.ERROR, 37 ),
new DiagnosticExpectation( Kind.ERROR, 43 ),
new DiagnosticExpectation( Kind.ERROR, 44 ),
new DiagnosticExpectation( Kind.ERROR, 45 ),
new DiagnosticExpectation( Kind.ERROR, 50 )
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,49 @@
*/
package org.hibernate.validator.ap.testmodel.annotationparameters;

import java.math.BigDecimal;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import java.math.BigDecimal;

/**
* @author Marko Bekhta
*/
public class InvalidDecimalMinMaxParameters {

@DecimalMax( value = "a" )
@DecimalMin( value = "a" )
@DecimalMax(value = "a")
@DecimalMin(value = "a")
private BigDecimal decimal1;

@DecimalMax( value = "123.12.00" )
@DecimalMin( value = "123.12.00" )
@DecimalMax(value = "123.12.00")
@DecimalMin(value = "123.12.00")
private BigDecimal decimal2;

@DecimalMax( value = "123E-3-3" )
@DecimalMin( value = "123E-3-3" )
@DecimalMax(value = "123E-3-3")
@DecimalMin(value = "123E-3-3")
private BigDecimal decimal3;

@DecimalMax.List({ @DecimalMax(value = "123E-3-3") })
@DecimalMin.List({ @DecimalMin(value = "123E-3-3") })
private BigDecimal decimal4;

public InvalidDecimalMinMaxParameters(
@DecimalMax(value = "a") BigDecimal decimal1,
@DecimalMax(value = "0.0.0E+7") BigDecimal decimal2,
@DecimalMax(value = "1234.5E-4-4") BigDecimal decimal3
) {

}

public void doSomething(
@DecimalMax(value = "a") BigDecimal decimal1,
@DecimalMax(value = "0.0.0E+7") BigDecimal decimal2,
@DecimalMax(value = "1234.5E-4-4") BigDecimal decimal3
) {

}

@DecimalMax(value = "1234.5E-4-4")
public BigDecimal doSomething() {
return BigDecimal.ONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,44 @@
*/
package org.hibernate.validator.ap.testmodel.annotationparameters;

import javax.validation.constraints.Digits;
import java.math.BigDecimal;
import javax.validation.constraints.Digits;

/**
* @author Marko Bekhta
*/
public class InvalidDigitsParameters {

@Digits( integer = -3, fraction = 3 )
@Digits(integer = -3, fraction = 3)
private BigDecimal decimal1;

@Digits( integer = 5, fraction = -3 )
@Digits(integer = 5, fraction = -3)
private BigDecimal decimal2;

@Digits( integer = -5, fraction = -3 )
@Digits(integer = -5, fraction = -3)
private BigDecimal decimal3;

@Digits.List({ @Digits(integer = -5, fraction = -3), @Digits(integer = 5, fraction = -3) })
private BigDecimal decimal4;

public InvalidDigitsParameters(
@Digits(integer = -1, fraction = 3) BigDecimal decimal1,
@Digits(integer = -5, fraction = -3) BigDecimal decimal2,
@Digits(integer = 3, fraction = -3) BigDecimal decimal3
) {

}

public void doSomething(
@Digits(integer = -1, fraction = 3) BigDecimal decimal1,
@Digits(integer = -5, fraction = -3) BigDecimal decimal2,
@Digits(integer = 3, fraction = -3) BigDecimal decimal3
) {

}

@Digits(integer = -1, fraction = 3)
public BigDecimal doSomething() {
return BigDecimal.ONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,37 @@
*/
public class InvalidLengthParameters {

@Length( min = -1 )
@Length(min = -1)
private String string1;

@Length( max = -10 )
@Length(max = -10)
private String string2;

@Length( min = 15, max = 10 )
@Length(min = 15, max = 10)
private String string3;

@Length.List({ @Length(min = 15, max = 10), @Length(max = -10) })
private String string4;

public InvalidLengthParameters(
@Length(min = -1) String strings1,
@Length(max = -10) String strings2,
@Length(min = 15, max = 10) String strings3
) {

}

public void doSomething(
@Length(min = -1) String strings1,
@Length(max = -10) String strings2,
@Length(min = 15, max = 10) String strings3
) {

}

@Length(min = -10)
public String doSomething() {
return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,36 @@
*/
public class InvalidPatternParameters {

@Pattern( regexp = "\\" )
@Pattern(regexp = "\\")
private String strings1;

@Pattern( regexp = "[a" )
@Pattern(regexp = "[a")
private String strings2;

@Pattern( regexp = "+" )
@Pattern(regexp = "+")
private String strings3;

@Pattern.List({ @Pattern(regexp = "+"), @Pattern(regexp = "[a") })
private String strings4;

public InvalidPatternParameters(
@Pattern(regexp = "\\") String strings1,
@Pattern(regexp = "[test") String strings2,
@Pattern(regexp = "+") String strings3
) {

}

public void doSomething(
@Pattern(regexp = "\\") String strings1,
@Pattern(regexp = "[test") String strings2,
@Pattern(regexp = "+") String strings3
) {

}

@Pattern(regexp = "\\")
public String doSomething() {
return "";
}
}
Loading