Run the Groovy code in Java (original)

xiaoxiao2021-03-06  60

Groovy is designed to be lightweight and easy to embed to the Java application system.

1, Groovy script sample

DEF RUN (FOO) {

Println 'Hello World!'

X = 123

FOO * 10

}

Run foo

l Groovy script sample contains the Run () function, and then calls Run (), return results.

l When calling Run (), the parameter passed is the Foo property (when running the script, the attribute value is provided)

2, run the Groovy script using Groovyshell

Import groovy.lang.binding;

Import groovy.lang.groovyshell;

Import java.io.file;

Public class embedgroovy {

PRIVATE 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

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 (2)};

EmbedGroovy.SetParameters (paramnames, paramvalues);

Object result = EmbedGroovy.Runscript ("src / foo.groovy");

System.out.println (Result);

System.out.println (EmbedGroovy.getProperty ("foo");

System.out.println (EmbedGroovy.getProperty ("x");

}

}

l GroaVyshell's Evaluate () method runs the Groovy script of the specified file and returns the result (if any)

l You can use the binding setProperty () method to set the required properties value before the Groovy script runs, and provide the binding object when you create a Groovyshell object.

Like, you can use binding getProperty () method to get the specified property value after the Groovy script runs, in order to use l in the later code, and the variables defined in the Groovy script (such as X) can be used as binding. Attribute is accessed

l It can be seen from the example above that the Groovy script is run in Java, which can be entered, and therefore, the GROOVY script can be implemented as a functional module to improve the coding efficiency.

3, dynamism in Java, load and run Groovy code

Import groovy.lang.groovyclassloader;

Import groovy.lang.groovyObject;

Import java.io.file;

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 = loadinger.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 / FOO.GROOVY", "Run", params);

System.out.println (Result);

System.out.println (DynamicGroovy.getProperty);

}

}

l Use GroovyClassLoader to dynamically load the Groovy class in the Java program and execute them (call its method)

l GroovyClassLoader Parseclass () method parses the Groovy script of the specified file to generate the corresponding Groovy class and load

l You can create an instance of a GroovyObject interface, call its InvokeMethod () method to call the Groovy class

The two parameters of the INVokeMethod () method respectively correspond to the method name of the Groovy class and the array of parameters passed to the method.

l You can use the GROOVYOBJECT's getProperty () method to get the specified property value of the Groovy class.

转载请注明原文地址:https://www.9cbs.com/read-112043.html

New Post(0)