是否可以仅在内存中以编程方式编译 java 源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7989135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:34:39  来源:igfitidea点击:

Is it possible to programmatically compile java source code in memory only?

javacode-generationjava-compiler-api

提问by Sergio

I have found many references explaining how to programmatically compile a Java class using the JavaCompilerclass:

我找到了许多解释如何使用JavaCompiler该类以编程方式编译 Java 类的参考资料:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null, "a_file_name");

However, I would like to know if there is an open source library that let me compile source code generated programmatically (therefore without a src file being involved) and generate some byte code in an output stream (without generating a class file in the file system).

但是,我想知道是否有一个开源库可以让我编译以编程方式生成的源代码(因此不涉及 src 文件)并在输出流中生成一些字节码(不生成文件系统中的类文件) )。

For example, I am looking for being able to write something like this:

例如,我正在寻找能够写这样的东西:

InputStream input = generateSourceCode();
OutputStream output = getByteCode(input);
doCoolStuffWithByteCode(output);

Thanks for any help.

谢谢你的帮助。

回答by James Black

To start, look at the JavaCompiler API. Basically:

首先,查看JavaCompiler API。基本上:

  1. Create the Java class in a string.
  2. Put the string into class that extends SimpleJavaFileObject.
  3. Compile using a JavaCompilerinstance.
  1. 在字符串中创建 Java 类。
  2. 将字符串放入扩展SimpleJavaFileObject 的类中。
  3. 使用JavaCompiler实例编译。

Finally, call the methods the new class.

最后,调用新类的方法。



Here is an examplethat works with JDK6+:

这是一个适用于 JDK6+的示例

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.Arrays;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject.Kind;

public class CompileSourceInMemory {
  public static void main(String args[]) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

    StringWriter writer = new StringWriter();
    PrintWriter out = new PrintWriter(writer);
    out.println("public class HelloWorld {");
    out.println("  public static void main(String args[]) {");
    out.println("    System.out.println(\"This is in another java file\");");    
    out.println("  }");
    out.println("}");
    out.close();
    JavaFileObject file = new JavaSourceFromString("HelloWorld", writer.toString());

    Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
    CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);

    boolean success = task.call();
    for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
      System.out.println(diagnostic.getCode());
      System.out.println(diagnostic.getKind());
      System.out.println(diagnostic.getPosition());
      System.out.println(diagnostic.getStartPosition());
      System.out.println(diagnostic.getEndPosition());
      System.out.println(diagnostic.getSource());
      System.out.println(diagnostic.getMessage(null));

    }
    System.out.println("Success: " + success);

    if (success) {
      try {
        Class.forName("HelloWorld").getDeclaredMethod("main", new Class[] { String[].class })
            .invoke(null, new Object[] { null });
      } catch (ClassNotFoundException e) {
        System.err.println("Class not found: " + e);
      } catch (NoSuchMethodException e) {
        System.err.println("No such method: " + e);
      } catch (IllegalAccessException e) {
        System.err.println("Illegal access: " + e);
      } catch (InvocationTargetException e) {
        System.err.println("Invocation target: " + e);
      }
    }
  }
}

class JavaSourceFromString extends SimpleJavaFileObject {
  final String code;

  JavaSourceFromString(String name, String code) {
    super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension),Kind.SOURCE);
    this.code = code;
  }

  @Override
  public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    return code;
  }
}

回答by Brian Roach

JavaDocs are your friend:

JavaDocs 是你的朋友:

http://download.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html

http://download.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html

Look at the last section that refers to the SimpleJavaFileObject; it shows you how to use it in conjunction with code that is stored in a String

看看最后一节提到的SimpleJavaFileObject; 它向您展示了如何将它与存储在String

回答by juancn

We gave a talk about this use case in JavaOne 2016 (the question is kind of old, but there seems to be some interest still).

我们在 JavaOne 2016 中讨论了这个用例(这个问题有点老,但似乎仍有一些兴趣)。

There is a repositorywith examples of practical code generation using javac in-memory.

有一个存储库,其中包含使用内存中的 javac 生成实际代码的示例。

Specifically look at SimpleJavaCompilerfor an example on how to do this in memory that deals with thread safety (we use it in the context of a server) for a single class. It could easily be adapted for a multi-class scenario.

具体查看SimpleJavaCompiler以获取有关如何在处理单个类的线程安全(我们在服务器的上下文中使用它)的内存中执行此操作的示例。它可以很容易地适应多类场景。

There are also classes to deal with class loading and code generation (scoping of variables, generating unique names, name shadowing, etc.).

还有用于处理类加载和代码生成(变量范围、生成唯一名称、名称阴影等)的类。