Additionally, some of the values are primitives and are not of type Object and need to be coerced:

Int to java.lang.Integer

Double to java.lang.Double

Scala does this already, but can’t implicitly convert from Option[primitive] to Option[Object].

Worst of all, the thought of coding - and maintaining - a function per case class is untenable. If another programmer were to add or remove a field from the case class, then they would have to know to come here and update this function: the compiler wouldn’t inform them that a field in the class wasn’t being passed to the Java function. That’s unacceptable.

In my perfect, ideal world, it would be possible to just pass the case class to the Java function like so:

performJavaOp(person:_*)

So, is that possible? YES.

Every case class is automatically derived from Product, which means we can iterate over all their values and - with a little reflection - also get all the field names. All that’s needed to do is code up an implicit type conversion from any case class to an sequence of interspersed key/values: