Non-primitive Number classes

3 views
Skip to first unread message

Christopher Morley

unread,
Feb 13, 2026, 12:25:34 PM (6 days ago) Feb 13
to Py4J Support and Comments
Hi - anyone still here?

I am working in some java code that has derived from Number, but the string representation of these objects is not parseable on the python side, so I get an error when accessing via python (also, the slicing problem - I lose the real type).

It would be nice if Gateway.getReturnObject could be convinced to use getReferenceReturnObject rather than assuming it is a primitive type, ie treat the class the way BigDecimal and BigInteger are treated in isPrimitiveObject:

protected boolean isPrimitiveObject(Object object) {
return object instanceof Boolean || object instanceof String
|| (object instanceof Number && !(object instanceof BigDecimal || object instanceof BigInteger))
|| object instanceof Character || object instanceof byte[];
}


I have two plausible mods, wondering which is preferred (or some other solution). Option 1 is a conservative approach, where py4j only assumes objects are primitive if they are a known primitive type:

protected boolean isPrimitiveObject(Object object) {
return object instanceof Boolean || object instanceof String
    || isPrimitiveNumber(object)
    || object instanceof Character || object instanceof byte[];
}

   protected boolean isPrimitiveObject(Object object) {
    return object.getClass().equals(java.lang.Double) ||
        object.getClass().equals(java.lang.Float) ||
        object.getClass().equals(java.lang.Integer) ||
        object.getClass().equals(java.lang.Long) ||
        object.getClass().equals(java.lang.Short) ||
        object.getClass().equals(java.lang.Byte);
}

Option 2, the java programmer is responsible for telling py4j not to treat a Number-derived class as a primitive, eg by deriving from a trivial marker interface like NonPrimitiveNumber:

protected boolean isPrimitiveObject(Object object) {
return object instanceof Boolean || object instanceof String
|| (object instanceof Number && !(object instanceof BigDecimal || object instanceof BigInteger || object instanceof NonPrimitiveNumber))
|| object instanceof Character || object instanceof byte[];
}
where NonPrimitiveNumber is an empty marker interface provided in the py4j package

Thoughts? Any interest in a patch of either approach?
Thanks
Chris

Reply all
Reply to author
Forward
0 new messages