So. I'm having an issue.
I'm building an application that loads class files as extensions during runtime. I'm doing this, becuase I want my application to be able to grow in the future without having to recode and recompile the main source.. So, I plan to make seperate class files that can be loaded at runtime, depending on their existance.
I'm having trouble with loading the classes and calling their methods..
Every extension class will have the following function in common, for example:
public void SetRefs ([ClassName] theobj) {
// Set Object References
myobj = theobj;
}
And my application loads the extensions like so:
private interface ExtensionInf {
public void SetRefs ([ClassName] theobj);
}
.....
try {
String className = classFileName.substring ( 0,
classFileName.lastIndexOf (".")).trim () );
Class loaded = Class.forName (className);
// Try to load class object into Java
ExtensionInf loadedclassobj = (ExtensionInf)
loaded.getConstructor().newInstance();
loadedclassobj.SetRefs(MyObject);
} catch (Exception ex) {
// Invalid Extension
AppDebug (ex);
}
I know this doesn't work. I get a ClassCast exception.
So, I can't use the interface like that.
And I tried using *.getClass().getMethod(...).invoke(...);
...but I got an some other error, like, the object wasn't an instance or whatever.
So my question is, as I've searched google for about a total of 2 hours now, tried several different tactics and have grown tired... How, do I load class files using their name, instance them, and access their methods..?
:-|
Thankie!