【问题标题】:How to Create Java class/es files dynamically?如何动态创建 Java 类/es 文件?
【发布时间】:2018-11-01 01:15:24
【问题描述】:

我需要一种方法来为 ex 运行 java 方法。 createModule("登录") 并作为输出:

  1. 名为 mod_login 的新文件夹
  2. 在 mod_login 内部从模板创建的 java 类文件

如果模板是

class Name extends Blah implement Blah {

    private createdInt;

    private int getCreatedInt() {
        return createdInt;
    }

}

作为回报,我想获得一个动态创建的类:

 class Login extends Blah implement Blah {

    private loginInt;

    private int getLoginInt() {
        return loginInt;
    }
}

试图研究 groovy 来做这件事,但找不到任何有用的东西。

附:它不应该在运行时发生,它更像是一个助手,只需一个按钮就可以实例化这些模块,而不是输入它们

【问题讨论】:

  • 那么简单地将MyClass.java 创建为普通文件?
  • 那种。从我的角度来看,是的。必须将新目录创建为 Android 项目中的一个包,并将文件创建为那里的可访问类。
  • 在这种情况下查看文件 api - 我真的不明白你的问题

标签: java class dynamic dynamic-class-creation


【解决方案1】:

对您有帮助的工作示例。

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;

public class HelloWorld {

public static void main(String[] args) throws Exception {

    // create an empty source file
    File sourceFile = File.createTempFile("Hello", ".java");
    sourceFile.deleteOnExit();

    // generate the source code, using the source filename as the class name
    String classname = sourceFile.getName().split("\\.")[0];
    String sourceCode = "public class " + classname + "{ public void hello() { System.out.print(\"Hello world\");}}";

    // write the source code into the source file
    FileWriter writer = new FileWriter(sourceFile);
    writer.write(sourceCode);
    writer.close();

    // compile the source file
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    File parentDirectory = sourceFile.getParentFile();
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(parentDirectory));
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile));
    compiler.getTask(null, fileManager, null, null, null, compilationUnits).call();
    fileManager.close();

    // load the compiled class
    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { parentDirectory.toURI().toURL() });
    Class<?> helloClass = classLoader.loadClass(classname);

    // call a method on the loaded class
    Method helloMethod = helloClass.getDeclaredMethod("hello");
    helloMethod.invoke(helloClass.newInstance());
 }
}

【讨论】:

    【解决方案2】:

    您只需要在动态创建文件的方法中定义 2 个变量。

    类名 & 属性名

    现在使用这些创建一个扩展名为 .java 的文件,并按原样从模板中写入文本。对于 className 和 propertyName 生成逻辑,请使用上面的变量。

    如果你想创建多个这样的文件,那么将 className 和 propertyName 放在一个列表中并运行一个 forloop。

    【讨论】:

    • 你的意思是直接创建一个带有 .java 扩展名的空文件并用大量的刺来填充它吗?
    猜你喜欢
    • 2010-10-08
    • 2011-10-30
    • 1970-01-01
    • 2011-04-05
    • 2016-10-24
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多