Skip to content

Decide retention for our annotations #28

Closed

Description

@kevinb9n
No description provided.

Activity

  1. kevinb9n commented on Apr 19, 2019

    @kevinb9n
    CollaboratorAuthor

    I'm not really aware of a reason why we would ever regret using runtime retention.

    And whenever we don't, we will sooner or later be asked to.

  2. wmdietlGC commented on Apr 20, 2019

    @wmdietlGC
    Collaborator

    For the Checker Framework, we provided a separate checker-qual-android artifact that uses CLASS retention, because RUNTIME seems to cause issues with the default Android configuration: typetools/checker-framework#2118 (comment)

    By default, the annotations are RUNTIME retention https://checkerframework.org/manual/#faq-runtime-retention

  3. kevinb9n commented on Jun 12, 2019

    @kevinb9n
    CollaboratorAuthor

    I think the only thing that makes sense is for us to make everything runtime retention. When users have performance-based reasons for wanting to turn that off, they can deal with that in their toolchain; I don't know if it's even any of our concern how they do.

  4. kevinb9n commented on Jun 24, 2019

    @kevinb9n
    CollaboratorAuthor

    I believe we consider this settled for now. No-runtime-retention versions of our annotations can be hacked up as a secondary matter.

  5. cpovirk commented on May 8, 2020

    @cpovirk
    Collaborator

    (We now have #96 to consider a special case for nullness. It hasn't seen much activity, but I wanted to cross-link these 2 issues.)

  6. cpovirk commented on Dec 15, 2020

    @cpovirk
    Collaborator

    (Just to pile on: I was just reminded that Guice is an example of a library that requires runtime retention for nullness annotations. But that said: It also ignores type annotations entirely! So that's something that will affect us inside Google, at least.)

  7. added a commit that references this issue on Dec 15, 2020
  8. cpovirk commented on Jan 25, 2021

    @cpovirk
    Collaborator

    I saw a concern internally about runtime retention on Android [edit: the same one mentioned above]: Under older versions of Android, a runtime-visible annotation must appear in the same dex as all classes that use it. For widely-used nullness annotations, this would force a huge number of classes to appear in a single dex, exceeding the dex limit.

    It may be that the solution to this is going to be to still use RUNTIME retention but to declare CLASS-retention aliases (using the system to be designed in #35). However, if the need for CLASS-retention aliases is going to be so large, we should at least consider providing a standard annotation, rather than requiring everyone to redeclare it.

  9. cpovirk commented on Jan 25, 2021

    @cpovirk
    Collaborator

    Hmm, or does the problem arise only with annotations that have an enum element?? I will try to learn more about this. If so, this is yet another reason to avoid enum elements.

  10. cpovirk commented on Jan 25, 2021

    @cpovirk
    Collaborator

    Ah: I think the problem with old versions of Android was indeed specific to annotations with an enum element. But the workaround for that problem in the Android build tools forces any runtime-retention annotation to be in the same dex as the classes that use it. So currently, even a plain runtime-retention @Nullable is likely to cause issues.

    Perhaps the workaround could be improved, but we'd need to look into that.

  11. cpovirk commented on Jan 25, 2021

    @cpovirk
    Collaborator

    But wait, no? @Nullable will be a type annotation, which doesn't show up in Android's bytecode at all.

    ...except... our "in the club" annotation will be a declaration annotation. It will cause the same problem.

    (I was starting to wonder if the "in the club" annotation could mostly be placed in package-info classes -- and, if so, whether those show up in Android bytecode. But we definitely can't assume that universally -- probably especially for protobuf (the context in which I heard about this problem), as I believe users often generate their protobufs into the same package as other code. If so, they'd really need to annotate the whole package (which can be a pain) or else opt out with a "not in the club" annotation -- which would trigger the same problem!)

  12. kevin1e100 commented on Jan 26, 2021

    @kevin1e100
    Collaborator

    Note the combination of type and runtime-retention declaration annotations is somewhat scary for Android also (regardless of Android version): @DefaultNotNull declaration annotations would be visible at runtime, but any @Nullable type annotations wouldn't be, for instance. So it wouldn't be possible to determine whether a value was meant to be nullable at runtime, and in fact, the presence of @DefaultNotNull may suggest the opposite.

  13. cpovirk commented on Jan 26, 2021

    @cpovirk
    Collaborator

    Thanks. I recall your pointing that out in #96, and we should definitely have all these points in front of us.

    I'm finding myself rapidly convinced that we need a type-use, runtime-retention Nullable (for Guice) and a declaration, class-retention InTheClub (for multidex). That handily solves the problem you've raised, too. So at the very least, I'm inclined to try that internally to see if it shakes out any other fun surprises.

    We can presumably still define aliases with different retentions (or users can do it themselves), but those are feeling like the least dangerous defaults.

  14. 15 remaining items

  15. cpovirk commented on Dec 30, 2021

    @cpovirk
    Collaborator

    One thing that's convenient (which we've already said a bit about above): One of the platforms most likely to care about code size, Android, already leaves type-use annotations out of the final binary. So, if we can make it practical for most people to use type-use annotations, then there's no need for Android apps to fear runtime retention for those annotations, in contrast to what some do now:

    • javax.annotation.Nullable vs androidx.annotation.Nullable
      • Always prefer androidx.annotation.Nullable.
      • It uses @Retention(SOURCE) [cpovirk: actually, CLASS] rather than @Retention(RUNTIME).

    That still leaves the possibility that @NullMarked is used often enough to cause concern, as I feared above. But if that comes up, we have options.

  16. modified the milestones: 1.0, 0.3 on Dec 2, 2022
  17. added
    design
    on Mar 16, 2023
  18. added
    dev
    on Mar 13, 2024
  19. netdpb commented on Apr 9, 2024

    @netdpb
    Collaborator

    The current, working decision, as of 2021-12-08, is that JSpecify's annotations will have runtime retention, since some reflection-based tools, like Guice, require runtime retention for annotations.

    The argument for changing this decision is that some platforms, like Android, are sensitive to code size and don't require runtime retention, so we should not impose those costs.

    However, platforms or applications that do not want the code size cost of runtime-retained annotations can use tools to strip them out of binary code before deployment. We shouldn't preclude reflection-based tools from using nullness information.

    This must be decided before version 1.0 of the jar.

    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.

    Results: Eight 👍; no other votemoji.

  20. netdpb commented on May 21, 2024

    @netdpb
    Collaborator

    Decision: JSpecify's annotations will have runtime retention.

  21. changed the title [-]Decide retention for our annotations [working decision: runtime][/-] [+]Decide retention for our annotations[/+] on May 21, 2024
  22. laeubi commented on Jan 31, 2025

    @laeubi

    Runtime for compiler / tool targeting annotations is simply wrong as it will especially for dependent projects cause issues, e.g. compiler will probably complain and you force all your users that depend on a jar using jspecify to have jspecify on the classpath, I can only ask to reevaluate the decision.

  23. cpovirk commented on Jan 31, 2025

    @cpovirk
    Collaborator

    The annotations are also used by tools at runtime, such as by Guice for dependency injection and NullPointerTester for testing.

  24. agentgt commented on Jan 31, 2025

    @agentgt

    @laeubi I feel your pain but realize the only other choice would be assuming no one wants to do runtime checks is CLASS since SOURCE will not be enough if you want analysis to work across compile time boundaries.

    The difference between CLASS and RUNTIME at compile time and compile dependencies is for all extensive purposes the same. The only guarantee is that CLASS annotations will not be loaded by reflection via getAnnotations. However with all annotations one could just do SomeAnnotation.class.getName(); so it is not a guarantee with the retention.

    Regardless you can always (but probably should not) explicitly exclude the dependency provided no one tries load up the JSpecify classes. This is because annotations are lazy loaded (ignoring graalvm native at the moment). I actually do this all the time for checkerframework since a ton of projects use its annotations. I just exclude them.

    BTW it is notable that the only annotation library that supports CLASS is Eclipses. That is all the others (pre jspecify) are using RUNTIME including spring's older annotations (I think they have switched to JSpecify now?).

    So hopefully you just have one runtime annotation to worry about in the long run.

  25. sdeleuze commented on Feb 3, 2025

    @sdeleuze
    Collaborator

    Spring Framework main branch has indeed switched to JSpecify, and we have introduced a new Nullness API that requires runtime retention.

    BTW the working group has carefully designed JSpecify annotations to avoid Java compiler warnings like the one triggered by the JSR 305 enum attributes.

  26. cpovirk commented on Aug 14, 2026

    @cpovirk
    Collaborator

    @msridhar recently passed along an example of a project that documented that it was avoiding JSpecify because of RUNTIME retention.

    However, the project's cited concern was around whether the annotations are present in the class file. But as #28 (comment) discusses, the only real alternative is CLASS retention, which likewise puts the annotations in the class file.

    Reading between the lines, I interpret the project's decision to use [edit: SpotBugs plus the javax.annotation.concurrent portion of] JSR-305 as about either:

    (I'll also note that Android projects could still choose to worry about the combination of RUNTIME type-use annotations with CLASS declaration annotations. But I'd expect that to be a negligible concern. And the much bigger Android concern around legacy multidex should be obsolete nowadays, as we've seen inside Google.)

    This all still reminds me of the idea of a separate "version" of JSpecify with a different retention policy. I felt more worried about that as I thought about it more, so it seems fairly unlikely to me at present. Maybe the more promising approach is to provide a tool/configuration for changing retention or stripping annotations at the end of the build, though of course that wouldn't help with the compile-time problem [edit: that would exist even with CLASS retention!] that this particular project might well be focused on.

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

    designdev

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions