1

如何使用 janino 编译器将简单的 java 源代码从字符串编译为字节数组中的字节码?

到目前为止,我一直在尝试理解此处的 janino 文档(在 SimpleCompiler 下),以及如何将单个 Java 源代码编译为字节码格式的单个类,但我认为我做得不对。

到目前为止,我的幼稚方法如下所示:

    private void testJanino() {
        String cn = "Arne";
        SimpleCompiler sc = new SimpleCompiler();
        try {
            sc.cook("public class Arne{ public float doWork(){return 42.0f;}}");
            InputStream is = sc.getClassLoader().getResourceAsStream(cn);
            if (null != is) {
                int len = is.available();
                byte[] ar = new byte[len];
                System.out.println("SZ:" + len);
                int r = is.read(ar);
                System.out.println("READ:" + r);
            } else {
                System.out.println("NO IS");
            }
        } catch (CompileException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

输出NO IS表明我试图错误地使用它。

那么我应该怎么做呢?

编辑:我一直在不停地前进,我似乎无法得到我想要的东西。UnitCompiler这是使用 janino类的尝试:

  private String mClassName = "Arnulf";
  private String mCode = "public class " + mClassName + "{ public float doWork(){return 42.0f;}}";
  private void testJanino2() {
        try {
            InputStream is = new ByteArrayInputStream(mCode.getBytes(StandardCharsets.UTF_8));
            Scanner s = new Scanner(mClassName, is);
            Parser p = new Parser(s);
            Java.CompilationUnit compilationUnit = p.parseCompilationUnit();
            IClassLoader iClassLoader = new LOLClassLoader(null);
            UnitCompiler uc = new UnitCompiler(compilationUnit, iClassLoader);
            uc.compileUnit(false, false, false);
        } catch (CompileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

一个简单LOLClassLoader的“记录一切,什么都不做”的类扩展IClassLoader.

这最终以这样的空指针结束:

java.lang.NullPointerException
    at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:429)
    at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:393)
    at org.codehaus.janino.UnitCompiler.access$400(UnitCompiler.java:185)
    at org.codehaus.janino.UnitCompiler$2.visitPackageMemberClassDeclaration(UnitCompiler.java:347)
    at org.codehaus.janino.Java$PackageMemberClassDeclaration.accept(Java.java:1139)
    at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:354)
    at org.codehaus.janino.UnitCompiler.compileUnit(UnitCompiler.java:322)
    at com.mypackage.backend.MyServlet.testJanino2(MyServlet.java:60)
    at com.mypackage.backend.MyServlet.doPost(MyServlet.java:208)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:127)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:491)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

此时欢迎任何帮助或提示。

谢谢!

4

1 回答 1

1

那么我应该怎么做呢?

这似乎有效:

private void testJanino() throws Exception {
    SimpleCompiler sc = new SimpleCompiler();
    sc.cook("public class Arne{ public float doWork(){return 42.0f;}}");
    Class<?> arneClass = sc.getClassLoader().loadClass("Arne");
    Object arne = arneClass.newInstance();
    Method doWork = arneClass.getDeclaredMethod("doWork");
    Object result = doWork.invoke(arne, new Object[0]);
    System.out.println(result);
}

如果您真的想动态编译一个类,那么能够在流中获取其字节码,请查看如何动态编译和加载外部 java 类?

于 2015-01-24T01:28:19.013 回答