-
Notifications
You must be signed in to change notification settings - Fork 302
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
Repetitive annotation conjunction #527
Comments
Yes, I thought about that in the past, too. I have kept it mainly for backwards compatibility, but I'm not sure if we should not just break it. It feels like "are annotated or meta-annotated" is by far the most common case. |
@codecholeric Oooh, I like your suggestion of expressing this as Would be very interesting if people could chime in which rely on Not sure how and when to publish this breaking change though. Is there a schedule for 1.0 already? For the time being, I probably have to define my own predicate to use with the |
I think we can just break it with the respective warning. From what I saw your use case is the typical one, and that one would not be affected by the change (only be written a little more complicated than necessary after the change 😉 ) There is no schedule for 1.0, only a scope 😉 Basically full generic support and method references. Unfortunately this is a little bit more complicated than I would wish, so it's taking me quite a while (since I'm mainly working on it in the evenings / on the weekends) |
Alright, thanks a lot 🙂 I appreciate your answer here and your work done on the library so far! Let me know when I can remove the then-redundant rule from my tests :) |
So far the most common use case for checking rules for meta-annotations seems to be annotation composition, where users want to test that certain classes/members are either directly annotated with some annotation, or with some annotation that is meta-annotated with the respective annotation. Thus it makes sense to cover this use case directly with the `metaAnnotated(..)` syntax. For example ``` @interface A{} @A @interface B{} class Foo { @A Object directlyAnnotated; @b Object indirectlyAnnotated; } ``` Previously only `indirectlyAnnotated` would have counted as meta-annotated with `@A`. Now we count both those methods as meta-annotated with `@A`. The old behavior of really being annotated only through the hierarchy, but not directly can still be expressed as `metaAnnotatedWith(A.class).and(not(annotatedWith(A.class)))`, which seems to be good enough still to cover the old behavior. Besides that the only use case I could imagine is to enforce the use of a custom composite annotation, which could also be achieved by simply forbidding the original annotation altogether. Resolves: #527 Signed-off-by: Peter Gafert <[email protected]>
So far the most common use case for checking rules for meta-annotations seems to be annotation composition, where users want to test that certain classes/members are either directly annotated with some annotation, or with some annotation that is meta-annotated with the respective annotation. Thus it makes sense to cover this use case directly with the `metaAnnotated(..)` syntax. For example ``` @interface A{} @A @interface B{} class Foo { @A Object directlyAnnotated; @b Object indirectlyAnnotated; } ``` Previously only `indirectlyAnnotated` would have counted as meta-annotated with `@A`. Now we count both those methods as meta-annotated with `@A`. The old behavior of really being annotated only through the hierarchy, but not directly can still be expressed as `metaAnnotatedWith(A.class).and(not(annotatedWith(A.class)))`, which seems to be good enough still to cover the old behavior. Besides that the only use case I could imagine is to enforce the use of a custom composite annotation, which could also be achieved by simply forbidding the original annotation altogether. Resolves: #527 Signed-off-by: Peter Gafert <[email protected]>
ArchUnit is a wonderful library, thanks! I found one little nuisance though with regard to annotation-based rules. Consider the following Spring controller:
Let's say I want to have a rule that verifies that all my controller methods return
String
s (not a real requirement, just an example). Simple:This works and does what I expect it to do. But it feels repetitive having to specify the annotation twice: once as a direct annotation and a second time as a meta annotation. I find myself very often in this situation, especially when writing rules for a Spring application, since Spring mostly does not distinguish between annotations and meta annotations.
Changing this to:
no longer fails for the
getId
method if its signature is in violation of the rule. Am I missing something from the docs? Ideally, I'd like to express the rule.that().areAnnotatedOrMetaAnnotatedWith(MyAnnotation.class)
without having to repeat myself.Are there common cases where we need to filter annotation targets by meta annotation only? From my point of view, when filtering for meta annotations, you want to include direct annotations most of the time.
Unfortunately, I cannot come up with a new name which would combine the two conjunctions. Thus my proposal would be a breaking change:
areMetaAnnotatedWith()
should (by default) include meta annotated elements and directly annotated elements (this potentially changes behavior of existing rules)areOnlyMetaAnnotatedWith()
which excludes directly annotated elements and only matches elements whose annotations itself are meta-annotated with the specified class.I'd be happy to work on a PR, but before starting, I wanted to double-check if no such possibility already exists. If such a method already exists, please point me to the docs and close this issue :)
The text was updated successfully, but these errors were encountered: