I’m trying to write something like framework. I need to search particular folder on my computer and be able to get all class files that are there which implements my interface. While I know how to scan folder and check all class files somehow I can’t get to check whether read class implements my interface.
if (Game.class.isAssignableFrom(classInFolder))
{
//Do sth here
}
That’s one approach I tried but it never hop into if clause – Game is my Interface
public interface Game {
public void startAlgorithm();
}
and classInFolder is Class that I got from my folder. Funny thing if I try to do the same with some library interface – let’s say Serializable it works fine – I tried it. What’s more when I wrote something like that
Type[] type = classInFolder.getGenericInterfaces();
for (int i = 0;i<type.length;++i)
{
System.out.println("interface = " + type[i]);
if (type[i] instanceof Game){
System.out.println("It is");
}
}
I got on my console output – interface = mypackage.Game
But there was not “It is” output. The same thing was with my own annotation on class I tried to use – I can’t get to check whether my class implements my interfaces but it works when those are interfaces from some lib. I deploy my project on tomcat v 7.0 everything is build on Java 7 on Eclipse with the use of Spring.
I would be grateful for any ideas about what could be happening.
Your
Gameclass is probably loaded by tomcat class loader.Your
classInFolderis probably loaded by your own custom class loader.You need to make sure that the 1st loader is the parent of the 2nd loader