1

I am defining this functional interface as :

@java.lang.FunctionalInterface
interface Foo
{
    @javax.validation.constraints.NotNull Bar getBar()
}

The problem here is that subtypes (lambdas, ...) overriding getBar() seem not constrainted by the not nullable constraint.

What are the practical solutions to this ?

HPH
  • 388
  • 2
  • 11

1 Answers1

0

Please be precise as your observation is in direct conflict with accepted answer here. seem not constrainted is not very precise, either it is or it is not.

javax.validation.constraints.@NotNull is a JSR validation so it should be covered by above answer.If not , probably you might try commenting in that answer.

I would suggest to post your driver program / set up since I doubt that jsr bean validator might not be getting called in your instantiations.

You should understand that this particular behavior listed in above answer is very specific to bean validation annotation processors & has nothing to do with generic annotation inheritance in Java.

Just off topic but trying to include these answers to understand Annotation Inheritance in Java,

Answer -1

Answer -2

Answer -3

Answer -4 Spring MVC Validations

Just to illustrate , no error messages till line - Bar bar = foo.getBar(); but when I put it through validator , I get the message - bar- may not be null for both parent & child ,

@java.lang.FunctionalInterface
public interface FooChild extends Foo {

}

and driver program ,

    import java.util.Set;

    import javax.validation.Configuration;
    import javax.validation.ConstraintViolation;
    import javax.validation.Validation;
    import javax.validation.Validator;
    import javax.validation.ValidatorFactory;

    public class DriverProgram {

        public static void main(String[] args) {
        Foo foo = () -> {
            return null;
        };

        Bar bar = foo.getBar();

        Configuration<?> config = Validation.byDefaultProvider().configure();
        ValidatorFactory factory = config.buildValidatorFactory();
        Validator validator = factory.getValidator();
        factory.close();

        Set<ConstraintViolation<Foo>> violations = validator.validate(foo);
        violations.forEach(v -> System.out.println(v.getPropertyPath() + "- " + v.getMessage()));
FooChild fooChild = () -> {
        return null;
    };

    bar = fooChild.getBar();

    violations = validator.validate(fooChild);
    violations.forEach(v -> System.out.println(v.getPropertyPath() + "- " + v.getMessage()));


        }

    }
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • The [linked answer](https://stackoverflow.com/a/9067939/3850730) seems to provide the correct answer, you can go ahead and mark this question as *duplicate*. – HPH Jan 23 '19 at 11:54