private static Importable getRightInstance(String s) throws Exception { Class c = Class.forName(s).asSubclass(Importable.class); Importable i = c.newInstance(); return i; }
which i can also write
private static Importable getRightInstance(String s) throws Exception {
Class<? extends Importable> c = (Class<? extends Importable>)Class.forName(s);
Importable i = c.newInstance();
return i;
}
or
private static Importable getRightInstance(String s) throws Exception {
Class<?> c = Class.forName(s);
Importable i = (Importable)c.newInstance();
return i;
}
where Importable is an interface and s is a string representing an implementing class. Well, in any case it gives the following:
Exception in thread "main" java.lang.IncompatibleClassChangeError: class C1 has
interface Importable as super class
Here is the last snippet of the stack trace:
at java.lang.Class.forName(Class.java:169)
at Importer.getRightImportable(Importer.java:33)
at Importer.importAll(Importer.java:44)
at Test.main(Test.java:16)
Now, class C1 actually implemens Importable and i totally don't understand why it complaints.
Thanks in advance.
IncompatibleClassChangeError means something is wrong with the class file that you're loading. In this case, it sounds like Importable was originally a class when C1 was compiled, and now you've changed it to an interface. Since the JVM cares about the difference between extends SomeClass and implements SomeInterface, you'll need to recompile C1 against the current Importable interface (and probably also change its code from extends to implements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With