简体   繁体   中英

How to execute functions of multiple classes dynamically from a package which contains multiple java source files?

I am trying to create a java application that will be running for long periods of time. In this java application there will be a package where multiple number of java files will be there. The name of the classes will be same to the name of the java files. And all the classes will contain 2-3 functions(fixed name of functions for each class) but their definition will be changed from one java file to another.

So how can I do this? One solution which I have already have is using java reflection. But I don't want to implement it using java reflection. I do have a constraint regarding this,in my project.

Kindly help me out. Provide your valuable comment.

i am not sure what dynamic level you need, but you can use interface, and other classes implements it

Interface MainClass{
    public void method1(int param1);
    public void method2(String param1);
    public void method3();
}

Class Sub1 implements MainClass{

    public void method1(int param1){
    //blah blah blah
    }
    public void method2(String param1){
    //blah blah blah
    }

    public void method3(){
    //blah blah blah
    }

}

Class Sub2 implements MainClass{

    public void method1(int param1){
    //blah blah blah
    }
    public void method2(String param1){
    //blah blah blah
    }

    public void method3(){
    //blah blah blah
    }
}

Class SubN implements MainClass{

    public void method1(int param1){
    //blah blah blah
    }
    public void method2(String param1){
    //blah blah blah
    }

    public void method3(){
    //blah blah blah
    }
}

Class Starter{

    private Sub1 s1 = new Sub1();
    private Sub2 s2 = new Sub1();
    private Sub3 sn = new Sub1();


    s1.method1(12);
    s2.method2("OK");
    //blah blah blah
}
#####EDIT / Update ------------------------------------------------

this is what i mean by starter-target sol, the target is the 50 or 100 class you want to run and the starter is the class that will instantiate all of them and call the methods inside

the idea here, is that, the starter class is auto generated auto compiled, and then as a normal class, instantiated and used.

find sample full sol:

 class Generator{ public boolean generateStarter(String classesPath, String writePath){ File classes = new File(classesPath); String allClasses[] = classes.list(); StringBuilder str = new StringBuilder(); str.append("Class Starter {\\n"); str.append("public void go(){\\n"); String tmpVarName = ""; for(int a=0;a<allClasses.length;a++){ //XXX var1 = new XXX(); tmpVarName = generateVariableName(); str.append(getClassName(allClasses[a]) + " " + tmpVarName + " = new " + getClassName(allClasses[a]) + "();\\n"); str.append(tmpVarName + ".method1();\\n");//as method names are known and fixed... str.append(tmpVarName + ".method2();\\n");//as method names are known and fixed... str.append(tmpVarName + ".methodN();\\n\\n");//as method names are known and fixed... }//for loop str.append("}//go()\\n"); str.append("}//CLASS Starter"); //save the file to be compiled. writeFile(writePath,str.toString()); return true; }//generateStarter() private String getClassName(String fileName){ return fileName.split(".")[0]; } private String generateVariableName(){ //return a random variable name ex, hja123; //or you can pass class name and return className.toLowercase(); } private void writeFile(String path, String data){ //writes a text file, with data inside... } //output of class generator should be something like this /* Class Starter{ private Sub1 s1 = new Sub1(); private Sub2 s2 = new Sub1(); private Sub3 sn = new Sub1(); s1.method1(); s2.method2(); //blah blah blah } */ }//CLASS Generator class Compiler{ public boolean compile(String javaClassPath){ getRuntime.exec(new String[]{"c:\\\\java\\\\bin\\\\javac.exe",javaClassPath}); return true; } }//CLASS Compiler class MainStarter{ public static void main(String args[]){ //c:\\\\projx\\\\com\\\\libs\\\\ is the path of the 100 or 200 classes (target classes) you want to run. //c:\\\\projx\\\\Starter.java is the path for the class that will run the 100 class (target classes) [auto generated] Generator generator = new Generator("c:\\\\projx\\\\com\\\\libs\\\\","c:\\\\projx\\\\Starter.java"); if(generator.generateStarter()){ Compiler compiler = new Compiler(); if(compiler.compile("c:\\\\projx\\\\Starter.java")){ //the class is no exist and complied, so you can instantiate it and call method go() Starter starter = new Starter(); starter.go(); }else{//compiled OK System.out.println("compile failed!"); } }else{//generated OK System.out.println("generate failed!"); } }//main() }//MainStarter() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM