java - Reflected Method calling constructor multiple times -


i'm working on launching javafx gui app using custom classloader , reflection, seem inadvertently invoking application constructor multiple times, throws exception (using sort of singleton pattern guarantee nobody tries re-initialize main app class).

the myapp constructor's illegalstateexception thrown when call startupmethod.invoke(instance); launcher class.

public class myapp extends application {      private static myapp instance = null;      private static boolean started = false;      public myapp() {         if (instance != null) {             throw new illegalstateexception("myapp initialized");         }         instance = this;     }      public static myapp getinstance() {         return instance;     }      public void startup() {         synchronized (lock) {             if (started == true) {                 throw new illegalstateexception("myapp running");             }         }        // things start gui, etc...        //  ... ... ...        started = true;        launch(); // static method part of javafx platform     }      @override     public void start(final stage stage) {         // stuff display gui     }  } 

.

public class launcher {      public static void main(string[] args) {         new launcher().start();     }      private void start() {         // create custom classloader ...         // ... ... ...         class<?> myappclass = myloader.loadclass("com.something.myapp");         // calls myapp constructor , sets "instance" static var "this"         object instance = myappclass.newinstance();          method startupmethod = myappclass.getmethod("startup");         // seems call myapp constructor again!, exception thrown...         startupmethod.invoke(instance);     }  } 

if comment out exception in myapp constructor, app starts fine, means i've still invoked constructor twice , i'm unsure why. need able guard against people invoking constructor more once.

edit: research, it's appearing call static method application.launch() attempting build new instance of myapp on javafx application thread...

update:

fixed adding static method myapp invokes application.launch() method. launcher class load myapp class , call static method like:

public class myapp extends application {     public myapp() {         if (instance != null) {             throw new illegalstateexception("already initialized");         }         instance = this;     }     // other stuff     public static void startup() {         launch();     } } 

.

public class launcher {     // other stuff     class<?> myappclass = myloader.loadclass("com.something.myapp");     method startupmethod = myappclass.getmethod("startup");     startupmethod.invoke(null, null); } 

application.launch() creates instance of class on called (necessarily subclass of application). that's second instance coming from.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -