• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic class type at runtime  RSS feed

 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am currently writing a generic class (an implementation of Iterator, actually), and have a question about it. When I get the data I want to iterate through, I need the class type; that is, the "T" in Iterator<T>. Is there a way to get it?

I am afraid there is none (apart from providing a class in the constructor, or some other workaround), as the type information is removed at runtime. I just hope that I have forgotten something...

Thanks,
David
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I understand you correctly. You are making an iterator that contain objects of an unknown class, and you want to know that class - correct?

The following code will do that for you

getClass returns the runtime class of an object.

But maybe this wasn't quite what you were looking for...?
[ November 21, 2006: Message edited by: �dne Brunborg ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I get the data I want to iterate through, I need the class type; that is, the "T" in Iterator<T>.
Why do you need the to know the type at all? Perhaps there is a better solution.
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answers.

�dne: that is a bit different than what I need. I am writing the code of the Iterator itself, and not just using it.

Garrett: I need to know the type, because there is a database query in the Iterator, for which I need to know the class of the object.
There is, of course, a different (not better, just different) solution: provide the result set in the constructor.

I was just curious if it can be done at all. I am becoming more and more convinced that there was none, because of the half-hearted generics implementation in java.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Nemeskey:
I need to know the type, because there is a database query in the Iterator, for which I need to know the class of the object.
There is, of course, a different (not better, just different) solution: provide the result set in the constructor.


To have data access in the Iterator - sounds like a bad idea to me.

What you are saying is that you want something like this:



or this



Now, the second one is not such a bad idea - create an adaptor transforming the resultSet to something conforming to the Iterator interface.
[ November 21, 2006: Message edited by: �dne Brunborg ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, the second one is not such a bad idea - create an adaptor transforming the resultSet to something conforming to the Iterator interface.

Even cooler would be to make the wrapper implement the Iterable interface (not too much extra effort) then you can use the foreach shorthand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!