Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39628 > unrolled thread
| Started by | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| First post | 2020-10-21 04:38 -0700 |
| Last post | 2020-10-22 04:16 -0700 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| Date | 2020-10-21 04:38 -0700 |
| Subject | getting generic type parameter in method |
| Message-ID | <7e2c11ae-85bd-42cd-a7a7-27313edb9178n@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2020-10-21 08:43 -0400 |
| Message-ID | <rmpact$irl$1@dont-email.me> |
| In reply to | #39628 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Marcel Mueller <news.5.maazl@spamgourmet.org> |
|---|---|
| Date | 2020-10-21 15:28 +0200 |
| Message-ID | <rmpd1i$6r1$1@gwaiyur.mb-net.net> |
| In reply to | #39628 |
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
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2020-10-21 21:33 -0400 |
| Message-ID | <rmqngq$441$1@gioia.aioe.org> |
| In reply to | #39628 |
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
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| Date | 2020-10-22 04:16 -0700 |
| Message-ID | <57063d5e-1ccc-4a19-8d25-bb97a0e7286en@googlegroups.com> |
| In reply to | #39631 |
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
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| Date | 2020-10-22 04:21 -0700 |
| Message-ID | <dd8bd1d8-f1fb-4a4e-a345-b38cd0606fccn@googlegroups.com> |
| In reply to | #39631 |
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
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2020-10-22 04:16 -0700 |
| Message-ID | <157fe891-725a-423f-9edd-b7cbae9b20d8n@googlegroups.com> |
| In reply to | #39628 |
> // 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?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web