Java Generics Type Casting -
lets say...
<t, s extends t> void work(final class<t> type, final s object) {}
or
<t> void work(final class<t> type, final t object) {}
how can pass following parameters work()?
final class<?> type = <not null> // reflection output. final object object = <not null> assert type.isinstance(object); // absolutely guaranteed work(type, type.cast(object)); // compile error; how can this? work(type, object); // compile error; how can this?
you'd need class<t>
work, can that. once have that, can this:
work(type, type.cast(object));
now, since you've got class<?>
can't assign class<t>
, can work around providing additional method:
<t> void checkedwork(final class<t> type, final object object) { work(type, type.cast(object)); }
Comments
Post a Comment