Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39631

Re: getting generic type parameter in method

Path csiph.com!news.mixmin.net!aioe.org!.POSTED.5Avcpu9drOe6MAssky6/+Q.user.gioia.aioe.org!not-for-mail
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: getting generic type parameter in method
Date Wed, 21 Oct 2020 21:33:15 -0400
Organization Aioe.org NNTP Server
Lines 32
Message-ID <rmqngq$441$1@gioia.aioe.org> (permalink)
References <7e2c11ae-85bd-42cd-a7a7-27313edb9178n@googlegroups.com>
NNTP-Posting-Host 5Avcpu9drOe6MAssky6/+Q.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.12.1
Content-Language en-US
X-Notice Filtered by postfilter v. 0.9.2
Xref csiph.com comp.lang.java.programmer:39631

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

getting generic type parameter in method Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2020-10-21 04:38 -0700
  Re: getting generic type parameter in method Eric Sosman <esosman@comcast-dot-net.invalid> - 2020-10-21 08:43 -0400
  Re: getting generic type parameter in method Marcel Mueller <news.5.maazl@spamgourmet.org> - 2020-10-21 15:28 +0200
  Re: getting generic type parameter in method Arne Vajhøj <arne@vajhoej.dk> - 2020-10-21 21:33 -0400
    Re: getting generic type parameter in method Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2020-10-22 04:16 -0700
    Re: getting generic type parameter in method Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2020-10-22 04:21 -0700
  Re: getting generic type parameter in method "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2020-10-22 04:16 -0700

csiph-web