Groovy Groovy study notes four to cooperate with Java

(1) call Java from Groovy
Java is very simple call from Groovy, as long as the JAR in the CLASSPATH, then use standard import statement on the line.
To joda-time, for example, download joda-time, address: https://github.com/JodaOrg/joda-time/releases/download/v2.6/joda-time-2.6-dist.tar.gz
Decompression, joda-time-2.6.jar into Groovy installation directory / lib
Sample code:
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) Calling Groovy from Java
Called from java program needs to Groovy Groovy and its associated JAR CLASSPATH put under this program, because they are dependencies at runtime.
Can directly groovy installation directory /embeddable/groovy-all-2.4.0-rc-1.jar into CLASSPATH java project.
Here are a few to call Groovy code from Java approach
  1. Use Bean Scripting Framework (BSF), i.e. JSR223;
  2. Use GroovyShell;
  3. Use GroovyClassLoader;
  4. Use GroovyScriptEngine;
  5. Using the embedded Groovy console.
1)GroovyShell
When temporary quick call Groovy and evaluate expressions or code similar script, you can use 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 performance and Java ClassLoader like to find the classes and methods you want to call, and then call on the line.
Sample code:
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万+

Guess you like

Origin blog.csdn.net/bsr1983/article/details/84694456