Package

Source Code of Groovy3

import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.lang.MetaClass;
import groovy.lang.Script;

import java.io.File;
import java.lang.reflect.Field;

import test.CustomerService;

public class Groovy3 {

  /**
   * @param args
   * @throws Exception
   * @throws InstantiationException
   */
  public static void main(final String[] args) throws InstantiationException, Exception {
    Binding binding = new Binding();
    binding.setVariable("foo", new Integer(2));
    GroovyShell shell = new GroovyShell(binding);
    Script script = shell.parse("service.with{sru()} ;x = 123; return foo * 10;");
    MetaClass metaClass = script.getMetaClass();
    Binding binding2 = script.getBinding();
    // Object property = script.getProperty("service");
    script.setProperty("service", new CustomerService() {

      @Override
      public void sru() {
        System.out.println("sruuu");

      }

    });
    script.run();

    String fileName = "src/main/java/Tester.groovy";
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class clazz = gcl.parseClass(new File(fileName));
    Field[] fields = clazz.getFields();
    Field[] declaredFields = clazz.getDeclaredFields();
    Field declaredField = clazz.getDeclaredField("service");
    Object aScript = clazz.newInstance();

    TestInterface ifc = (TestInterface) aScript;
    ifc.printIt();

  }
}
TOP

Related Classes of Groovy3

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.