Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39631
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: getting generic type parameter in method |
| Date | 2020-10-21 21:33 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <rmqngq$441$1@gioia.aioe.org> (permalink) |
| References | <7e2c11ae-85bd-42cd-a7a7-27313edb9178n@googlegroups.com> |
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
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