如何从包含多个java源文件的包中动态执行多个类的函数?

时间:2014-05-12 16:20:25

标签: java android java-ee selenium-webdriver

我正在尝试创建一个将长时间运行的Java应用程序。在这个java应用程序中,将有一个包含多个java文件的包。类的名称将与java文件的名称相同。并且所有类将包含2-3个函数(每个类的函数的固定名称),但它们的定义将从一个java文件更改为另一个。

那我该怎么做呢?我已经拥有的一个解决方案是使用java反射。但我不想使用java反射来实现它。在我的项目中,我对此有一个约束。

请帮助我。提供您宝贵的意见。

1 个答案:

答案 0 :(得分:0)

我不确定你需要什么动态水平, 但你可以使用接口,其他类实现它

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
}
#####编辑/更新------------------------------------------ ------

这就是我所说的起动器 - 目标溶胶, 目标是您要运行的50或100级 而starter是将实例化所有这些并调用

中的方法的类

这里的想法是,启动类是自动生成的 自动编译,然后作为普通类进行实例化和使用。

查找样本完整溶胶:

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()