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
{{ message }}
Repository navigation
Document on how to add the jspecify dependency: Maven and Gradle #503
Fwiw, because JSpecify can be used at runtime, I'd favor api in Gradle (and not-provided, not-optional in Maven). Those who don't use the annotations at runtime, and don't want to ship the JSpecify JAR in their application can safely exclude it from their deliverables, but the rule should be that it's "included by default".
(it is an anti-pattern to mark annotation dependencies as compileOnly or compileOnlyApi, it depends on the annotation retention: source → compileOnly, class → compileOnlyApi, runtime → api; in Maven: source → provided/optional is the closest you can do, class or runtime → compile)
If you're using the java (or application) plugin in Gradle, rather than java-library, for an application, then you don't have api and compileOnlyApi, and you only have to choose between implementation and compileOnly, which is what I hinted above: if you don't use the annotations at runtime, and don't want to ship the JSpecify JAR in your application, you can safely use compileOnly, otherwise use implementation.
My personal opinion at this point is that jspecify should be declared as a normal library, not provided. If its provided IDEs get confused when showing sources.
For situations that want to avoid dependencies altogether, that generally implies a non-managed context where someone just downloads a jar. That is still viable so long as org.jspecify is marked requires static when libraries have a module-info.
I'm with @bowbahdoe on this. JSpecify unlike Checkerframework and Eclipse null analysis does not really specify if checking is static only.
That is it is not unreasonable for applications to use reflection to check if a field is nullable or not in a similar vain to checking if a parameter/field/return is Optional. That is there could be runtime based soft implementations of null checking especially given that RETENTION is RUNTIME.
Eclipse annotations are retention CLASS so they can largely be provided (and indeed is preferred when dealing with jlink as it still lacks a module-info).
My point is in terms of API exporting they are both requires transitive as the types of jspecify are being exposed. In fact the Eclipse compiler will actually complain if the annotation is on a generic if it is not exposed (this is probably a bug in ECJ but there is a solid chance that javac could omit warnings in the future!).
Thus the correct module-info.java is:
modulecom.mylib {
exportsexported.pack;
requirestransitivejava.base; // this is implicitrequirestransitiveorg.jspecify;
}
Maven in terms of Maven project and not poms being published from some other build system has no analog to bare requires. That is <scope>compile</scope> is actually requires transitive. That is requires would work with <scope>runtime</scope> and indeed gradle can produce such pom but with Maven your library that uses JSpecify will not compile. In theory Maven 4 will fix this.
Consequently and why I added that java.base reference is that JSpecify should be seen as something that if successful like reactive-streams would be put in java.base and therefore would become requires transitive anyway.
I have a hard time understanding why you would suggest including jspecify as api in gradle. As far as i know api should only be used when users of the library transitively require the dependency at compile time to use yours (for example when you expose guava specific collections you should use api for guava because guava is part of the public api of your lib), but your nulless checks, annotations & tools are not interesting to your consumers. Important for them is seeing the annotations on the public methods/fields/classes which they would even if you use implementation. Or is there a good reason i haven't seen/realised to use api?
My understanding is that the annotations would not be on the classpath for your users if you used implementation. I guess this would not be too hard for me to confirm, and I'll try to get around to it someday.
Even with that said: Tools can sometimes operate correctly even if the annotations aren't on the classpath. (References to the annotations would still appear in the class files for the library; tools just wouldn't necessarily be able to find the class files for the annotations.) But that's not always the case, and even if they do operate correctly, they may generate warnings. We do need to write more up here. At the moment, the best single reference I have is this section of the Guava wiki.
This is where you declare dependencies which are transitively exported to consumers, for compile time and runtime.
implementation
This is where you declare dependencies which are purely internal and not meant to be exposed to consumers (they are still exposed to consumers at runtime).
compileOnlyApi
This is where you declare dependencies which are required at compile time by your module and consumers, but not at runtime. This typically includes dependencies which are shaded when found at runtime.
While you can get away with implementation and module-info.java requires (and requires static) I don't think it is a good idea.
Important for them is seeing the annotations on the public methods/fields/classes which they would even if you use implementation. Or is there a good reason i haven't seen/realised to use api?
I'll give three reasons why:
Unless you do not annotate anything public they are exposed. TYPE_USE runtime annotations are an extension of the type. If you return or accept something annotated as Nullable it should be largely considered the same as returning say Guava's Optional and which case is then api (or compile in Maven or requires transitive in module-info).
The build systems and JPMS vary greatly across the board on scope (transitive visibility) but one thing they can all agree on is requires transitive aka api aka compile.
I think / hope in the long run that this library will largely become successful like reactive-streams or slf4j and would be included in java.base. Reactive Streams and SLF4J are API with specifications with the expectation that you will probably be exposing their types (perhaps less for slf4j). Reactive Streams is a great analog because of the coloring of reactive code the types need to be transitive. Ditto for collection libs (and is why Guava version pain exists) and I argue ditto for JSpecify.
We've added this documentation on the docs-1.0 branch, which will be merged soon.
I think you're still missing JPMS guidelines - my current understanding is that modular libraries should use requires transitive org.jspecify and not requires transitive static org.jspecify, but one needs to go through a couple of lengthy issues to figure that out
Activity
tbroyer commentedon Mar 29, 2024
Fwiw, because JSpecify can be used at runtime, I'd favor
apiin Gradle (and not-provided, not-optional in Maven). Those who don't use the annotations at runtime, and don't want to ship the JSpecify JAR in their application can safely exclude it from their deliverables, but the rule should be that it's "included by default".(it is an anti-pattern to mark annotation dependencies as
compileOnlyorcompileOnlyApi, it depends on the annotation retention: source →compileOnly, class →compileOnlyApi, runtime →api; in Maven: source → provided/optional is the closest you can do, class or runtime → compile)GregDThomas commentedon Mar 29, 2024
For libraries, yes. For applications, I would think
compileOnlywould be fine, no?tbroyer commentedon Mar 29, 2024
If you're using the
java(orapplication) plugin in Gradle, rather thanjava-library, for an application, then you don't haveapiandcompileOnlyApi, and you only have to choose betweenimplementationandcompileOnly, which is what I hinted above: if you don't use the annotations at runtime, and don't want to ship the JSpecify JAR in your application, you can safely usecompileOnly, otherwise useimplementation.bowbahdoe commentedon Jul 11, 2024
My personal opinion at this point is that jspecify should be declared as a normal library, not provided. If its provided IDEs get confused when showing sources.
For situations that want to avoid dependencies altogether, that generally implies a non-managed context where someone just downloads a jar. That is still viable so long as
org.jspecifyis markedrequires staticwhen libraries have a module-info.agentgt commentedon Jul 11, 2024
I'm with @bowbahdoe on this. JSpecify unlike Checkerframework and Eclipse null analysis does not really specify if checking is static only.
That is it is not unreasonable for applications to use reflection to check if a field is nullable or not in a similar vain to checking if a parameter/field/return is
Optional. That is there could be runtime based soft implementations of null checking especially given that RETENTION is RUNTIME.Eclipse annotations are retention CLASS so they can largely be
provided(and indeed is preferred when dealing with jlink as it still lacks a module-info).agentgt commentedon Jul 12, 2024
I didn't have time to elaborate yesterday but JSpecify in terms of Maven should be
And here is the controversial part if you are library writer if any part of your public API exposes
@Nullableyou technically should do:The reason is imagine if JSpecify was some sort of container analogous to
Optionalthat I will callOpt.The true analog of course is:
My point is in terms of API exporting they are both
requires transitiveas the types of jspecify are being exposed. In fact the Eclipse compiler will actually complain if the annotation is on a generic if it is not exposed (this is probably a bug in ECJ but there is a solid chance that javac could omit warnings in the future!).Thus the correct module-info.java is:
Maven in terms of Maven project and not poms being published from some other build system has no analog to bare
requires. That is<scope>compile</scope>is actuallyrequires transitive. That isrequireswould work with<scope>runtime</scope>and indeed gradle can produce such pom but with Maven your library that uses JSpecify will not compile. In theory Maven 4 will fix this.Consequently and why I added that
java.basereference is that JSpecify should be seen as something that if successful like reactive-streams would be put injava.baseand therefore would becomerequires transitiveanyway.netdpb commentedon Jul 16, 2024
We've added this documentation on the docs-1.0 branch, which will be merged soon.
nbrugger-tgm commentedon Jul 22, 2024
I have a hard time understanding why you would suggest including jspecify as
apiin gradle. As far as i knowapishould only be used when users of the library transitively require the dependency at compile time to use yours (for example when you expose guava specific collections you should useapifor guava because guava is part of the public api of your lib), but your nulless checks, annotations & tools are not interesting to your consumers. Important for them is seeing the annotations on the public methods/fields/classes which they would even if you useimplementation. Or is there a good reason i haven't seen/realised to useapi?cpovirk commentedon Jul 22, 2024
My understanding is that the annotations would not be on the classpath for your users if you used
implementation. I guess this would not be too hard for me to confirm, and I'll try to get around to it someday.Even with that said: Tools can sometimes operate correctly even if the annotations aren't on the classpath. (References to the annotations would still appear in the class files for the library; tools just wouldn't necessarily be able to find the class files for the annotations.) But that's not always the case, and even if they do operate correctly, they may generate warnings. We do need to write more up here. At the moment, the best single reference I have is this section of the Guava wiki.
GregDThomas commentedon Jul 22, 2024
implementation means it's on the runtime classpath, but not the compile time classpath.
api means it's on both the runtime and compile time classpath.
cpovirk commentedon Jul 22, 2024
Oops, yes, sorry. I am mixing up the Gradle configuration with the less flexible Maven configuration :) Thanks for the correction.
sdavids commentedon Jul 22, 2024
https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph
apiimplementationcompileOnlyApiagentgt commentedon Jul 22, 2024
While you can get away with
implementationand module-info.javarequires(andrequires static) I don't think it is a good idea.I'll give three reasons why:
TYPE_USEruntime annotations are an extension of the type. If you return or accept something annotated asNullableit should be largely considered the same as returning say Guava'sOptionaland which case is thenapi(orcompilein Maven orrequires transitivein module-info).requires transitiveakaapiakacompile.reactive-streamsorslf4jand would be included injava.base. Reactive Streams and SLF4J are API with specifications with the expectation that you will probably be exposing their types (perhaps less for slf4j). Reactive Streams is a great analog because of the coloring of reactive code the types need to be transitive. Ditto for collection libs (and is why Guava version pain exists) and I argue ditto for JSpecify.cpovirk commentedon Aug 20, 2024
I'll add to the above that
implementationdoesn't quite fully work even for usages inprivatemembers. While I wouldn't go so far as to run around telling people to never use it, I see this as chipping further away at the small number of cases in whichimplementationcould be advisable.pivovarit commentedon Dec 15, 2025
I think you're still missing JPMS guidelines - my current understanding is that modular libraries should use
requires transitive org.jspecifyand notrequires transitive static org.jspecify, but one needs to go through a couple of lengthy issues to figure that out