了解知识


embedding java

package groovytest
def run(foo) {
println 'Hello World!'
x = 123
foo *= 10
println foo
}
run (foo)

=======================================

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package groovytest;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import java.io.File;

public class EmbedGroovy {

private Binding binding = new Binding();

public Object getProperty(String name) {
return binding.getProperty(name);
}

public void setParameters(String[] paramNames, Object[] paramValues) {
int len = paramNames.length;
if (len != paramValues.length) {
System.out.println("parameters not match!");
}
for (int i = 0; i < len; i++) {
binding.setProperty(paramNames[i], paramValues[i]);
}
}

public Object runScript(String scriptName) {
GroovyShell shell = new GroovyShell(binding);
try {
return shell.evaluate(new File(scriptName));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static void main(String[] args) {
EmbedGroovy embedGroovy = new EmbedGroovy();
String[] paramNames = {"foo"};
Object[] paramValues = {new Integer(100)};
embedGroovy.setParameters(paramNames, paramValues);
Object result = embedGroovy.runScript("src/groovytest/script.groovy");
System.out.println(result);
System.out.println(embedGroovy.getProperty("foo"));
//System.out.println(embedGroovy.getProperty("x"));
}
}
============================================================

init:
Deleting: C:\my\java\GroovyTest\build\built-jar.properties
deps-jar:
Updating property file: C:\my\java\GroovyTest\build\built-jar.properties
Compiling 1 source file to C:\my\java\GroovyTest\build\classes
警告: 来自注释处理程序 'org.netbeans.modules.openide.modules.PatchedPublicProcessor' 的受支持 source 版本 'RELEASE_6' 低于 -source '1.7'
警告: 来自注释处理程序 'org.netbeans.modules.openide.util.ServiceProviderProcessor' 的受支持 source 版本 'RELEASE_6' 低于 -source '1.7'
警告: 来自注释处理程序 'org.netbeans.modules.openide.util.NbBundleProcessor' 的受支持 source 版本 'RELEASE_6' 低于 -source '1.7'
警告: 来自注释处理程序 'org.netbeans.modules.openide.util.URLStreamHandlerRegistrationProcessor' 的受支持 source 版本 'RELEASE_6' 低于 -source '1.7'
4 个警告
compile-single:
run-single:
Hello World!
1000
null
100
成功生成(总时间:1 秒)

=====================================================================

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package groovytest;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import java.io.File;

/**
*
* @author hxfcalf
*/
public class DynamicGroovy {

private GroovyObject groovyObject;

public Object getProperty(String name) {
return groovyObject.getProperty(name);
}

public Object invokeScriptMethod(String scriptName, String methodName, Object[] args) {
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
try {
Class groovyClass = loader.parseClass(new File(scriptName));
groovyObject = (GroovyObject) groovyClass.newInstance();
return groovyObject.invokeMethod(methodName, args);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static void main(String[] args) {
DynamicGroovy dynamicGroovy = new DynamicGroovy();
Object[] params = {new Integer(2)};
Object result = dynamicGroovy.invokeScriptMethod("src/groovytest/script.groovy", "run", params);
System.out.println(result);
System.out.println(dynamicGroovy.getProperty("x"));
}
}
===================================================================================
init:
Deleting: C:\my\java\GroovyTest\build\built-jar.properties
deps-jar:
Updating property file: C:\my\java\GroovyTest\build\built-jar.properties
Compiling 1 source file to C:\my\java\GroovyTest\build\classes
警告: 来自注释处理程序 'org.netbeans.modules.openide.modules.PatchedPublicProcessor' 的受支持 source 版本 'RELEASE_6' 低于 -source '1.7'
警告: 来自注释处理程序 'org.netbeans.modules.openide.util.ServiceProviderProcessor' 的受支持 source 版本 'RELEASE_6' 低于 -source '1.7'
警告: 来自注释处理程序 'org.netbeans.modules.openide.util.NbBundleProcessor' 的受支持 source 版本 'RELEASE_6' 低于 -source '1.7'
警告: 来自注释处理程序 'org.netbeans.modules.openide.util.URLStreamHandlerRegistrationProcessor' 的受支持 source 版本 'RELEASE_6' 低于 -source '1.7'
4 个警告
compile-single:
run-single:
Hello World!
20
null
123
成功生成(总时间:1 秒) 

标签: java Groovy
扩展知识