Im trying to achieve a simple data validation on the output of a class field that contains a lambda expression. The validation is done using a custom annotation and ConstraintValidator as follows:
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MandatoryValidation.class)
public @interface Mandatory {
boolean value() default true;
String message() default "A value must be supplied";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import com.google.common.base.Supplier;
public class MandatoryValidation implements ConstraintValidator<Mandatory, Supplier<String>> {
private boolean mandatory;
@Override
public void initialize(Mandatory mandatory) {
this.mandatory = mandatory.value();
}
@Override
public boolean isValid(Supplier<String> supplier, ConstraintValidatorContext constraintValidatorContext) {
if (mandatory && supplier.get() == null) {
return false;
} else {
return true;
}
}
}
import java.util.function.Supplier;
public class SampleClass {
private SampleGenerator generator;
@Mandatory
public Supplier<String> lambdaField = () -> generator.next();
}
My problem is that the above code generates one compilation error stating that "The annotation @Mandatory is disallowed for this data type."
The error disappears if I remove the "@Constraint(validatedBy = MandatoryValidation.class)" line from my custom annotation, which leads me to think that I did screw up somewhere in the custom validator but i have no clue how to get it fixed.
Anyone can tell me what is wrong with the above code ?
I'm using the following config :
Jdk : jdk1.8.0_65
javax.validation-api : 1.1.0.Final
hibernate-validator-annotation-processor : 4.1.0.Final
As for the full compiler error message :
Error:(5, 5) java: The annotation @Mandatory is disallowed for this data type.
I've also noticed the following warning :
Warning:java: Supported source version 'RELEASE_6' from annotation processor 'org.hibernate.validator.ap.ConstraintValidationProcessor' less than -source '1.8'.
This warning is removed if i switch to the 5.2.2.Final version of hibernate-validator-annotation-processor but the compilation error remains.
Alright this turned out to be one of the dumbest mistakes that i've managed to pull out to this date. The Major screw up : the Supplier used in the Test class is of type java.util.function.Supplier, while the Supplier used in the custom validator is of type com.google.common.base.Supplier, hence the obvious compilation error. The second major screw up is that i didnt include complete imports in my question thinking that they would bloat up the post. Well that turned out to be a stupid decision.
TLDR: Messed up the imports, used wrong types, totally overlooked the explicit compilation error, went seeking help to my idiocy on stackoverflow.