java - Passing class name as a parameter -


i have following function

public void loadwindowandsenddatatest(string path, string appname, connectiondata connectiondata) {     try {         stage subwindow = new stage();         subwindow.initmodality(modality.application_modal);         fxmlloader loader = new fxmlloader();         parent parent = loader.load(getclass().getresource(path).openstream());         exitcontroller exitcontroller = (exitcontroller) loader.getcontroller();         exitcontroller.getconnectiondata(connectiondata);          scene scene = new scene(parent);          subwindow.setscene(scene);         subwindow.settitle(appname);         subwindow.show();     } catch(ioexception e) {         e.printstacktrace();     } 

and i'd achive have more general function can pass class name (in case exitcontroller), this:

public void loadwindowandsenddatatest(string path, string appname, connectiondata connectiondata, string classname) {     try {         stage subwindow = new stage();         subwindow.initmodality(modality.application_modal);         fxmlloader loader = new fxmlloader();         parent parent = loader.load(getclass().getresource(path).openstream());         /* sth classname obtain usedclasscontroller class */         usedclasscontroller usedclasscontroller = (usedclasscontroller) loader.getcontroller();         usedclasscontroler.getconnectiondata(connectiondata);          scene scene = new scene(parent);          subwindow.setscene(scene);         subwindow.settitle(appname);         subwindow.show();     } catch(ioexception e) {         e.printstacktrace();     } 

the assumption class i'm passing function have getconnectiondata() fuction implemented. there way this?

if you're assuming controller class has getconnectiondata(connectiondata) method implemented, use reflection invoke method:

public void loadwindowandsenddatatest(string path, string appname, connectiondata connectiondata) {     try {         stage subwindow = new stage();         subwindow.initmodality(modality.application_modal);         fxmlloader loader = new fxmlloader();         parent parent = loader.load(getclass().getresource(path).openstream());         object controller = loader.getcontroller();         method getconnectiondatamethod =              controller.getclass().getmethod("getconnectiondata", connectiondata.class);         getconnectiondatamethod.invoke(controller, connectiondata);          scene scene = new scene(parent);          subwindow.setscene(scene);         subwindow.settitle(appname);         subwindow.show();     } catch(exception e) {         e.printstacktrace();     } } 

this isn't particularly robust or elegant approach. perhaps better define interface getconnectiondata method:

public interface connectiondataprovider {      public void getconnectiondata(connectiondata data) ;  } 

and have controllers implement method:

public class exitcontroller implements connectiondataprovider {      public void getconnectiondata(connectiondata data) {         // ...     }      // existing code ... } 

then can assume controller class implementing method:

public void loadwindowandsenddatatest(string path, string appname, connectiondata connectiondata) {     try {         stage subwindow = new stage();         subwindow.initmodality(modality.application_modal);         fxmlloader loader = new fxmlloader();         parent parent = loader.load(getclass().getresource(path).openstream());         connectiondataprovider controller = loader.getcontroller();         controller.getconnectiondata(connectiondata);          scene scene = new scene(parent);          subwindow.setscene(scene);         subwindow.settitle(appname);         subwindow.show();     } catch(exception e) {         e.printstacktrace();     } } 

note neither of these approaches requires pass in type (class) of controller. if needed reason, following (using interface approach):

public <t extends connectiondataprovider> void loadwindowandsenddatatest(         string path, string appname,          connectiondata connectiondata, class<t> controllertype) {      try {         stage subwindow = new stage();         subwindow.initmodality(modality.application_modal);         fxmlloader loader = new fxmlloader();         parent parent = loader.load(getclass().getresource(path).openstream());         t controller = loader.getcontroller();          // if wanted cast explicitly here, do:         // t controller = controllertype.cast(loader.getcontroller());          controller.getconnectiondata(connectiondata);          // controllertype if need....          scene scene = new scene(parent);          subwindow.setscene(scene);         subwindow.settitle(appname);         subwindow.show();     } catch(exception e) {         e.printstacktrace();     } } 

and invoke with

loadwindowandsenddatatest("/path/to/fxml", "application name",      connectiondata, exitcontroller.class); 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -