Java Code Examples for groovy.lang.GroovyObject#invokeMethod()

The following examples show how to use groovy.lang.GroovyObject#invokeMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyExecUtil.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public static Object execGroovyRetObj(GroovyObject groovyObject, String methodName,
		Object... paramArray) {
	try {
		//add 201806 ninghao
		if(groovyObject==null){
			throw new RuntimeException("groovyObject is null methodName="+methodName);
		}			
		
		//GroovyObject groovyObject = (GroovyObject) getGroovyObj(groovyName);
		//GroovyAopInter groovyAop=(GroovyAopInter) MicroContextHolder.getContextMap().get("groovyAop");
		GroovyAopInter firstAop=GroovyAopChain.getFirstAop();
		Object retObj=null;
		if(firstAop==null){
			retObj=groovyObject.invokeMethod(methodName, paramArray);
		}else{
			retObj=firstAop.invokeMethod(groovyObject,null, methodName, paramArray);
		}
		return retObj;
	} catch (Throwable t) {
		logger.error(t.toString(), t);
		if(throwFlag){
			throw new RuntimeException(t);
		}
		return null;
	}
}
 
Example 2
Source File: GroovyCompilerTest.java    From zuul with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadGroovyFromString() {

    GroovyCompiler compiler = Mockito.spy(new GroovyCompiler());

    try {

        String code = "class test { public String hello(){return \"hello\" } } ";
        Class clazz = compiler.compile(code, "test");
        assertNotNull(clazz);
        assertEquals(clazz.getName(), "test");
        GroovyObject groovyObject = (GroovyObject) clazz.newInstance();
        Object[] args = {};
        String s = (String) groovyObject.invokeMethod("hello", args);
        assertEquals(s, "hello");
    } catch (Exception e) {
        assertFalse(true);
    }
}
 
Example 3
Source File: GroovyExecUtil.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public static Object execGroovyRetObj(String groovyName, String methodName,
		Object... paramArray) {
	try {
		GroovyObject groovyObject = (GroovyObject) getGroovyObj(groovyName);
		//add 201806 ninghao
		if(groovyObject==null){
			throw new RuntimeException("groovyName="+groovyName+" is null methodName="+methodName);
		}	
		
		//GroovyAopInter groovyAop=(GroovyAopInter) MicroContextHolder.getContextMap().get("groovyAop");
		GroovyAopInter firstAop=GroovyAopChain.getFirstAop();
		Object retObj=null;
		if(firstAop==null){
			retObj=groovyObject.invokeMethod(methodName, paramArray);
		}else{
			retObj=firstAop.invokeMethod(groovyObject,groovyName, methodName, paramArray);
		}
		return retObj;
	} catch (Throwable t) {
		logger.error(t.toString(), t);
		if(throwFlag){
			throw new RuntimeException(t);
		}
		return null;
	}
}
 
Example 4
Source File: PatternSetAntBuilderDelegate.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static Object addFilenames(Object node, Iterable<String> filenames, boolean caseSensitive) {
    GroovyObject groovyObject = (GroovyObject) node;
    Map<String, Object> props = new HashMap<String, Object>(2);
    props.put("casesensitive", caseSensitive);
    for (String filename : filenames) {
        props.put("name", filename);
        groovyObject.invokeMethod("filename", props);
    }
    return node;
}
 
Example 5
Source File: ReflexMatchContext.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public void send(Map<String,Object> vals, GroovyObject action) {
   if (action instanceof Closure) {
      send(vals, (Closure<?>)action);
   } else {
      action.invokeMethod("send", new Object[] { getProtocolSendProcessor(), vals });
   }
}
 
Example 6
Source File: PatternSetAntBuilderDelegate.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static Object addFilenames(Object node, Iterable<String> filenames, boolean caseSensitive) {
    GroovyObject groovyObject = (GroovyObject) node;
    Map<String, Object> props = new HashMap<String, Object>(2);
    props.put("casesensitive", caseSensitive);
    for (String filename : filenames) {
        props.put("name", filename);
        groovyObject.invokeMethod("filename", props);
    }
    return node;
}
 
