aspose file tools*
The moose likes Beginning Java and the fly likes get Collection base class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get Collection base class" Watch "get Collection base class" New topic
Author

get Collection base class

Emili Calonge
Ranch Hand

Joined: May 17, 2003
Posts: 84
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

The information about generics is removed from the bytecode when you compile your code. Search for type erasure for more information.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
Despite type erasure, this workaround would be helpful

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Emili Calonge
Ranch Hand

Joined: May 17, 2003
Posts: 84
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
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

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

Joined: May 17, 2003
Posts: 84
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
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

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

Joined: May 17, 2003
Posts: 84
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
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

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

Joined: May 17, 2003
Posts: 84
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
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

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

Joined: May 17, 2003
Posts: 84
Ok, thank you so much for your time and explanations!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get Collection base class