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

The following examples show how to use groovy.lang.MetaClass#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: ScriptBytecodeAdapter.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static Object invokeMethodOnSuperN(Class senderClass, GroovyObject receiver, String messageName, Object[] messageArguments) throws Throwable {
    MetaClass metaClass = receiver.getMetaClass();
    // ignore interception and missing method fallback
    Object result = null;
    try {
        result = metaClass.invokeMethod(senderClass, receiver, messageName, messageArguments, true, true);
    } catch (GroovyRuntimeException gre) {
        throw unwrap(gre);
    }
    return result;
}
 
Example 2
Source File: InvokerHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static Object invokeSuperMethod(Object object, String methodName, Object arguments) {
    if (object == null) {
        throw new NullPointerException("Cannot invoke method " + methodName + "() on null object");
    }

    Class theClass = object.getClass();

    MetaClass metaClass = metaRegistry.getMetaClass(theClass.getSuperclass());
    return metaClass.invokeMethod(object, methodName, asArray(arguments));
}
 
Example 3
Source File: GrailsDataBinder.java    From AlgoTrader with GNU General Public License v2.0 5 votes vote down vote up
private void addAssociationToTarget(String name, Object target, Object obj) {
	if (obj == null) {
		return;
	}

	MetaClassRegistry reg = GroovySystem.getMetaClassRegistry();
	MetaClass mc = reg.getMetaClass(target.getClass());
	final String addMethodName = "addTo" + GrailsNameUtils.getClassNameRepresentation(name);
	mc.invokeMethod(target, addMethodName, obj);
}
 
