Search in sources :

Example 61 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 62 with GroovyObject

use of groovy.lang.GroovyObject in project runesource by PureCS.

the class PluginHandler method loadPlugins.

/**
     * Loads all plugins.
     *
     * @param pluginsFile a text file containing a list of plugins to load
     * @throws Exception
     */
public static void loadPlugins(String pluginsFile) throws Exception {
    loadedPluginFile = pluginsFile;
    File file = new File(pluginsFile);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String pluginName;
    while ((pluginName = reader.readLine()) != null) {
        if (pluginName.trim().length() == 0)
            continue;
        // Loading the plugin instance
        Class cls = classLoader.parseClass(new File(PLUGIN_DIRECTORY + pluginName + ".groovy"));
        GroovyObject obj = (GroovyObject) cls.newInstance();
        Plugin plugin = (Plugin) obj;
        plugin.setInstance(obj);
        register(pluginName.replace('/', '.'), plugin);
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) GroovyObject(groovy.lang.GroovyObject)

Example 63 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 64 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)

Example 65 with GroovyObject

use of groovy.lang.GroovyObject in project gradle by gradle.

the class AsmBackedClassGeneratorTest method mixesInGroovyObjectInterface.

@Test
public void mixesInGroovyObjectInterface() throws Exception {
    Class<? extends Bean> generatedClass = generator.generate(Bean.class);
    assertTrue(GroovyObject.class.isAssignableFrom(generatedClass));
    Bean bean = generatedClass.newInstance();
    GroovyObject groovyObject = (GroovyObject) bean;
    assertThat(groovyObject.getMetaClass(), notNullValue());
    groovyObject.setProperty("prop", "value");
    assertThat(bean.getProp(), equalTo("value"));
    assertThat(groovyObject.getProperty("prop"), equalTo((Object) "value"));
    assertThat(groovyObject.invokeMethod("doStuff", new Object[] { "some value" }), equalTo((Object) "{some value}"));
}
Also used : BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) GroovyObject(groovy.lang.GroovyObject) DslObject(org.gradle.api.internal.plugins.DslObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) GroovyObject(groovy.lang.GroovyObject) Test(org.junit.Test)

Aggregations

GroovyObject (groovy.lang.GroovyObject)142 GroovyClassLoader (groovy.lang.GroovyClassLoader)15 Closure (groovy.lang.Closure)11 File (java.io.File)8 Map (java.util.Map)8 Decorator (com.opensymphony.module.sitemesh.Decorator)7 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)7 MetaClass (groovy.lang.MetaClass)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