是否有java等效的python eval函数? [英] Is there a java equivalent of the python eval function?

查看:742
本文介绍了是否有java等效的python eval函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有java等价的python eval函数?

Is there a java equivalent of the python eval function?

这将是一个函数,它接受一个任意字符串并尝试在当前上下文中执行它。 / p>

This would be a function which takes an arbitrary string and attempts to execute it in the current context.

推荐答案

基于这个Java提示,如果你愿意,可以动态编译Java字符串。使用 com.sun.tools.javac.Main.compile(source)

Based on this Java Tip, compiling a Java string on the fly is indeed possible, if you are willing to use com.sun.tools.javac.Main.compile(source).

<$ c $中的类c> com.sun.tools 当然不是官方Java API的一部分。

Classes in com.sun.tools are of course not part of the official Java API.

在Java 6中有一个编译器API可供使用编程访问编译器。请参阅接口JavaCompiler的文档

In Java 6 there is a Compiler API to provide programmatic access to the compiler. See the documentation for interface JavaCompiler.

任何标准API都不提供直接 eval ,但存在构建您自己的API的工具。如果您尝试执行完全通用的eval,则可能会出现JVM内部JVM问题,因此最好限制您要执行的操作范围。

No direct eval is provided by any standard API, but the tools exist to build one of your own. You might have "JVM inside of JVM" issues if you try and do a completely general eval, so it is best to limit the scope of what you want to do.

另请参阅: Java中是否有eval()函数?,以获得一些好的评论和解释。

Also see: Is there an eval() function in Java? for some good commentary and explanations.

这篇关于是否有java等效的python eval函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