ice icon indicating copy to clipboard operation
ice copied to clipboard

Remove Support for Writing `null` Enum Values in Java

Open InsertCreativityHere opened this issue 6 months ago • 1 comments

The static implementation of ice_write that slice2java generates for enums has a special case to handle null. If you try to marshal a null enumerator, we'll instead marshal the first declared enumerator. This seems very unexpected in my opinion.

My only guess is that we're trying to save users from possibly getting a NPE (null-pointer exception). But... Users really shouldn't be passing null enums around, and we shouldn't be trying to save them from the consequences of doing so.


The code-gen in question:

out << sp << nl << "public static void ice_write(com.zeroc.Ice.OutputStream ostr, " << name << " v)";
out << sb;
out << nl << "if(v == null)";
out << sb;
string firstEnum = enumerators.front()->mappedName();
out << nl << "ostr.writeEnum(" << absolute << '.' << firstEnum << ".value(), " << p->maxValue() << ");";
out << eb;
out << nl << "else";
out << sb;
out << nl << "ostr.writeEnum(v.value(), " << p->maxValue() << ");";
out << eb;
out << eb;

InsertCreativityHere avatar May 27 '25 18:05 InsertCreativityHere

I believe we also have a similar behavior for strings somewhere.

externl avatar May 27 '25 19:05 externl