java - What happens to type if I derive non-generic class from a generic class? -
let say, have usual pojo called person
. now, instance of arraylist<person>
have type arraylist
@ runtime due type erasure. if instance undergoes serialization/deserialization through library gson, desirialized object not arraylist<person>
unless, of course, provide type token.
now, here question. happens if create class (say persons
) follows:
public class persons extends arraylist<person> { }
will type information preserved here? deserialization mechanism able infer type indeed arraylist<person>
, not raw type arraylist
?
will type information preserved here?
yes; example, this:
final parameterizedtype supertypeofpersons = (parameterizedtype) persons.class.getgenericsuperclass(); system.out.println(supertypeofpersons.getactualtypearguments()[0].getsimplename());
will print person
.
is deserialization mechanism able infer type indeed
arraylist<person>
, not raw typearraylist
?
it potentially could so, doesn't mean does so; it's not trivial thing do. if there specific set of serialization frameworks need support, should test each of them.
Comments
Post a Comment