• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently learning about reflection and I've done a few lines of code here and there that return things like the package name etc but what I would like is to be able to check if all the classes implement a certain interface and if they do add them to a vector

Could anyone show me a clear straight forward example of this or have any good reference sites? - Thanks!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would reflection accomplish that "if (myObjectInstance instanceof MyInterface)" wouldn't?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean that you want to find all the classes in the classpath that implement a specific interface?

Unfortunately, that is not really easy. Java doesn't have any built-in method to list all the classes in the classpath, or all the classes in a specific package.

The Spring framework, a very popular Java application framework, can help you with this. I once wrote some utility methods using Spring to find all classes in a specific package, which you can find here: Jesper's PackageUtils
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:I'm currently learning about reflection and I've done a few lines of code here and there that return things like the package name etc but what I would like is to be able to check if all the classes implement a certain interface and if they do add them to a vector

Well, there's a method in the Class class called isAssignableFrom(), which is probably worth looking at. I also wouldn't use a Vector (if that's what you meant by "vector"), because it's a legacy class. A List or Set of some kind would probably be better.

It would also be easier if we could see your "few lines of code". Just remember when you post it: DontWriteLongLines.

Winston
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Jesper - Yes precisely what you said,I need to find out which classes use a specific interface but I'm having a hard time finding any clear example of this in java. Unfortunately I'm not allowed to use spring, is there anything else you know of that may help?

@ Ulf - Your example, would that help me obtain what I need? Finding out which classes use a specific interface?

@Winston - thankyou i'll check that out. Yes I meant Vector as I was told I had to return a Vector of strings. My code is just a few little sample lines that do different things like the following:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:My code is just a few little sample lines that do different things like the following:

You could do it that way, but I suspect that isAssignableFrom() from might be a bit less verbose - particularly if you know which interface you want to check for.

If you don't - or you have more than one to check for - then you could possibly use what you have converted to a Set, and then run something like retainAll() on it.

Winston
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston - Thanks for your reply, I know the interface which I need to check for it's called 'plugin' and there is only one just not sure how you do this

Would anyone mind explaining why when I check for the interface being used I get this as a result:



code being used
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:Would anyone mind explaining why when I check for the interface being used I get this as a result:

Because myClass.getInterfaces() return an array; and that's the way toString() works for an array.

if you want to see the contents of the array, use 'Arrays.toString(interfaces)'.

Winston
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class LauncherPlugin implements the interface plugin so shouldn't interfaces have the interface in or am I doing this wrong? - Thank you for all your help Winston much appreciated!

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:The class LauncherPlugin implements the interface plugin so shouldn't interfaces have the interface in or am I doing this wrong?

Without seeing a bit more code (and also the results) it's impossible to say. I presume that you are getting some results now.

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To go back to what Winston said earlier.
Isn't it simpler to test with the instanceof operator?

But when you start looking for things with reflection or instanceof, go back and consider the types in other parts of the program. Can't you simply declare the variable as type Plugin?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had missed a little piece of code out when printing to console, no wonder it never worked! When I debugged I hovered over it and it had the plugin value so I knew the print out statement must of been wrong and surely enough it was!

Is there a way I can now elaborate on this so that all classes are searched to see if they too implement said interface 'plugin'?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:To go back to what Winston said earlier.
Isn't it simpler to test with the instanceof operator?

Not in this case I think, because Alice is dealing with classes, not objects.

@Alice: If that's not the case, and you're just getting the class from a getClass() call, then Campbell's absolutely right.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:Is there a way I can now elaborate on this so that all classes are searched to see if they too implement said interface 'plugin'?

Well, it depends entirely on what you mean by "all classes", but assuming you have a collection of them, you'd just run that code for each one.

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

Why not put all your Classes into a List, get its Stream, filter it for whether it implements Plugin and collect into a new List?

I have just remembered why you can't use a Stream

Put all your Classes into a List. Iterate that List and find whether it implements Plugin.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
@Alice: If that's not the case, and you're just getting the class from a getClass() call, then Campbell's absolutely right.

Winston
I think she is trying to learn reflection. Was getClass() implemented in her version of Java ?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Yeah still using good ol' java 6! :') Thank you for your help though - you both are very insightful and great at explaining stuff, you guys should be doing tutorials of this! :P

I'll try what you suggested Campbell!Thank you!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:you guys should be doing tutorials of this! :P

I might do, except that I hate reflection.

To me, it's the spawn of the Devil; especially in a statically-typed language like Java. I realise that it's needed - though not nearly as much as beginners tend to think - and I'm certainly grateful for things like instanceof; but after 12 years I can honestly still count the number of times I've needed to use it for anything "heavy" on the fingers of one hand.

That said, I've never had to write an IDE.

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is good for writing IDEs and it is good for demonstrating the obscurer parts of the language. But it isn't any good for normal use.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My obscurer link is crappy code you would never want in real life.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell - do you have any idea how to get all the classes out of my package and get them into a list? I've tried a couple different ways from links I've looked at I'd try and show you but I've looked at an awful lot now and can't seem to pinpoint them again,they didn't work for me at least and I'm starting to struggle to find much about it. There seems to be a lot of people saying you cant get them all too then others are saying you can so it's a little confusing as to how to get it to actually work.

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The package is a directory, right?
You can get the properties of this directory by creating a File object to embody it.
The File class has methods to allow you to list all included files. You might do well to attach a filter so you only get files with a particular extension.
I think you get an array, which is easy to pass to the constructor of a List implementation.
It is all in the Java Tutorials, but I think you may have to look in the “legacy” section, even without joking about it. Most of it uses NIO2 which is Java7 stuff. You may be able to find an archived version of the Java6 tutorial.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have to find a Java6 tutorial, or an old book, e.g. 7th edition of Horstmann and Cornell, covering Java6.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell, I'll have a look and see if I can track down the Java 6 stuff and I'll look for the books you mentionned- I still have no clue as to why I have to use Java 6....
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of companies are very conservative about their software and prefer to use stuff over 5 years old on the grounds that new stuff might have bugs in which haven't been found yet. Of course, Oracle no longer seek bugs in Java6. Only in Java7/8.

By the way: you can probably find a Java6 tutorial here.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah can't be too careful when you're a business with stuff like that could cause a lot of problems! Thank you for providing that link - very kind of you! Awaiting 14 minutes to download it now ;P
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As has already been mentioned, there is no easy way to access a list of what classes are on the classpath. Java does not give access to that information, as far as I am aware. The ClassLoaders must know because they have to search the classpath when loading classes, but I don't know a way to access that from within your code.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I did have the classpath - is there a way I could do it then?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Things like java.io.File allow you to explore a directory and get an array of all contained files. Is that any use to you?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have the classpath you can certainly do it, but there would still be a lot of work to do. The classpath gives you a list of directories and jar files. You would need to expand this into a list of fully qualified class names in those directories and inside the jar files.

If this is an exercise given by a teacher it seems a little complicated so I would seek clarification that this is what was intended.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!