Search in sources :

Example 21 with GroovyObject

use of groovy.lang.GroovyObject in project cucumber-jvm by cucumber.

the class GroovyWorld method findWorldWithMethod.

private GroovyObject findWorldWithMethod(String methodName, Object arguments) {
    Object[] args = unwrapMethodArguments(arguments);
    if (worlds.isEmpty()) {
        throw new MissingMethodException(methodName, this.getClass(), args);
    }
    if (worlds.size() == 1) {
        return worlds.get(0);
    }
    GroovyObject worldWithMethod = null;
    for (GroovyObject world : worlds) {
        if (world.getMetaClass().getMetaMethod(methodName, args) != null) {
            if (worldWithMethod == null) {
                worldWithMethod = world;
            } else {
                throw new RuntimeException("Multiple method call: " + methodName);
            }
        }
    }
    if (worldWithMethod == null) {
        throw new MissingMethodException(methodName, this.getClass(), args);
    }
    return worldWithMethod;
}
Also used : MissingMethodException(groovy.lang.MissingMethodException) GroovyObject(groovy.lang.GroovyObject) GroovyObject(groovy.lang.GroovyObject)

Example 22 with GroovyObject

use of groovy.lang.GroovyObject in project qi4j-sdk by Qi4j.

the class GroovyMixin method invokeAsObject.

private Object invokeAsObject(Method method, Object[] args, URL groovySource) throws Throwable {
    try {
        Class declaringClass = method.getDeclaringClass();
        GroovyObject groovyObject = groovyObjects.get(declaringClass);
        if (groovyObject == null) {
            InputStream is = null;
            final Class groovyClass;
            try {
                is = groovySource.openStream();
                StringBuilder sourceBuilder = new StringBuilder();
                Inputs.text(groovySource).transferTo(Outputs.text(sourceBuilder));
                GroovyClassLoader groovyClassLoader = new GroovyClassLoader(declaringClass.getClassLoader());
                groovyClass = groovyClassLoader.parseClass(sourceBuilder.toString());
            } finally {
                if (is != null) {
                    is.close();
                }
            }
            groovyObject = (GroovyObject) groovyClass.newInstance();
            if (hasProperty(groovyObject, "This")) {
                groovyObject.setProperty("This", me);
            }
            groovyObjects.put(declaringClass, groovyObject);
        }
        return groovyObject.invokeMethod(method.getName(), args);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) InputStream(java.io.InputStream) MissingPropertyException(groovy.lang.MissingPropertyException) GroovyObject(groovy.lang.GroovyObject)

Example 23 with GroovyObject

use of groovy.lang.GroovyObject in project atlas by alibaba.

the class ClosureFactory method buildClosure.

public static Closure<?> buildClosure(String... strings) throws IOException {
    Closure<?> closure = null;
    // Create a method returning a closure
    StringBuilder sb = new StringBuilder("def closure() { { script -> ");
    sb.append(StringUtils.join(strings, "\n"));
    sb.append(" } }");
    // Create an anonymous class for the method
    GroovyClassLoader loader = new GroovyClassLoader();
    Class<?> groovyClass = loader.parseClass(sb.toString());
    try {
        // Create an instance of the class
        GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
        // Invoke the object's method and thus obtain the closure
        closure = (Closure<?>) groovyObject.invokeMethod("closure", null);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    } finally {
        loader.close();
    }
    return closure;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyObject(groovy.lang.GroovyObject)

Example 24 with GroovyObject

use of groovy.lang.GroovyObject in project fastjson by alibaba.

the class GroovyTest method test_groovy.

public void test_groovy() throws Exception {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    GroovyClassLoader loader = new GroovyClassLoader(parent);
    // A类
    Class AClass = loader.parseClass(//
    "class A {\n" + //
    "    int id\n" + "}");
    // A实例
    GroovyObject a = (GroovyObject) AClass.newInstance();
    a.setProperty("id", 33);
    String textA = JSON.toJSONString(a);
    GroovyObject aa = (GroovyObject) JSON.parseObject(textA, AClass);
    Assert.assertEquals(a.getProperty("id"), aa.getProperty("id"));
    System.out.println(a);
    // B类,继承于A
    Class BClass = loader.parseClass(//
    "class B extends A {\n" + //
    "    String name\n" + "}");
    // B实例
    GroovyObject b = (GroovyObject) BClass.newInstance();
    b.setProperty("name", "jobs");
    String textB = JSON.toJSONString(b);
    GroovyObject bb = (GroovyObject) JSON.parseObject(textB, BClass);
    Assert.assertEquals(b.getProperty("id"), bb.getProperty("id"));
    Assert.assertEquals(b.getProperty("name"), bb.getProperty("name"));
    // 序列化失败
    System.out.println(JSON.toJSONString(b, true));
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyObject(groovy.lang.GroovyObject)

Example 25 with GroovyObject

use of groovy.lang.GroovyObject in project spring-framework by spring-projects.

the class GroovyScriptFactoryTests method testWithTwoClassesDefinedInTheOneGroovyFile_CorrectClassFirst.

@Test
public void testWithTwoClassesDefinedInTheOneGroovyFile_CorrectClassFirst() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("twoClassesCorrectOneFirst.xml", getClass());
    Messenger messenger = (Messenger) ctx.getBean("messenger");
    assertNotNull(messenger);
    assertEquals("Hello World!", messenger.getMessage());
    // Check can cast to GroovyObject
    GroovyObject goo = (GroovyObject) messenger;
    assertNotNull(goo);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Messenger(org.springframework.scripting.Messenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) GroovyObject(groovy.lang.GroovyObject) Test(org.junit.Test)

Aggregations

GroovyObject (groovy.lang.GroovyObject)141 GroovyClassLoader (groovy.lang.GroovyClassLoader)14 Closure (groovy.lang.Closure)11 Map (java.util.Map)8 Decorator (com.opensymphony.module.sitemesh.Decorator)7 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)7 MetaClass (groovy.lang.MetaClass)7 File (java.io.File)7 HashMap (java.util.HashMap)7 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)7 Page (com.opensymphony.module.sitemesh.Page)5 HTMLPageParser (com.opensymphony.module.sitemesh.parser.HTMLPageParser)5 Config (grails.config.Config)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ServletContext (javax.servlet.ServletContext)5 PropertySourcesConfig (org.grails.config.PropertySourcesConfig)5 MockApplicationContext (org.grails.support.MockApplicationContext)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 MockServletConfig (org.springframework.mock.web.MockServletConfig)5 ArrayList (java.util.ArrayList)4