0

I have a class which is implementing an interface with generics.

class MainClass {
    interface MyInterface <T extends MyInterface.A> {
        static class A {
        }
    }
}

//I have another class which implements this interface:
class ImplementingClass implements MyInterface<A> {
}

//I am loading MyInterface class from a jar in a different place: 
static loadJar(){
    String dexPath = new ContextWrapper(context).getCacheDir().getAbsolutePath();
    DexClassLoader classLoader = new DexClassLoader(JAR_FILE, dexPath, null, ClassLoader.getSystemClassLoader());
    Class c = classLoader.loadClass("ImplementingClass");
    MyInterface nvQS = (MyInterface) c.newInstance();
}

I am getting a class cast exception. I am not sure why is that? The class is implementing the interface. I tried it with the generics as well but I still get the exception.

Please let me know in case I am not clear. Thanks.

Including the Exception : java.lang.ClassCastException: ImplementingClass cannot be cast to MainClass$MyInterface

Hey, the interface is build as a java static library in android. The jar and the jar loader will be compiled against separate copies right? Is that a problem? Do you think that is why I might be getting a class cast?

user2248720
  • 11
  • 1
  • 6
  • What is the exception you are getting? You haven't provided the stack trace. Usually the error message in the exception tells you fairly specifically what the problem is. – azurefrog Apr 23 '15 at 23:26
  • Do you use IDE? I advise you to use Debug to see the object "c". See it's class. – MageXellos Apr 23 '15 at 23:29
  • Yep I did that. "Name of the loaded class : ImplementingClass" – user2248720 Apr 23 '15 at 23:30
  • http://stackoverflow.com/questions/10569952/how-to-cast-two-instance-of-the-same-loaded-different-classloader –  Apr 23 '15 at 23:47
  • If you have MyInterface in an external jar file how could you possibly be able to compile MyInterface nvQS = (MyInterface) c.newInstance(); ? Which means that you have one MyInterface in your project and one MyInterface in the external jar file thus there are two different classes even if they have exactly the same signature. – hevi Apr 23 '15 at 23:56
  • No no. MyInterface is a library that I import in both the places ie the place where I load the jar and the place where it is implemented in the ImplementingClass. – user2248720 Apr 24 '15 at 00:00
  • @WillieWheeler : I saw this answer. They are doing somehing similar. http://stackoverflow.com/questions/3022454/how-to-load-a-java-class-dynamically-on-android-dalvik – user2248720 Apr 24 '15 at 00:31

2 Answers2

0

You are creating a new interface MainClass.MyInterface which is not the same as MyInterface. If you implement MainClass.MyInterface and also cast to MainClass.MyInterface then it should work. It probably could have been avoided by choosing better names.

Not to be confused with the packages, they can be skipped if the MainClass is included.

maraca
  • 8,468
  • 3
  • 23
  • 45
  • While importing I include the class name. So while using I can directly use the interface name right? Maybe that is not the issue? Also apologies for the names. I thought I was making it simple. I will retain original names next time. Thanks. – user2248720 Apr 24 '15 at 00:29
  • @user2248720 You can't do that if you have the interface inside another class as far as I know. Then you need to write the class containing the interface in front. The packages can be skipped. Maybe it depends on the java version. – maraca Apr 24 '15 at 00:31
  • Then that should throw me a compiler error too right? It compiles fine. I am using Java version 7 – user2248720 Apr 24 '15 at 02:03
  • @user2248720 no it won't if both are existing. Just try it. – maraca Apr 24 '15 at 07:04
  • Hey, I tried that. I am still getting the error. :( – user2248720 Apr 24 '15 at 16:32
0

so the problem was that the interface was a static library. So since the jar and the code where it was loaded were compiled separately, they are not considered as the same class by java. I made it a shared library and it works fine, no exception. :-)

user2248720
  • 11
  • 1
  • 6