jpy-consortium / jpy

Apache License 2.0
68 stars 16 forks source link

Specific typing information lost when executing certain java patterns #117

Open devinrsmith opened 8 months ago

devinrsmith commented 8 months ago

Certain patterns that compile in java are not currently executable in jpy.

For example,

import jpy

_DeserializationFeature = jpy.get_type(
    "com.fasterxml.jackson.databind.DeserializationFeature"
)
_JsonMapper = jpy.get_type("com.fasterxml.jackson.databind.json.JsonMapper")
_JsonReadFeature = jpy.get_type("com.fasterxml.jackson.core.json.JsonReadFeature")

print(
    _JsonMapper.builder()
    .configure(_DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, True)
    .enable(_JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS)
    .build()
)

fails with

Type: <class 'RuntimeError'>
Value: no matching Java method overloads found

If the code is modified as such:

print(
    _JsonMapper.builder()
    .enable(_JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS)
    .configure(_DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, True)
    .build()
)

it works (enable comes first).

This is probably because jpy uses the base typing information from MapperBuilder as opposed to the more specific type present in the case of JsonMapper.Builder wrt com.fasterxml.jackson.databind.cfg.MapperBuilder#configure. It would be ideal if jpy knew that JsonMapper#configure returned a JsonMapper.

public class JsonMapper extends ObjectMapper
{
...
    public static class Builder extends MapperBuilder<JsonMapper, Builder> {
    ...