Path: csiph.com!news.mixmin.net!aioe.org!.POSTED.5Avcpu9drOe6MAssky6/+Q.user.gioia.aioe.org!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= 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: 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 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 fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class 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, List => 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