Example 4
Source File: DefaultInvoker.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Object superCall(Class methodType, Object receiver, String method, Object[] args) throws Throwable {
    try {
        MetaClass mc = InvokerHelper.getMetaClass(receiver.getClass());
        return mc.invokeMethod(methodType.getSuperclass(), receiver, method, args, true, true);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example 5
Source File: GroovyBeanDefinitionReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * This method overrides method invocation to create beans for each method name that
 * takes a class argument.
 */
public Object invokeMethod(String name, Object arg) {
	Object[] args = (Object[])arg;
	if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
		return beans((Closure) args[0]);
	}
	else if ("ref".equals(name)) {
		String refName;
		if (args[0] == null) {
			throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found");
		}
		if (args[0] instanceof RuntimeBeanReference) {
			refName = ((RuntimeBeanReference) args[0]).getBeanName();
		}
		else {
			refName = args[0].toString();
		}
		boolean parentRef = false;
		if (args.length > 1 && args[1] instanceof Boolean) {
			parentRef = (Boolean) args[1];
		}
		return new RuntimeBeanReference(refName, parentRef);
	}
	else if (this.namespaces.containsKey(name) && args.length > 0 && args[0] instanceof Closure) {
		GroovyDynamicElementReader reader = createDynamicElementReader(name);
		reader.invokeMethod("doCall", args);
	}
	else if (args.length > 0 && args[0] instanceof Closure) {
		// abstract bean definition
		return invokeBeanDefiningMethod(name, args);
	}
	else if (args.length > 0 &&
			(args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
		return invokeBeanDefiningMethod(name, args);
	}
	else if (args.length > 1 && args[args.length -1] instanceof Closure) {
		return invokeBeanDefiningMethod(name, args);
	}
	MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
	if (!mc.respondsTo(getRegistry(), name, args).isEmpty()){
		return mc.invokeMethod(getRegistry(), name, args);
	}
	return this;
}
 
Example 6
Source File: GroovyBeanDefinitionReader.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * This method overrides method invocation to create beans for each method name that
 * takes a class argument.
 */
public Object invokeMethod(String name, Object arg) {
	Object[] args = (Object[])arg;
	if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
		return beans((Closure) args[0]);
	}
	else if ("ref".equals(name)) {
		String refName;
		if (args[0] == null) {
			throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found");
		}
		if (args[0] instanceof RuntimeBeanReference) {
			refName = ((RuntimeBeanReference) args[0]).getBeanName();
		}
		else {
			refName = args[0].toString();
		}
		boolean parentRef = false;
		if (args.length > 1 && args[1] instanceof Boolean) {
			parentRef = (Boolean) args[1];
		}
		return new RuntimeBeanReference(refName, parentRef);
	}
	else if (this.namespaces.containsKey(name) && args.length > 0 && args[0] instanceof Closure) {
		GroovyDynamicElementReader reader = createDynamicElementReader(name);
		reader.invokeMethod("doCall", args);
	}
	else if (args.length > 0 && args[0] instanceof Closure) {
		// abstract bean definition
		return invokeBeanDefiningMethod(name, args);
	}
	else if (args.length > 0 &&
			(args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
		return invokeBeanDefiningMethod(name, args);
	}
	else if (args.length > 1 && args[args.length -1] instanceof Closure) {
		return invokeBeanDefiningMethod(name, args);
	}
	MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
	if (!mc.respondsTo(getRegistry(), name, args).isEmpty()){
		return mc.invokeMethod(getRegistry(), name, args);
	}
	return this;
}
 
Example 7
Source File: GroovyBeanDefinitionReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This method overrides method invocation to create beans for each method name that
 * takes a class argument.
 */
public Object invokeMethod(String name, Object arg) {
	Object[] args = (Object[])arg;
	if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
		return beans((Closure) args[0]);
	}
	else if ("ref".equals(name)) {
		String refName;
		if (args[0] == null)
			throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found");

		if (args[0] instanceof RuntimeBeanReference) {
			refName = ((RuntimeBeanReference) args[0]).getBeanName();
		}
		else {
			refName = args[0].toString();
		}
		boolean parentRef = false;
		if (args.length > 1) {
			if (args[1] instanceof Boolean) {
				parentRef = (Boolean) args[1];
			}
		}
		return new RuntimeBeanReference(refName, parentRef);
	}
	else if (this.namespaces.containsKey(name) && args.length > 0 && args[0] instanceof Closure) {
		GroovyDynamicElementReader reader = createDynamicElementReader(name);
		reader.invokeMethod("doCall", args);
	}
	else if (args.length > 0 && args[0] instanceof Closure) {
		// abstract bean definition
		return invokeBeanDefiningMethod(name, args);
	}
	else if (args.length > 0 &&
			(args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
		return invokeBeanDefiningMethod(name, args);
	}
	else if (args.length > 1 && args[args.length -1] instanceof Closure) {
		return invokeBeanDefiningMethod(name, args);
	}
	MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
	if (!mc.respondsTo(getRegistry(), name, args).isEmpty()){
		return mc.invokeMethod(getRegistry(), name, args);
	}
	return this;
}
 
Example 8
Source File: GroovyBeanDefinitionReader.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
/**
	 * This method overrides method invocation to create beans for each method name that
	 * takes a class argument.
	 */
	public Object invokeMethod(String name, Object arg) {
		Object[] args = (Object[])arg;
		if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
			return beans((Closure) args[0]);
		}
		else if ("ref".equals(name)) {
			String refName;
			if (args[0] == null)
				throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found");

			if (args[0] instanceof RuntimeBeanReference) {
				refName = ((RuntimeBeanReference)args[0]).getBeanName();
			}
			else {
				refName = args[0].toString();
			}
			boolean parentRef = false;
			if (args.length > 1) {
				if (args[1] instanceof Boolean) {
					parentRef = (Boolean) args[1];
				}
			}
			return new RuntimeBeanReference(refName, parentRef);
		}
		else if (this.namespaces.containsKey(name) && args.length > 0 && (args[0] instanceof Closure)) {
//			GroovyDynamicElementReader reader = createDynamicElementReader(name);
//			reader.invokeMethod("doCall", args);
		}
		else if (args.length > 0 && args[0] instanceof Closure) {
			// abstract bean definition
			return invokeBeanDefiningMethod(name, args);
		}
		else if (args.length > 0 && (args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
			return invokeBeanDefiningMethod(name, args);
		}
		else if (args.length > 1 && args[args.length -1] instanceof Closure) {
			return invokeBeanDefiningMethod(name, args);
		}
		MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
		if (!mc.respondsTo(getRegistry(), name, args).isEmpty()){
			return mc.invokeMethod(getRegistry(), name, args);
		}
		return this;
	}
 
Example 9
Source File: GroovyBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * This method overrides method invocation to create beans for each method name that
 * takes a class argument.
 */
public Object invokeMethod(String name, Object arg) {
	Object[] args = (Object[])arg;
	if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
		return beans((Closure) args[0]);
	}
	else if ("ref".equals(name)) {
		String refName;
		if (args[0] == null)
			throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found");

		if (args[0] instanceof RuntimeBeanReference) {
			refName = ((RuntimeBeanReference)args[0]).getBeanName();
		}
		else {
			refName = args[0].toString();
		}
		boolean parentRef = false;
		if (args.length > 1) {
			if (args[1] instanceof Boolean) {
				parentRef = (Boolean) args[1];
			}
		}
		return new RuntimeBeanReference(refName, parentRef);
	}
	else if (this.namespaces.containsKey(name) && args.length > 0 && (args[0] instanceof Closure)) {
		GroovyDynamicElementReader reader = createDynamicElementReader(name);
		reader.invokeMethod("doCall", args);
	}
	else if (args.length > 0 && args[0] instanceof Closure) {
		// abstract bean definition
		return invokeBeanDefiningMethod(name, args);
	}
	else if (args.length > 0 && (args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
		return invokeBeanDefiningMethod(name, args);
	}
	else if (args.length > 1 && args[args.length -1] instanceof Closure) {
		return invokeBeanDefiningMethod(name, args);
	}
	MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
	if (!mc.respondsTo(getRegistry(), name, args).isEmpty()){
		return mc.invokeMethod(getRegistry(), name, args);
	}
	return this;
}
 
Example 10
Source File: OwnedMetaClass.java    From groovy with Apache License 2.0 4 votes vote down vote up
public Object invokeMethod(Object object, String methodName, Object arguments) {
    final Object owner = getOwner();
    final MetaClass ownerMetaClass = getOwnerMetaClass(owner);
    return ownerMetaClass.invokeMethod(owner, methodName, arguments);
}
 
Example 11
Source File: OwnedMetaClass.java    From groovy with Apache License 2.0 4 votes vote down vote up
public Object invokeMethod(Object object, String methodName, Object[] arguments) {
    final Object owner = getOwner();
    final MetaClass ownerMetaClass = getOwnerMetaClass(owner);
    return ownerMetaClass.invokeMethod(owner, methodName, arguments);
}
 
Example 12
Source File: OwnedMetaClass.java    From groovy with Apache License 2.0 4 votes vote down vote up
public Object invokeMethod(Class sender, Object receiver, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass) {
    final Object owner = getOwner();
    final MetaClass ownerMetaClass = getOwnerMetaClass(owner);
    return ownerMetaClass.invokeMethod(sender, owner, methodName, arguments, isCallToSuper, fromInsideClass);
}
 
Example 13
Source File: InvokerHelper.java    From groovy with Apache License 2.0 4 votes vote down vote up
static Object invokePojoMethod(Object object, String methodName, Object arguments) {
    MetaClass metaClass = InvokerHelper.getMetaClass(object);
    return metaClass.invokeMethod(object, methodName, asArray(arguments));
}
 
Example 14
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);
}