Groovy学习笔记四 Groovy与Java合作

(1)从Groovy调用Java
从Groovy调用Java很简单,只要把JAR放到CLASSPATH中,然后用标准的import语句就行了。
解压,将joda-time-2.6.jar放到Groovy安装目录/lib下
示例代码:
import org.joda.time.*;
DateTime dateTime=new DateTime();
int month=dateTime.getMonthOfYear();
println month
println dateTime.toString()
输出

1
2015-01-05T19:16:07.107+08:00
  2)从Java调用Groovy
从java程序调用Groovy需要把Groovy及其相关的JAR放到这个程序的CLASSPATH下,因为它们都是运行时的依赖项。
可以直接把groovy安装目录/embeddable/groovy-all-2.4.0-rc-1.jar放到java项目的CLASSPATH中。
下面是几种从Java调用Groovy代码的办法
  1. 使用Bean Scripting Framework(BSF),即JSR223;
  2. 使用GroovyShell;
  3. 使用GroovyClassLoader;
  4. 使用GroovyScriptEngine;
  5. 使用嵌入式Groovy控制台。
1)GroovyShell
在临时性快速调用Groovy并计算表达式或类似于脚本的代码时,可以用GroovyShell。
import groovy.lang.Binding;
import groovy.lang.GroovyShell;

/**
 * Created with IntelliJ IDEA.
 * User: billlee
 * Date: 2015/1/5
 * Time: 19:28
 * To change this template use File | Settings | File Templates.
 */
public class GroovyShellDemo {
    public static void main(String[] args) {
        Binding binding=new Binding();
        binding.setVariable("x",3);
        binding.setVariable("y",0.2);
        GroovyShell groovyShell=new GroovyShell(binding);
        Object value=groovyShell.evaluate("x+y");
        System.out.println("x+y="+value.toString());
        value=groovyShell.evaluate("x-y");
        System.out.println("x-y="+value.toString());
        value=groovyShell.evaluate("x*y");
        System.out.println("x*y="+value.toString());
        value=groovyShell.evaluate("x/y");
        System.out.println("x/y="+value.toString());
    }
}
输出:
x+y=3.2
x-y=2.8
x*y=0.6000000000000001
x/y=15.0
  2)GroovyClassLoader
GroovyClassLoader的表现和Java的ClassLoader很像,找到类和想要调用的方法,然后调用就行了。
示例代码:
GetMaxValueOfList.groovy
/**
 * Created with IntelliJ IDEA.
 * User: billlee
 * Date: 2015/1/5
 * Time: 19:36
 * To change this template use File | Settings | File Templates.
 */
class GetMaxValueOfList {
    def Integer getMaxOfList(List list)
    {
        list.max()
    }
}
  GroovyClassLoaderDemo.java
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

import java.io.File;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: billlee
 * Date: 2015/1/5
 * Time: 19:38
 * To change this template use File | Settings | File Templates.
 */
public class GroovyClassLoaderDemo {
    public static void main(String[] args) {
        GroovyClassLoader loader=new GroovyClassLoader();
        try {
            Class<?> groovyClass=loader.parseClass(new File("GetMaxValueOfList.groovy"));
            GroovyObject groovyObject=(GroovyObject)groovyClass.newInstance();
            ArrayList<Integer> integers=new ArrayList<>();
            integers.add(1);
            integers.add(122);
            integers.add(145);
            integers.add(173);
            Object[] arguments={integers};
            Object maxValue=groovyObject.invokeMethod("getMaxOfList",arguments);
            System.out.println("maxValue="+maxValue.toString());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception e="+e.toString());
        }
    }
}

输出为:
maxValue=173
  3)GroovyScriptEngine
使用 GroovyScriptEngine要指明Groovy代码的URL或所在目录。Groovy脚本引擎会加载那些脚本,并在必要时进行编译,包括其中的依赖脚本。
hello.groovy
def helloStatement="Hello Groovy ,Welcome ${name}"
GroovyScriptEngineDemo.java
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;

/**
 * Created with IntelliJ IDEA.
 * User: billlee
 * Date: 2015/1/5
 * Time: 20:25
 * To change this template use File | Settings | File Templates.
 */
public class GroovyScriptEngineDemo {
    public static void main(String[] args) {
        try {
            String root[]=new String[]{"C:/groovy"};
            GroovyScriptEngine groovyScriptEngine=new GroovyScriptEngine(root);
            Binding binding=new Binding();
            binding.setVariable("name","billlee");
            Object object=groovyScriptEngine.run("hello.groovy",binding);
            System.out.println("output "+object.toString());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception e="+e.toString());
        }
    }
}
输出:
output Hello Groovy ,Welcome billlee
 
发布了250 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/bsr1983/article/details/84694456