Einzelnen Beitrag anzeigen
Alt 15.05.2007, 20:39   #6
jak
Inventar
 
Registriert seit: 13.06.2001
Beiträge: 1.830


Standard

Doch, sollte gehen:
Code:
Class c;
String cn = example.classname;
Constructor con = null;
Class[] constructorParameterClasses = {Class1.class, Class2.class}; //the classes of the paramters for the constructor
Object[] constructorParameters = {new Class1, new Class2} //the parameters for the constructor

try {
  c = Class.forName(cn, false, this.getClass().getClassLoader());
} catch (ClassNotFoundException ex) {
  //Class not found, do something
}

try {
  con = c.getConstructor(constructorParameterClasses);
} catch (SecurityException se) {
  //Security Manager denied access
} catch (NoSuchMethodException nsme) {   
  //No such Method. Perhaps the constructor is not public?
}
Object o1 = null;

try {
  o1 = c1.newInstance(constructorParameters);
} catch (InvocationTargetException ite) {
  //if the underlying constructor throws an exception.
} catch (IllegalArgumentException iare) { //wrong number of params?
  //if the number of actual and formal parameters differ;
  //if an unwrapping conversion for primitive arguments fails;
  // or if, after possible unwrapping, a parameter value cannot be converted
  // to the corresponding formal parameter type by a method invocation
  // conversion.
} catch (IllegalAccessException iace) {
  //constructor not Public
} catch (InstantiationException ie) {
  //Cannot execute constructor make sure it is not abstract;
} catch (ExceptionInInitializerError eiie) {
  //if the initialization provoked by this method fails.
}

try {
  SomeOtherClass soc = (SomeOtherClass) o1;
} catch (ClassCastException cce){
  //cast to new Class failed.
}
In deinem Fall müsste es so etwas wie: ServerPlugin server_plugin = (ServerPlugin) plugin;
sein.

Wenn der Cast nicht funktioniert, liegt es vermutlich daran, wie die Hierarchie (Vererbung) deiner Klassen aufgebaut ist.

jak
____________________________________
Join the DNRC | Godwin\'s Law (thx@stona)
Documentation is like sex: If it\'s good, it\'s very, very good. If it\'s bad, it\'s better than nothing.
\"In theory, theory and practice are the same. In practice, they are not\" (Lawrence Berra)
jak ist offline   Mit Zitat antworten