• Post Reply Bookmark Topic Watch Topic
  • New Topic

Transforming a class instance to fit into the generic bracket  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

suppose I create the following method:


which I should invoke like so: if I want to get the Box holding Strings, or for the Box holding Integers. I have been playing around a bit, casting up and down, but so far, I have not found a solution, so I thought maybe you guys could help me out a bit?

Thank you.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Java™ generics uses erasure, the actual type parameters are not available at run-time. So I wonder whether what you want is possible at all.
 
Ted Scofield
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about some different approaches that would be remotely similar or analogue to this one? Since I've created a project with all the fancy generics and I'm almost done, I find it really difficult to accept the fact that I might be still at ground zero.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with



Now, the thing is, this is only going to work if the type of classType is fully known at compile time -- but as Campbell implies, that's the only time anything ever works with generics.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the OP was talking about something like this:


Note that even though you have to suppress the "unchecked" cast, the construct is actually typesafe since the signature of the addList() method insures that the generic type of the List is the same as the generic type of the Class. If you uncomment line 23 it will fail to compile.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:What's wrong with



Now, the thing is, this is only going to work if the type of classType is fully known at compile time -- but as Campbell implies, that's the only time anything ever works with generics.


With that construct you don't even need the parameter. The compiler can infer it from the assignment:

 
Ted Scofield
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garrett, this is exactly what I was talking about. Now all I gotta do is figure out how to include Strings in the mappings, so that I can retrieve the Box I need. Thank you very much for showing me the example. Also, all other comments were very useful, so thanks to all of you.

If I get stuck again, should I continue the conversation in this thread, or make another one?

Thanks again guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!