Bypassing compiler generated null checks

Hi all,

I’m in the process of writing a library for downloading dependencies at runtime. Many people use this to download the Kotlin standard library, with the class calling it written in Kotlin.

A small problem arises when using something like this:

val dependencyManager = PDMBuilder(this).build()
dependencyManager.loadAllDependencies().join()

The Kotlin compiler simply insists upon generating a null check, even though the result of build() is annotated with @NotNull.

To do this, it uses the Intrinsics class, which isn’t present on the classpath yet, causing an error.

I’ve found a reasonably simple workaround which is using the !! operator or returning with the Elvis operator after calling build(), but both of these prompt the “unnecessary operator” warning in the IDE.

Is there a better workaround for doing this? I also found it a little bit tricky to reproduce in a much more simple project.

Thanks

For clarification, this is the decompiled code.

PluginDependencyManager var10000 = (new PDMBuilder((Plugin)this)).build();
      Intrinsics.checkNotNullExpressionValue(var10000, "PDMBuilder(this).build()");
      PluginDependencyManager dependencyManager = var10000;
      dependencyManager.loadAllDependencies().join();
1 Like

I think the -Xno-param-assertions compiler flag should work

I’d consider writing your bootstrap code in Java.

Even if you get it to work in Kotlin now, there’s always the risk that future changes to the compiler and/or runtime – or changes to your code – could cause similar errors in future.

3 Likes