-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Make constraint annotations @Repeatable #8063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make constraint annotations @Repeatable #8063
Conversation
|
Please do NOT merge, there is something missing. |
|
Updated. I forgot to make sure that Ready for review. |
| try { | ||
| final Method method = annotation.getClass().getMethod("value"); | ||
| if (!method.isAccessible()) { | ||
| method.setAccessible(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need setAccessible here? It may have an impact on Java 9 support. I suggest we fail with a clear message here telling users they can't create @ Repeatable annotations with the containing annotation type not having a public value method.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marcospereira Turns out setAccessible just isn't needed here. A container annotation must always have, by specification, a publicly accessible value() method.
As soon as you put @Repeatable(ContainerAnnotation.class) on an annotation and you would remove or rename the value() method of the ContainerAnnotation the compiler complains with The container annotation type @Constraints.Max.List must declare a member value().
Plus if you put private or protected in front of the value() method the compiler complains with Illegal modifier for the annotation attribute Constraints.Max.List.value; only public & abstract are permitted.
So for us means either the value() method is defined and if so it for sure is public and therefore accessible by us - or there is no value() method which means it just isn't a container annotation.
I updated the pr.
|
@marcospereira If you find time, can you review again? Thanks! |
marcospereira
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code LGTM. We can also have documentation updates and maybe a Hightlights27 added for this.
|
@marcospereira Done! |
gmethvin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| */ | ||
| public static <A extends Annotation> Annotation[] unwrapContainerAnnotations(final A[] annotations) { | ||
| final List<Annotation> unwrappedAnnotations = new LinkedList<>(); | ||
| for(final Annotation maybeContainerAnnotation : annotations) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spacing after if and for is inconsistent here.
I think we should consider adding a Java formatter for our source code soon. Added an issue for that: #8109.
|
Great addition, thank you @mkurz. |
* Allow @repeatable Java action annotations * Smaller and more focused tests * Backport AnnotationUtils class introduced in #8063
In
masterbranch we upgraded to Hibernate Validator 6 which made all it's constraint annotations@Repeatable. This PR does the same for the constraint annotations play-java offers.This allows a user to e.g. apply multiple
@ValidateWith, etc. on the same element or use groups to choose when a certain constraint annotation should be used, even when an other group needs the same constraint annotation on the same element. Very nice actually, makes the form validation much more flexible...