使用Janino计算Java表达式

官方介绍

Janino是一个超小型,超快的Java编译器。

Janino不仅可以将一组源文件编译为一组类文件(如JAVAC),还可以在内存中编译Java表达式,块,类主体或源文件,加载字节码并直接在同一JVM中执行。

JANINO与Apache Commons JCI(“Java编译器接口”)和JBoss Rules / Drools集成在一起。

JANINO还可用于 静态代码分析 或 代码修改。

代码示例

修改了下官方的示例代码

import org.codehaus.commons.compiler.CompilerFactoryFactory;
import org.codehaus.commons.compiler.IExpressionEvaluator;
import org.springframework.util.StopWatch;

/**
 * Sample application which demonstrates how to use the {@link IExpressionEvaluator} class.
 */
public class ShippingCost {

    public static void main(String[] args) throws Exception {
        //StopWatch 计算方法耗时的优雅方式,这里使用的是spring的,guava和apach-commons都有类似的功能
        StopWatch sw = new StopWatch();

        // Convert command line argument to call argument "total".
        Object[] arguments = {new Double(120), new Integer(10)};

        sw.start("实例化表达式解析器");
        // Create "ExpressionEvaluator" object.
        IExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
        sw.stop();
        ee.setExpressionType(boolean.class);
        ee.setParameters(new String[]{"total", "count"}, new Class[]{double.class, int.class});
        sw.start("设置表达式");
        ee.cook("count == 10 || total >= 100.0 && count > 8");
        sw.stop();

        sw.start("计算表达式");
        // Evaluate expression with actual parameter values.
        Object res = ee.evaluate(arguments);
        sw.stop();

        // Print expression result.
        System.out.println("Result = " + res);

        System.out.println(sw.prettyPrint());
        System.out.println(sw.shortSummary());
    }
}

PS

logback Conditional processing 就使用了Janino库来提升性能
https://logback.qos.ch/manual/configuration.html#conditional

还有很多用处,详见官网http://janino-compiler.github.io/janino/GitHub地址https://github.com/janino-compiler/janino

Janino是一个开源的Java编译器,它可以在运行时动态编译和执行Java代码。Janino提供了一个简单的API,使我们可以直接在Java应用程序中动态编译Java代码,并在运行时执行这些代码。 使用Janino执行Java代码非常简单。首先,我们需要引入Janino的依赖库,在项目的构建配置文件中添加如下配置: ``` <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> <version>3.1.0</version> </dependency> ``` 然后,我们可以使用Janino提供的`SimpleCompiler`类来编译和执行Java代码。下面是一个示例代码: ```java import org.codehaus.janino.SimpleCompiler; public class JaninoExample { public static void main(String[] args) throws Exception { String javaCode = "public class HelloWorld { public void sayHello() { System.out.println(\"Hello, World!\"); } }"; SimpleCompiler compiler = new SimpleCompiler(); compiler.cook(javaCode); Class<?> helloWorldClass = compiler.getClassLoader().loadClass("HelloWorld"); Object helloWorldInstance = helloWorldClass.getDeclaredConstructor().newInstance(); helloWorldClass.getMethod("sayHello").invoke(helloWorldInstance); } } ``` 在示例代码中,我们定义了一个叫做`HelloWorld`的Java类,其中包含了一个叫做`sayHello()`的方法。通过`SimpleCompiler`类的`cook()`方法编译Java代码,并使用`getClassLoader()`方法获取编译结果的`ClassLoader`。然后,可以使用`loadClass()`方法加载编译后的类,并使用`getDeclaredConstructor().newInstance()`方法创建类的实例。最后,可以使用`getMethod().invoke()`方法调用实例的方法。 通过以上的步骤,我们就可以在Java应用程序中使用Janino执行动态编译的Java代码了。需要注意的是,Janino只能执行基于Java语法的代码,不支持使用外部库或特殊的语言特性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值