Skip to content

Declaring that one annotation aliases or specializes another (@Implies) #35

Open

Description

@kevinb9n

A meta-annotation, used like:

@Implies(Nullable.class)
public @interface MyNullable { ... }

... which declares the annotated annotation to have all of the semantics of the linked annotation (plus optionally more).

This could make for easier migration, it could be used by things like @PolyNull so that the user doesn't have to redundantly write @Nullable @PolyNull, it could be used by a library that parallels ours to remove runtime retention, and perhaps other uses as well.

Activity

  1. kevinb9n commented on Jun 20, 2019

    @kevinb9n
    CollaboratorAuthor

    Notably it wouldn't be a complete solution, since there will be users who want to alias an annotation they don't own and the owner won't do it.

  2. changed the title [-]@Implies[/-] [+]Declaring that one annotation aliases or specializes another[/+] on Jun 21, 2019
  3. kevinb9n commented on Jun 21, 2019

    @kevinb9n
    CollaboratorAuthor

    Other names for @Implies?

    @Extends
    @Specializes
    @Refines
    

    [edit: see #218 as the new home for the topic that follows here. The comment here is asking whether we might just do that instead of having @Implies at all.]

    But an alternative to @Implies might be to establish a pattern instead, that most annotations should include Target(ANNOTATION_TYPE) and any code which for the presence of annotation @Foo should check any other annotations present to see if they have @Foo, and so on. This is a pattern we've actually used a fair number of times, and I wouldn't be surprised if it has plenty of industry precedent too.

    It's less clear/readable than @Implies and also leaves each annotation needing to specify "and here's what it means if you apply me to another annotation type" instead of just letting @Implies take care of that. OTOH, even in the latter case the annotation may still want to say "NOTE: any code checking for the presence of this annotation is expected to recognize @Implies!!!" anyway.

  4. donnerpeter commented on Jun 21, 2019

    @donnerpeter
    Collaborator

    Such annotation synonyms make IDEs life harder, and code analysis slower for everyone, even those not using synonyms. I'd prefer to avoid adding them if possible, unless there's a very high demand and specific scenarios.

    The reason: when we know the notnull annotation is called NotNull and we see some @Foo, we can safely skip it and save some time on resolving the Foo. But when there are synonyms, we should either resolve all annotations always, or precompute the names of all synonyms, which requires a whole-project search (using another index, but still not instant), which should also be repeated each time the caches are dropped, which is often. Building an efficient index of synonyms is complicated by the fact that they can be defined in any JVM language, and not all languages are supported equally well and allow for efficient annotation search. We're already suffering from similar issues in Spring support.

  5. kevinb9n commented on Jun 21, 2019

    @kevinb9n
    CollaboratorAuthor

    Very good to know, thank you.

  6. donnerpeter commented on Jun 24, 2019

    @donnerpeter
    Collaborator

    From meeting notes:

    The added burden to IDE analysis - does it scale with the number of usages of @Implies on encountered annotations, not the number of usages of the marked annotation?

    It depends on the specific syntax. If it's @Implies(NotNull.class), then with the current APIs it scales with the total number of usages of @Implies in the project and libraries. Shouldn't probably be too much. But note that this syntax doesn't allow implied annotations to have parameters. There are other approaches that allow parameters.

    One (AFAIK Spring takes it) is to just annotate an @interface Foo with your annotation, and then @Foo will also have your semantics. This one is the most expensive, it scales with the number of @YourAnnotation usages in the project&libraries, both on annotation types and in other places. Or with the total number of @interface declarations, which is lower, but still unfortunately a lot.

    The other is represented by @TypeQualifierNickname: when your @interface Foo is annotated with this @Nickname, then some subset of the other annotations on Foo is propagated to @Foo. Not all, obviously, because some of those annotation apply only to Foo, e.g. @Retention. Thus one should distinguish these subsets. In JSR-305, this is done with the "interesting" subset having @TypeQualifier. This scales with the number of @Nickname usages in the project&libraries, comparable with @Implies cost, but to me this added ceremony of type qualifiers feels a bit heavy.

  7. sdeleuze commented on Sep 12, 2019

    @sdeleuze
    Collaborator

    Spring has already been mentioned in previous comments, but I would like to add more context since our use case impact a lot of users and could also apply to other projects.

    Given the unclear status of JSR 305, we have decided not to expose it directly, but rather to hide its usage via the meta annotation support behind Spring null-safety annotations that are now part of our public API. We introduced them by anticipating a better replacement, which is currently happening with this codeanalysis annotations effort.

    We have following annotations:

    • @Nullable: equivalent of codeanalysis.experimental.annotations.Nullable
    • @NonNull: equivalent of codeanalysis.experimental.annotations.NotNull
    • @NonNullApi: equivalent of codeanalysis.experimental.annotations.DefaultNotNull currently with a narrower scope due to limitation of JSR 305, but the ideal target is to extend it to generics, varargs and array elements of related APIs, so same scope.
    • @NonNullFields: I don't think we have an equivalent currently in codeanalysis annotations.

    I am currently doing some test with @dzharkov on a local branch with codeanalysis annotations instead of our Spring ones meta-annotated with JSR 305 ones in order to provide feedback to the working group, but for any kind of real word usage, this "meta annotation feature" is mandatory, and the lack of it would prevent us from using codeanalysis annotations.

    We have no strong opinion about how that should be implemented, we are indeed usually using implicit meta annotations, but if a dedicated annotation like @Implies allows IDEs to implement that feature more efficiently that's perfectly fine.

    I think we would support both JSR 305 and codeanalysis annotations for some time (let say 1 year then sunset JSR 305 and let our null-safety annotations only annotated with codeanalysis annotations.

  8. artempyanykh commented on Mar 29, 2020

    @artempyanykh
    Collaborator

    In Nullsafe we use @Nullsafe class-level annotation to

    • mark that the class is DefaultNonNull
    • allow users to configure certain aspects of checker's behaviour.

    Annotation aliasing would be very helpful for us at least for the following reasons:

    • Help with interop between @Nullsafe Java classes and Kotlin code.
    • Make OSS FB libraries (that again use @Nullsafe classes) more friendly for the community.

    Given that annotations currently present in the design are parameterless there's not much difference between

    1. @Implies-annotation
    @Implies({DefaultNonNull.class})
    public @interface Nullsafe ...
    
    1. Aliasing via application
    @DefaultNonNull // alias
    public @interface Nullsafe ...
    

    No.1 is clearer, but No.2 (w/ a comment) is not too bad either, and added flexibility in allowing to specify arguments to aliased annotation seems to be a nice bonus.


    Notably it wouldn't be a complete solution, since there will be users who want to alias an annotation they don't own and the owner won't do it.

    @kevinb9n this is somewhat restrictive, but I don't feel this is necessarily a bad thing. At the very least this prevents people from producing inconsistent aliases. In this (rather far-fetched) example I wouldn't like to be the person who depends on MySuperAnnot and both libA and libB if

    libA:

    @Nullable
    public @interface MySuperAnnot ...
    

    libB:

    @NonNull
    public @interface MySuperAnnot ...
    

    @donnerpeter do I understand correctly that your concern with option No.2 (aliasing via application) is that it would require to scan all @interface declarations to build aliasing index compared to just uses of @Implies (but the resulting index size would be the same)?

  9. donnerpeter commented on Apr 1, 2020

    @donnerpeter
    Collaborator

    @artempyanykh It'd require to either scan all @interface declarations in all JVM languages, or all usages of @NotNull, or the intersection of these two sets (and do it after any change in the project, any typing).

    I'm not sure which index you mean and what would be the same.

  10. artempyanykh commented on Apr 2, 2020

    @artempyanykh
    Collaborator

    @donnerpeter sorry, I have 0 experience in implementing IDEs, but want to understand better the situation, so please bear with me.

    To support aliases in Infer, we would just lazily build a mapping from encountered annotations to their base jspecify analogues as we walk the CFG. Since this is a batch job, the overhead of this lookup would be low and there wouldn't be much difference between @Implies(DefaultNonNull.class), @DefaultNonNull public @interface MyAnnot, or the combination.

    Now in IDE this should happen incrementally, but I don't quite understand why the cost cannot be efficiently amortised.

    For instance:

    ...and do it after any change in the project, any typing

    Why would it need to be any typing? I'd assume it depends on the "context", so if someone is not editing annotation classes or changing annotations on elements there's no need to rebuild the index of aliases. Or am I missing something?


    On related note, perhaps one aspect of @TypeQualifierNickname that I find questionable is its recursive nature. Theoretically, it allows for very flexible setups, but I have found 0 usages of it both in our Android and server code. This may very well be idiosyncratic to our codebase, so input from people with more sophisticated setups would be appreciated.

    But anyhow, would it be easier to limit the scope of such annotation to be just a marker to simplify life of IDE implementers (if it's really needed) without the requirement to propagate annotations from an alias to annotations this alias is applied to?

    So that when someone wants to alias some base annotation from jspecify they would need to write:

    import jspecify.nullability.DefaultNonNull
    import jspecify.meta.Alias
    
    @Alias
    @DefaultNotNull
    @Nullsafe { ... }
  11. donnerpeter commented on Apr 3, 2020

    @donnerpeter
    Collaborator

    Why would it need to be any typing? I'd assume it depends on the "context", so if someone is not editing annotation classes or changing annotations on elements there's no need to rebuild the index of aliases. Or am I missing something?

    You're right, in an ideal world that'd be so. Unfortunately it's too expensive to determine what an edit (or an update from version control, or a library jar change) could affect across the project, so, at least in IntelliJ, we drop most caches on any change, and try to be fast in recomputing them.

    Non-recursive aliasing would indeed simplify the life of (our) IDE. But explicit marking seems much more important IMHO.

  12. kevinb9n commented on May 8, 2020

    @kevinb9n
    CollaboratorAuthor

    I'm still not in a position to comment on the performance implications, but can only speak to my continued positive impressions of the benefits of the feature.

    First, the topic of this comment is: what would it mean, exactly?

    Assuming this simple usage:

    @Implies(Foo.class)
    public @interface Bar {}
    

    And this generalized usage of our example annotation:

    // elsewhere
    @Bar /* some annotatable element here*/
    

    Implies says that IF @Foo is also legal to appear in that context (by its target type):

    // elsewhere
    @Bar @Foo /* some annotatable element here*/
    

    then these two snippets just above convey identical information; no meaning should be ascribed to the difference between them.

    (Of course, checkers that want to issue a warning to the latter that @Foo can be safely removed would be welcome to, so that is a difference in behavior, but still not a difference in meaning.)

    We would say that @Bar is a "specialization" or "refinement" of @Foo, and in fact @Refines and @Specializes are potential names for the annotation.

    Bar may or may not add semantics of its own. If it does not, we may use the term "alias" to describe it, and its javadoc should presumably say nothing but "Alias for {@link Foo}". (We might wish in theory that the @Implies link could be made in both directions, but in real life we'd have to be content with just one.) In this alias scenario, each client codebase may have its own policy for which annotation's usage is preferred; it is probably not something that @Bar itself can define and thus not something @Implies would have anything to do with.

    (The alias use case makes the name @Implies a little more attractive than @Refines or @Specializes, since the latter might subjectively be seen as suggesting more than the trivial refinement.)

    All the above doesn't provide any solution for implying another annotation with particular attribute values; e.g. you can't define a @Bar that implies @Foo("value here"). However (a) many of these cases would want some "pass-through" of values anyway, which it's hard to see any way to accomplish, and (b) for the hardcoded no-pass-through case, @Foo could choose to enable that differently, by adding @Target(ANNOTATION_TYPE) and documenting it to have that meaning when applied to annotations.

    (Aside: we've said we might offer small helper libraries for checker implementations or runtime/reflection code to use, if the need arises. A "does this element have this annotation on it" method that automatically traverses @Implies would be a simple example of this.)

  13. kevinb9n commented on May 8, 2020

    @kevinb9n
    CollaboratorAuthor

    Now starting to get at why I feel this is important,

    A thought experiment: if some codebase converts over to use our annotations completely, let's say that's what a 100% victory looks like for us. But what if it does not use our annotations at all, but it does use annotations that contain hard links via @Implies to ours? Would we call that a 50% victory, or 80%, or 10%, or what?

    imho, I think of it as like 90% victory. Because it can still be very concretely observed in the code that our semantics are the ones in play.

    If they use annotations that maybe mention ours in javadoc, that's kinda okay, except that that's going to serve no purpose until they go around configuring all their tools to say "please recognize our annotation as their annotation" (which would happen magically if we had @Implies). Of course, in the real world people will do that without even bothering to mention this in their annotations' javadoc, and from their pov, why should they?

  14. kevinb9n commented on Aug 26, 2020

    @kevinb9n
    CollaboratorAuthor

    A few design points:

    • @Implies still seems like a fine name to me
    • It should take a Class<? extends Annotation> ... it will only be useful for marker annotations
    • Possibly a boolean isAlias() default false might be useful, to signify that this class promises it adds no additional semantics and thus the two annotations are equivalent
    • The specified annotation type should have all the target types the current annotation type has [soft requirement]
    • The Implies javadoc should warn that it's important for "all" code that checks for the presence of an annotation to do it using a mechanism like that takes @Implies into account.
  15. 23 remaining items

  16. changed the title [-]Declaring that one annotation aliases or specializes another (`@Implies`) [working decision: yes][/-] [+]Declaring that one annotation aliases or specializes another (`@Implies`) [working decision: no][/+] on Nov 22, 2022
  17. added
    design
    and removed on Nov 30, 2022
  18. modified the milestones: 1.0, Post-1.0 on Mar 13, 2024
  19. netdpb commented on Apr 9, 2024

    @netdpb
    Collaborator

    The current, working decision, as of release 0.3, is that JSpecify 1.0 will not include a general specification of one annotation implying another.

    Although this use case is important to large library providers such as Spring Framework (see #307 for further discussion), its requirements are also quite complex. We won't delay 1.0 for this use case, but we should figure out what to do after 1.0.

    I propose the current decision be finalized for 1.0. If you agree, please add a thumbs-up emoji (👍) to this comment. If you disagree, please add a thumbs-down emoji (👎) to this comment and briefly explain your disagreement. Please only add a thumbs-down if you feel you can make a strong case why this decision will be materially worse for users or tool providers than an alternative.

  20. added
    needs-decision-post-1.0
    and removed on Apr 9, 2024
  21. sdeleuze commented on Apr 10, 2024

    @sdeleuze
    Collaborator

    I won't thumb-down this one, as I think it is great that 1.0 with current scope is released asap and I am not sure we need a mechanism as general/complex as @Implies, but I want to express (again) that the lack of meta-annotation/alias capability is a blocker for Spring Framework usage of JSpecify, so my ask would be to plan that feature for 1.1 and treat it as high priority after 1.0 release.

    Could be as simple as supporting annotating Spring null-safety annotation with JSpecify ones (I understand there are performances implications for checkers, but IMO this is doable, we are talking about build-time tooling, not runtime analysis). So we should probably discuss #7 (comment) before 1.0.

  22. changed the title [-]Declaring that one annotation aliases or specializes another (`@Implies`) [working decision: no][/-] [+]Declaring that one annotation aliases or specializes another (`@Implies`)[/+] on Aug 14, 2024
  23. added a commit that references this issue on Sep 19, 2024
  24. kevinherron commented on Jun 3, 2025

    @kevinherron

    @kevinb9n did this proposal ever get any traction?

    I'm modelling a domain where there are "structures with optional types" but for reasons, java.util.Optional isn't the answer. Currently I just use @Nullable on fields/getters for the fields that may be optional, but what I'd really like to do is introduce two new annotations, @Required and @Optional, that imply JSpecify @NonNull and @Nullable, respectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    designimpliesneeds-decision-post-1.0

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions