Results 21 to 22 of 22
- 06-04-2011, 11:46 AM #21
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Ah, OK. Norm is right, generics is a compile-time thing, erasure removes it from the runtime (mostly), so you need an object to work with.
There is a way to retain generic types for classes, which relys on the need for anonymous classes to retain their generic type information at runtime.
I only browsed through the earlier part of the thread, so this may already have been mentioned - ignore the following if so...
Using this technique, you could subclass a collection class such as ArrayList, to make the generic type available at runtime. See Using Type Tokens To Retrieve Generic Parameters and Super Type Tokens.
It's a bit of a hack though...Last edited by dlorde; 06-04-2011 at 11:52 AM.
- 06-04-2011, 03:28 PM #22
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
Thanks it made me understand what the problem with type erasure is. It makes me a bit sad though, since my work will not be as awesome, and easy to implement for users, as intended but I think I'll create constructors to open up for both solutions:
- Taking the class as a parameter in the constructor
- Or requiring instantiation of my class to be anonymous
So people can do what they find easiest - although I would've liked to be able to have one with an empty constructor since it would be easier for newbs.
Anyway, I'll edit this post with the code if my solution will work. Otherwise I'll probably do another post about my problems.
Similar Threads
-
Class as a Parameter.
By Soxman04 in forum New To JavaReplies: 2Last Post: 05-12-2011, 06:36 PM -
Class<T> in method parameter
By Onra in forum New To JavaReplies: 4Last Post: 03-14-2011, 12:12 AM -
doubt regarding <type parameter>
By subith86 in forum New To JavaReplies: 7Last Post: 03-01-2011, 03:35 PM -
Passing final parameter from a class
By ianyappy in forum New To JavaReplies: 8Last Post: 12-02-2010, 07:06 PM -
passing an enum type as a parameter ??!
By SCS17 in forum New To JavaReplies: 11Last Post: 07-13-2008, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks