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