Skip to content
This repository has been archived by the owner on Feb 7, 2021. It is now read-only.

Exception occurs when constraint set on overriden method #3

Closed
msche opened this issue Apr 9, 2015 · 3 comments
Closed

Exception occurs when constraint set on overriden method #3

msche opened this issue Apr 9, 2015 · 3 comments

Comments

@msche
Copy link
Member

msche commented Apr 9, 2015

javax.validation.ConstraintDeclarationException: Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints.

So this will cause the exception above:

public class A {
    public void method(Object arg) {...}
}

public class B extends A {
    @Override
    public void method(@NotNull Object arg) {...}
}
@msche
Copy link
Member Author

msche commented May 4, 2015

Exception message is as follows:

HV000151: A method overriding another method must not alter the parameter constraint configuration, but method public void org.uniknow.agiledev.tutorial.dbc.OverrideClass.method(java.lang.Object) changes the configuration of public void org.uniknow.agiledev.tutorial.dbc.BaseClass.method(java.lang.Object).

@msche
Copy link
Member Author

msche commented May 4, 2015

The exception is raised if you add parameter constraints to a method which overrides or implements a super-type method. This behavior is mandated by the Bean Validation spec (see http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-methodlevelconstraints-inheritance) in order to obey to the Liskov substitution principle:

Quote:
In sub types (be it sub classes/interfaces or interface implementations), no parameter constraints may be declared on overridden or implemented methods, nor may parameters be marked for cascaded validation. This would pose a strengthening of preconditions to be fulfilled by the caller.

@msche
Copy link
Member Author

msche commented May 4, 2015

Since the exception complies to the specs we won't fix this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant