Repository navigation
version compatibility
JSpecify supports any version of Java from 8 up (which is when type-use annotations made their debut).
However, we recommend building with as new a version of javac as you can. You can still ensure compatibility with versions as old as Java 8 by setting --release. (Setting -target/targetCompatibility, by contrast, does not ensure compatibility.)
We also recommend updating your runtime past Java 8 (not just for better JSpecify support but for performance improvements (e.g., compact strings) and more).
If you compile with javac from JDK 8, and use @NullMarked on any methods or constructors, you may be affected by this javac bug. It hasn't been fixed, and our attempts to work around it on our side haven't been fruitful.
Your workarounds:
- "Don't do that" (don't apply
@NullMarkedto methods or constructors; on the other hand, we think@NullUnmarkedshould be fine) - Of course, if upgrading to a newer JDK is possible, that would fix it. You can still build with
--release 8so that you can still run on Java 8. (There are sometimes reasons that getting a build to work with--releasecan be challenging (example), but it's usually trivial, and if it doesn't immediately work, you can try--systeminstead. If the code then builds without error, you're safe.) TODO: This may still produce warnings. - IF the linked bug is ever fixed, upgrade to a new enough
javacpatch release. You still won't be able to use the-Werroroption. - Continue to build with JDK 8 but switching to use the JDK 8-compatible javac9 codebase, as explained here TODO: say more. (and TODO: does the
-Werrorproblem still apply? Probably?)
TODO: Note that JDK 8 is more likely to have problems with type-use annotations in general (example)
TODO: And write about upgrading Error Prone if necessary.
There are two kinds of problems, one that can arise if you do not put the JSpecify annotations on your classpath and one that can arise if you do:
If you run with JDK 8, do not put the JSpecify annotations on your classpath, and use reflection on types that use type-use annotations like @Nullable and @NonNull, Java may throw exceptions. This is caused by JDK-8152174, which is fixed under newer JDKs. (This problem exists with any type-use annotations; it's not specific to JSpecify annotations.)
If you run with JDK 8, do put the JSpecify annotations on your classpath, and use reflection on NullMarked itself (not merely on an API that uses NullMarked, Java may throw exceptions. This is caused by our support for putting @NullMarked on a module, since modules aren't supported under Java 8. (One possible way to end up performing reflection on NullMarked itself is to ask Mockito to mock a class that is annotated with @NullMarked. If you want to mock such a class under Java 8, you can try opting out of copying annotations by calling MockSettings.withoutAnnotations.)
You may see "warning: unknown enum constant ElementType.MODULE." This can happen...
- if you use
@NullMarkedin your own code or... - if you use some Error Prone releases prior to 2.29.0 and even one of your dependencies uses
@NullMarked.
If you're using Error Prone, you'll probably want to upgrade.
If you use @NullMarked in your own code, we don't know of ways to eliminate the warning until you can upgrade your runtime past Java 8 and thus remove --release 8. You can at least avoid setting -Werror, which promotes that warning to a build error. (Some people may be interested in some more complex ideas that we've floated.)
The JSpecify annotations, like some other artifacts of nullness annotations (e.g., the Checker Framework, Eclipse), are type-use annotations. This enables them to be used in more locations, such as on the type argument in Future<@Nullable String>. Unfortunately, javac had a bug that prevented type-use annotations from being seen by whole-program annotation processors, such as Dagger. If you use one of these, you'll need to upgrade to JDK 22 in order to use any type-use annotations with it.
For more information, see https://jspecify.dev/docs/whether#annotation-processors.
In general, prefer to build with the newest version that you can, setting flags to tell it to maintain compatibility with the version you're actually targeting. One approach might be:
- Build with the newest JDK, setting
--releaseto the version you're targeting. If this works, you're done. - If that doesn't work, it might be because you're using internal JDK APIs from your target JDK version. (
--releaseis incompatible with--add-exports.) To make your build support them, set--systemto point to the target JDK, and set-sourceand-targetto the version of that target JDK. - If that doesn't work, it might be because you're using preview features. In that case, you'll probably need to build with the same version of the JDK that you're targeting. If it's possible to restrict your use of preview features to a small number of subprojects, then perhaps you can at least use that version only to build those subprojects.
- Someday, the JDK will finally drop support for targeting Java 8. If that happens, and if you're still targeting Java 8, then you'll have to stick with whatever the newest version that supports targeting Java 8 is. (As of this writing, a recent preview version of JDK 24 still supports Java 8.)
(With these approaches, you will not normally need Animal Sniffer, though it can remain useful on occasion, such as when targeting the same library at both JRE and Android users.)
Note: The contents of this wiki are not final, and represent current thinking and discussions.