Skip to content

Error Prone / NullAway support for JSpecify #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 5, 2025

Conversation

bbakerman
Copy link
Member

@bbakerman bbakerman commented Jun 4, 2025

related to #195

Putting ErrorProne / NullAway in place


tasks.withType(JavaCompile) {
options.release = 11
options.errorprone {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

builds with v17 - targets v11

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm .. I don't think we can do that: build with 17 would allow to use APIs which are in 17, but not in 11, which would result in errors when run with 11.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to achieve errorprone / nullaway checking without this

I took this from @bclozel examples : bclozel@f449972

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be safe as the "-release" option is tailored for that:

When using the --release option, only the supported documented API for that release may be used; you cannot use any options to break encapsulation to access any internal classes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.gradle.org/current/userguide/building_java_projects.html#sec:compiling_with_release

Using release property is possible starting from Java 10.
Selecting a Java release makes sure that compilation is done with the configured language level and against the JDK APIs from that Java version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we are running the v17 compiler but using the "release" of JDK 11 including libraries

@@ -142,7 +144,7 @@ private static DataLoaderOptions setInInstrumentation(DataLoaderOptions options,
*/
public DataLoaderRegistry register(DataLoader<?, ?> dataLoader) {
String name = dataLoader.getName();
assertState(name != null, () -> "The DataLoader must have a non null name");
Objects.requireNonNull(name, "The DataLoader must have a non null name");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tooling understands Objects.requireNonNull but not out nonNull method

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can teach tooling about this custom assertion method.

Spring Framework does that with a custom @Contract annotation, used on the relevant methods.

In a nutshell, two possibilities:

  1. use methods like Objects.requireNonNull, already known by tooling
  2. OR create a custom contract annotation, annotate the relevant methods in your codebase with it and teach NullAway about this annotation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - for the record the reason we have a custom Assert class is that back in Java 6 days - Objects.requireNonNull() didnt exist and we wanted near zero dependendencies - so we made out own

Same in graphql-java btw

* as expected in Kotlin land. We don't intend to ue Kotlin in our tests
* or to deliver Kotlin code in the java
*/
class KotlinExamples {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just enough Kotlin - tests only

confirmed the build .jar has no Kotlin classes!

}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
languageVersion = JavaLanguageVersion.of(17)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to run on 17 to get errorprone happy (compiled for 17)

but we target 11 later

@bbakerman bbakerman added this to the 5.0.1 milestone Jun 5, 2025
@bbakerman bbakerman merged commit 5d61833 into master Jun 5, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants