2

Suppose there is Java library that's declared @NonNullApi, but some its methods actually may consume null parameter.

Is there way to ignore non-nullability requirement and pass null there from Kotlin?

UPD Need to clarify: method is declared as non-nullable by 3rd party library, there's no way to change its code. The goal is to break limitation from calling side.

Obvious way is to use Reflection, but maybe Kotlin provides just syntax for that?

5
  • Annotate the parameter with @Nullable Commented Nov 18, 2019 at 14:46
  • Do you mean parameter declaration? This is 3rd party library, cannot change it. Commented Nov 18, 2019 at 14:48
  • What library is it? What method do you want to pass null into? Commented Nov 18, 2019 at 14:51
  • 1
    If it's a bug in the library to make this parameter non-nullable, then you could write a Java method that accepts null and passes it to the library, and call this Java method from Kotlin. Commented Nov 18, 2019 at 15:02
  • E.g. I've found such method in spring-context: MessageSource.getMessage actially can accept @Nullable String code Commented Oct 15, 2023 at 2:22

2 Answers 2

4

A distinct non-answer: don't do that!

That API documentation says: do not send null this way.

Even when the implementation today doesn't throw at you for passing in null, who says that isn't a bug, and that it won't be fixed in some upcoming version of that library?

In other words: write your code to match the specified API.

When the library implementation is more generous than the API specification, then rather follow the stricter rules. You should even consider to write up a but report for that library.

Sign up to request clarification or add additional context in comments.

Comments

2

As the other answer here points out, it's better not to overcome the nullability contract of a Java function expressed with nullability annotations.

But if you really believe that those annotations are there by mistake, you can use the following workaround:

  • Declare a helper static function in an utility Java class beside your Kotlin code. This function does not have NonNull annotation on those parameters where you need to pass null and it just forwards them to the method in question. The Java compiler doesn't prevent calling that method even when the nullability annotations do not match.
  • Import that static helper function to Kotlin and call it from there.

2 Comments

Thanks! Sure, this hack shouldn't be used for a well-designed API. It's sad that such a simple thing requires so much code.
@AlexeyAdamovskiy well,... I think it's better this way (so much code) then the other (having a simple fix for this)... the library designer doesn't want to accept null-values here... that the function works with null-values could just be a coincidence... and if it were a mistake and null should be accepted, then you better contact that library designer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.