Example 7
Source File: Node.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void buildChildren(final GroovyObject builder, final Map namespaceMap, final Map<String, String> namespaceTagHints) {
    for (Object child : this.children) {
        if (child instanceof Node) {
            ((Node) child).build(builder, namespaceMap, namespaceTagHints);
        } else if (child instanceof Buildable) {
            ((Buildable) child).build(builder);
        } else {
            builder.getProperty("mkp");
            builder.invokeMethod("yield", new Object[]{child});
        }
    }
}
 
Example 8
Source File: GroovyClassLoaderTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testCompile() throws Exception {
    Class groovyClass = loader.parseClass(new File("src/test/org/codehaus/groovy/classgen/Main.groovy"));

    System.out.println("Invoking main...");

    GroovyObject object = (GroovyObject) groovyClass.getDeclaredConstructor().newInstance();

    assertTrue(object != null);

    MetaClass metaClass = object.getMetaClass();
    System.out.println("Metaclass: " + metaClass);

    Class type = object.getClass();
    System.out.println("Type: " + type);

    // invoke via metaclass
    metaClass.invokeMethod(object, "main", null);

    // invoke directly
    object.invokeMethod("main", null);
}
 
Example 9
Source File: RunBugsTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testSuperMethod2Bug() throws Exception {
    GroovyObject object = compile("src/test/groovy/bugs/SuperMethod2Bug.groovy");
    object.invokeMethod("testBug", null);
}
 
Example 10
Source File: RunBugsTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testRodsBug() throws Exception {
    GroovyObject object = compile("src/test/groovy/bugs/RodsBug.groovy");
    object.invokeMethod("testBug", null);
}
 
Example 11
Source File: NodePrinterTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testSmallTree() throws Exception {
    GroovyObject object = compile("src/test/groovy/tree/SmallTreeTest.groovy");
    object.invokeMethod("testTree", null);
}
 
Example 12
Source File: RunBugsTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testClosureParameterPassingBug() throws Exception {
    GroovyObject object = compile("src/test/groovy/bugs/ClosureParameterPassingBug.groovy");
    object.invokeMethod("testBug", null);
}
 
Example 13
Source File: RunBugsTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testByteIndexBug() throws Exception {
    GroovyObject object = compile("src/test/groovy/bugs/ByteIndexBug.groovy");
    object.invokeMethod("testBug", null);
}
 
Example 14
Source File: RunClosureTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testBytecode4Bug() throws Exception {
    GroovyObject object = compile("src/test/groovy/bugs/Bytecode4Bug.groovy");
    object.invokeMethod("testInject", null);
    object.invokeMethod("testUsingProperty", null);
}
 
Example 15
Source File: NodePrinterTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testLittleClosure() throws Exception {
    GroovyObject object = compile("src/test/groovy/LittleClosureTest.groovy");
    object.invokeMethod("testClosure", null);
}
 
Example 16
Source File: NodePrinterTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testVerboseTree() throws Exception {
    GroovyObject object = compile("src/test/groovy/tree/VerboseTreeTest.groovy");
    object.invokeMethod("testTree", null);
}
 
Example 17
Source File: RunClosureTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testBytecode5Bug() throws Exception {
    GroovyObject object = compile("src/test/groovy/bugs/Bytecode5Bug.groovy");
    object.invokeMethod("testUsingLocalVar", null);
}
 
Example 18
Source File: RunBugsTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testMarkupAndMethodBug() throws Exception {
    GroovyObject object = compile("src/test/groovy/bugs/MarkupAndMethodBug.groovy");
    object.invokeMethod("testBug", null);
}
 
Example 19
Source File: RunClosureTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testBytecode2Bug() throws Exception {
    GroovyObject object = compile("src/test/groovy/bugs/Bytecode2Bug.groovy");
    object.invokeMethod("testTedsBytecodeBug", null);
}
 
Example 20
Source File: RunBugsTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testClosureVariableBug() throws Exception {
    GroovyObject object = compile("src/test/groovy/bugs/ClosureVariableBug.groovy");
    object.invokeMethod("testBug", null);
}