• getting generic type parameter in method

    From Philipp Kraus@21:1/5 to All on Wed Oct 21 04:38:57 2020
    Hello,

    I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:

    public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
    {
    if ( p_class.isAssignableFrom( List.class ) )
    {
    final List<?> l_data = JsonDataConverter.getInstance().fromData( p_bytes, List.class, List.class );
    // here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
    ....
    }
    ...
    }

    I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.

    Can you please give a little bit help to get the information?
    Thanks a lot

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eric Sosman@21:1/5 to Philipp Kraus on Wed Oct 21 08:43:06 2020
    On 10/21/2020 7:38 AM, Philipp Kraus wrote:
    Hello,

    I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:

    public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
    [...]
    I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.

    I'm not up to speed on all the latest developments, but I think
    you're out of luck at least through Java 15. JLS 4.7 describes which
    types are "reifiable," and includes a discussion on why some types
    (including most generic types) are not. Perhaps in some future Java
    things will change, but I don't believe they've changed yet.

    (However, "perhaps" I'm wrong. I'm at Java 11 these days, and
    have not fully explored the more recent releases.)

    --
    esosman@comcast-dot-net.invalid
    "Total honesty is what we as citizens deserve from our president."
    -- Melania Trump
    Ninety-one days to go.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marcel Mueller@21:1/5 to All on Wed Oct 21 15:28:17 2020
    Am 21.10.20 um 13:38 schrieb Philipp Kraus:
    Hello,

    I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:

    public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
    {
    if ( p_class.isAssignableFrom( List.class ) )
    {
    final List<?> l_data = JsonDataConverter.getInstance().fromData( p_bytes, List.class, List.class );
    // here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
    ....
    }
    ...
    }

    Java uses *type erasure*. So even the Java Runtime does not know the
    type of T when executing the method.

    The only way is to pass the type class along by your own. By using
    Class<T> you can at least avoid inconsistencies when calling the method.


    Marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Arne_Vajh=c3=b8j?=@21:1/5 to Philipp Kraus on Wed Oct 21 21:33:15 2020
    On 10/21/2020 7:38 AM, Philipp Kraus wrote:
    Hello,

    I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:

    public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
    {
    if ( p_class.isAssignableFrom( List.class ) )
    {
    final List<?> l_data = JsonDataConverter.getInstance().fromData( p_bytes, List.class, List.class );
    // here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
    ....
    }
    ...
    }

    I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.

    Can you please give a little bit help to get the information?

    As already pointed out by other then getting the information from
    l_data itself may be impossible due to type erasure.

    But even if Java did not lose the type, then you would probably be told
    Object, since the JSON in theory can contain both integers and strings.

    If you for whatever reasons are sure that all elements have the same
    type, then you can get the first element and look at its type.

    Arne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Philipp Kraus@21:1/5 to All on Thu Oct 22 04:21:30 2020
    Arne Vajhøj schrieb am Donnerstag, 22. Oktober 2020 um 03:33:36 UTC+2:
    On 10/21/2020 7:38 AM, Philipp Kraus wrote:
    Hello,

    I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:

    public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
    {
    if ( p_class.isAssignableFrom( List.class ) )
    {
    final List<?> l_data = JsonDataConverter.getInstance().fromData( p_bytes, List.class, List.class );
    // here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
    ....
    }
    ...
    }

    I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.

    Can you please give a little bit help to get the information?
    As already pointed out by other then getting the information from
    l_data itself may be impossible due to type erasure.

    But even if Java did not lose the type, then you would probably be told Object, since the JSON in theory can contain both integers and strings.

    If you for whatever reasons are sure that all elements have the same
    type, then you can get the first element and look at its type.

    Arne


    I'm implementing this interface https://github.com/uber/cadence-java-client/blob/master/src/main/java/com/uber/cadence/converter/DataConverter.java#L50
    for my own datatypes based on this example https://github.com/uber/cadence-java-samples/pull/37/files

    I'm in the fromData but if have as target type List<MyDataStruct> I can detect that I have got a List as target, but I need to know the generic type of the list for calling the right deserialization of my own datatype.
    I have got an own serialization of MyDataStruct but I have got multiple deserialization call for each of my own structures, but I need to know the concret type to call the right deserialization method.

    At the moment I cannot deserialize a list of my own datastructures, because I don't know which deserialization method must be called

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From e.d.programmer@gmail.com@21:1/5 to All on Thu Oct 22 04:16:29 2020
    // here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
    ....
    }
    ...
    }

    I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.

    Can you please give a little bit help to get the information?
    Thanks a lot

    If you don't know explicitly what you're deserializing just use Object then check instanceof?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Philipp Kraus@21:1/5 to All on Thu Oct 22 04:16:07 2020
    Arne Vajhøj schrieb am Donnerstag, 22. Oktober 2020 um 03:33:36 UTC+2:
    On 10/21/2020 7:38 AM, Philipp Kraus wrote:
    Hello,

    I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:

    public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
    {
    if ( p_class.isAssignableFrom( List.class ) )
    {
    final List<?> l_data = JsonDataConverter.getInstance().fromData( p_bytes, List.class, List.class );
    // here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
    ....
    }
    ...
    }

    I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.

    Can you please give a little bit help to get the information?
    As already pointed out by other then getting the information from
    l_data itself may be impossible due to type erasure.

    But even if Java did not lose the type, then you would probably be told Object, since the JSON in theory can contain both integers and strings.

    If you for whatever reasons are sure that all elements have the same
    type, then you can get the first element and look at its type.

    Arne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)