Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get Collection base class

 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to do the following:

I have a class instance, and this class has some method that return Collections. I need to know which is the base class of that collection.

So I have:



So then I have an instance of Vehicle, and I need to determine all the types of its methods returning Collections.

I've seen that there's a getComponentType() to get the base class of an Array, but I haven't found anything similar for Collections.

I always could use the name of the method, but I'd prefer if I had a more "clean" way of doing it.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The information about generics is removed from the bytecode when you compile your code. Search for type erasure for more information.
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Despite type erasure, this workaround would be helpful

 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may get really annoying, but can't reflection do the job?

Start with the Class object for Vehicle -- obtain via Vehicle.class. Get all the Method objects with the getDeclaredMethods() call. Iterate through all the Method objects and for each ... Get the return type via getReturnType(). With the return type classes, get all the types of classes and interfaces, with getClasses(). And finally, iterate through those classes until you find something that matches the Collection class type, via get getName().... whew!!!

Henry
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong, that was exactly what I was trying.
So I had:


but now if I add:

I get an empty Array. So maybe I didn't understand what you meant or I did something wrong, but I'm not getting any results.

I just hope I'm doing something wrong, because I need this to work.

Thanks for your time!
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. The getClasses() method never returned null for me. Maybe it has to do with paramerized types -- as I never had to use reflection on generics before. There are some methods specific to types which you may have to use instead.

Oh, can you tell me what happens if you traverse it manually? Using the getSuperclass() method repeatly on the return type?

Henry
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this is what you wanted,but I did:


and I got:

Class: java.lang.String # Superclass: java.lang.Object
Class: java.lang.Integer # Superclass: java.lang.Number
Class: java.lang.Number # Superclass: java.lang.Object
Class: domini.DotacionsTipus # Superclass: java.lang.Object
Class: java.lang.String # Superclass: java.lang.Object
Class: java.lang.Integer # Superclass: java.lang.Number
Class: java.lang.Number # Superclass: java.lang.Object
Class: java.lang.Integer # Superclass: java.lang.Number
Class: java.lang.Number # Superclass: java.lang.Object
Class: java.lang.String # Superclass: java.lang.Object
Class: java.util.Date # Superclass: java.lang.Object
Class: java.util.Date # Superclass: java.lang.Object
Class: java.util.Date # Superclass: java.lang.Object


It doesn't make any sense to me, but maybe you understand what's going on. The Collection base class isn't any of those mentioned there.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you have 10 different functions -- and with return types -- in the array, and none of them return a collection.

Anyway, I just ran your code, using a class that I know return collections (two methods to be exact)...



And I got this...



Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I just modified the example to take a type, and got the exact same result. The new methods look like this now...



Henry
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thing that the difference is that I'm doing all those things from otside the class. From another class. So maybe that's why I keep getting all this nulls.

If I run the superClass part again, but I print the function's name, I can see that the one that returns the Collection, is giving null on aux.getSuperclass() != null, so that's why I didn't get any Collection.

So I'm starting to think I won't be able to get the Collection's base class, or do you have some other idea?

Thanks so much!
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I run the superClass part again, but I print the function's name, I can see that the one that returns the Collection, is giving null on aux.getSuperclass() != null, so that's why I didn't get any Collection.

So I'm starting to think I won't be able to get the Collection's base class, or do you have some other idea?


Another reason why it could be returning null for the super class is because the return type doesn't have a super class. If the return type is a primative or interface, then there won't be a super type.

If you print the type, even if the super is null, what is the type?

Henry
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If befor running the aux.getSuperclass() != null, I print the method's name and it's returning type, I get for the one that returns a Collection, java.util.Collection.

So this looks ok, after getting this I can't get any more information about this Collection.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Emili Calonge:
If befor running the aux.getSuperclass() != null, I print the method's name and it's returning type, I get for the one that returns a Collection, java.util.Collection.

So this looks ok, after getting this I can't get any more information about this Collection.


I completely forgot that Collection is *not* a class -- its an interface. Without an instance, it is not possible to determine the class structure. The method is only defined to return an interface, you actually don't know what instance it will returned -- it can even return a different implementation every time.

To get more information, you will actually need to call the method to get a result, and then introspect it. And even then, it will only be valid for that one call to the method.

Henry
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thank you so much for your time and explanations!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic