You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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.
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.
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.
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!)
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.
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.
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.
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.
Reacted by Liam Miller-Cushon, Chris Povirk, Sébastien Deleuze, David P. Baker, Kevin Bourrillion, Manu Sridharan, Werner Dietl and Lázaro Clapp
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.
@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.
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.
@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:
general concern that support for runtime reflection is a bigger commitment / breaking change than support for compile-time static analysis (and the resulting generated runtime checks in Kotlin)
FWIW: At least in Google, all the practical trouble that I can recall would arise even with just CLASS retention.
Activity
kevinb9n commentedon Apr 19, 2019
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.
wmdietlGC commentedon Apr 20, 2019
For the Checker Framework, we provided a separate
checker-qual-androidartifact 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
kevinb9n commentedon Jun 12, 2019
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.
kevinb9n commentedon Jun 24, 2019
I believe we consider this settled for now. No-runtime-retention versions of our annotations can be hacked up as a secondary matter.
cpovirk commentedon May 8, 2020
(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.)
cpovirk commentedon Dec 15, 2020
(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.)
Change retention to RUNTIME.
cpovirk commentedon Jan 25, 2021
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
RUNTIMEretention but to declareCLASS-retention aliases (using the system to be designed in #35). However, if the need forCLASS-retention aliases is going to be so large, we should at least consider providing a standard annotation, rather than requiring everyone to redeclare it.cpovirk commentedon Jan 25, 2021
Hmm, or does the problem arise only with annotations that have an
enumelement?? I will try to learn more about this. If so, this is yet another reason to avoidenumelements.cpovirk commentedon Jan 25, 2021
Ah: I think the problem with old versions of Android was indeed specific to annotations with an
enumelement. 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@Nullableis likely to cause issues.Perhaps the workaround could be improved, but we'd need to look into that.
cpovirk commentedon Jan 25, 2021
But wait, no?
@Nullablewill 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-infoclasses -- 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!)kevin1e100 commentedon Jan 26, 2021
Note the combination of type and runtime-retention declaration annotations is somewhat scary for Android also (regardless of Android version):
@DefaultNotNulldeclaration annotations would be visible at runtime, but any@Nullabletype 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@DefaultNotNullmay suggest the opposite.cpovirk commentedon Jan 26, 2021
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.
15 remaining items
cpovirk commentedon Dec 30, 2021
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:
That still leaves the possibility that
@NullMarkedis used often enough to cause concern, as I feared above. But if that comes up, we have options.netdpb commentedon Apr 9, 2024
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.
netdpb commentedon May 21, 2024
Decision: JSpecify's annotations will have runtime retention.
[-]Decide retention for our annotations [working decision: runtime][/-][+]Decide retention for our annotations[/+]laeubi commentedon Jan 31, 2025
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.
cpovirk commentedon Jan 31, 2025
The annotations are also used by tools at runtime, such as by Guice for dependency injection and
NullPointerTesterfor testing.agentgt commentedon Jan 31, 2025
@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.
sdeleuze commentedon Feb 3, 2025
Spring Framework
mainbranch has indeed switched to JSpecify, and we have introduced a newNullnessAPI 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
enumattributes.cpovirk commentedon Aug 14, 2026
@msridhar recently passed along an example of a project that documented that it was avoiding JSpecify because of
RUNTIMEretention.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
CLASSretention, 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.concurrentportion of] JSR-305 as about either:NullMarkeditselfCLASSretention.(I'll also note that Android projects could still choose to worry about the combination of
RUNTIMEtype-use annotations withCLASSdeclaration 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
CLASSretention!] that this particular project might well be focused on.