如何使用janino编译器将字符串中的简单java源代码编译为字节码中的字节码?
到目前为止,我一直在尝试理解janino文档here(在SimpleCompiler下)和here 如何将单个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
,表示我试图错误地使用它。
那我该怎么做呢?
编辑:我一直在这不停地进行,我似乎无法得到我想要的东西。这是尝试使用janino UnitCompiler
类:
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
的类。
这最终会出现如下的nullpointer:
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)
此时欢迎任何帮助或提示。
谢谢!
答案 0 :(得分: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);
}
如果你真的想动态编译一个类,然后能够在一个流中获取它的字节码,请查看How do you dynamically compile and load external java classes?