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;
}
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);
}
}
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));
}
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);
}
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}"));
}
Aggregations