Java Code Examples for org.codehaus.groovy.runtime.InvokerHelper#invokeMethod()

The following examples show how to use org.codehaus.groovy.runtime.InvokerHelper#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: BindPath.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Add listeners to a specific object.  Updates the bould flags and update set
 *
 * @param listener This listener to attach.
 * @param newObject The object we should read our property off of.
 * @param updateSet The list of objects we have added listeners to
 */
public void addListeners(PropertyChangeListener listener, Object newObject, Set updateSet) {
    removeListeners();
    if (newObject != null) {
        // check for local synthetics
        TriggerBinding syntheticTrigger = getSyntheticTriggerBinding(newObject);
        MetaClass mc = InvokerHelper.getMetaClass(newObject);
        if (syntheticTrigger != null) {
            PropertyBinding psb = new PropertyBinding(newObject, propertyName);
            PropertyChangeProxyTargetBinding proxytb = new PropertyChangeProxyTargetBinding(newObject, propertyName, listener);

            syntheticFullBinding = syntheticTrigger.createBinding(psb, proxytb);
            syntheticFullBinding.bind();
            updateSet.add(newObject);
        } else if (!mc.respondsTo(newObject, "addPropertyChangeListener", NAME_PARAMS).isEmpty()) {
            InvokerHelper.invokeMethod(newObject, "addPropertyChangeListener", new Object[] {propertyName, listener});
            localListener = listener;
            updateSet.add(newObject);
        } else if (!mc.respondsTo(newObject, "addPropertyChangeListener", GLOBAL_PARAMS).isEmpty()) {
            InvokerHelper.invokeMethod(newObject, "addPropertyChangeListener", listener);
            globalListener = listener;
            updateSet.add(newObject);
        }
    }
    currentObject = newObject;
}
 
Example 2
Source File: GroovyBeanDefinitionReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public boolean addAll(Collection values) {
	boolean retVal = (Boolean) InvokerHelper.invokeMethod(this.propertyValue, "addAll", values);
	for (Object value : values) {
		updateDeferredProperties(value);
	}
	return retVal;
}
 
Example 3
Source File: Proxy.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object invokeMethod(String name, Object args) {
    try {
        return super.invokeMethod(name, args);
    }
    catch (MissingMethodException e) {
        return InvokerHelper.invokeMethod(adaptee, name, args);
    }
}
 
Example 4
Source File: GString.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Overloaded to implement duck typing for Strings
 * so that any method that can't be evaluated on this
 * object will be forwarded to the toString() object instead.
 */
@Override
public Object invokeMethod(String name, Object args) {
    try {
        return super.invokeMethod(name, args);
    } catch (MissingMethodException e) {
        // lets try invoke the method on the real String
        return InvokerHelper.invokeMethod(toString(), name, args);
    }
}
 
Example 5
Source File: DefaultTypeTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static boolean compareEqual(Object left, Object right) {
    if (left == right) return true;
    if (left == null) return right instanceof NullObject;
    if (right == null) return left instanceof NullObject;
    if (left instanceof Comparable) {
        return compareToWithEqualityCheck(left, right, true) == 0;
    }
    // handle arrays on both sides as special case for efficiency
    Class leftClass = left.getClass();
    Class rightClass = right.getClass();
    if (leftClass.isArray() && rightClass.isArray()) {
        return compareArrayEqual(left, right);
    }
    if (leftClass.isArray() && leftClass.getComponentType().isPrimitive()) {
        left = primitiveArrayToList(left);
    }
    if (rightClass.isArray() && rightClass.getComponentType().isPrimitive()) {
        right = primitiveArrayToList(right);
    }
    if (left instanceof Object[] && right instanceof List) {
        return DefaultGroovyMethods.equals((Object[]) left, (List) right);
    }
    if (left instanceof List && right instanceof Object[]) {
        return DefaultGroovyMethods.equals((List) left, (Object[]) right);
    }
    if (left instanceof List && right instanceof List) {
        return DefaultGroovyMethods.equals((List) left, (List) right);
    }
    if (left instanceof Map.Entry && right instanceof Map.Entry) {
        Object k1 = ((Map.Entry) left).getKey();
        Object k2 = ((Map.Entry) right).getKey();
        if (Objects.equals(k1, k2)) {
            Object v1 = ((Map.Entry) left).getValue();
            Object v2 = ((Map.Entry) right).getValue();
            return v1 == v2 || (v1 != null && DefaultTypeTransformation.compareEqual(v1, v2));
        }
        return false;
    }
    return (Boolean) InvokerHelper.invokeMethod(left, "equals", right);
}
 
Example 6
Source File: Script.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Prints a formatted string using the specified format string and arguments.
 *
 * @param format the format to follow
 * @param values an array of values to be formatted
 */
public void printf(String format, Object[] values) {
    Object object;

    try {
        object = getProperty("out");
    } catch (MissingPropertyException e) {
        DefaultGroovyMethods.printf(System.out, format, values);
        return;
    }

    InvokerHelper.invokeMethod(object, "printf", new Object[] { format, values });
}
 
Example 7
Source File: MethodTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void assertCallMethod(Object object, String method, Object expected) {
    Object value = InvokerHelper.invokeMethod(object, method, new Object[0]);
    assertEquals("Result of calling method: " + method + " on: " + object + " with empty list", expected, value);

    value = InvokerHelper.invokeMethod(object, method, null);
    assertEquals("Result of calling method: " + method + " on: " + object + " with null", expected, value);
}
 
Example 8
Source File: HtmlRenderContext.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private boolean groovyTrue(Object equal) {
    return (Boolean) InvokerHelper.invokeMethod(equal, "asBoolean", null);
}
 
Example 9
Source File: GroovyBeanDefinitionReader.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
public Object invokeMethod(String name, Object args) {
	return InvokerHelper.invokeMethod(this.propertyValue, name, args);
}
 
Example 10
Source File: ClosureMetaMethod.java    From groovy with Apache License 2.0 4 votes vote down vote up
public Object invoke(Object object, Object[] arguments) {
    Closure cloned = (Closure) closure.clone();
    cloned.setDelegate(object);
    arguments = coerceArgumentsToClasses(arguments);
    return InvokerHelper.invokeMethod(cloned, "call", arguments);
}
 
Example 11
Source File: GroovyBeanDefinitionReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public boolean add(Object value) {
	boolean retVal = (Boolean) InvokerHelper.invokeMethod(this.propertyValue, "add", value);
	updateDeferredProperties(value);
	return retVal;
}
 
Example 12
Source File: GroovyBeanDefinitionReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void leftShift(Object value) {
	InvokerHelper.invokeMethod(this.propertyValue, "leftShift", value);
	updateDeferredProperties(value);
}
 
Example 13
Source File: ClosureSpec.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public boolean isSatisfiedBy(T element) {
    Object value = closure.call(element);
    return (Boolean)InvokerHelper.invokeMethod(value, "asBoolean", null);
}
 
Example 14
Source File: ClosureSpec.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public boolean isSatisfiedBy(T element) {
    Object value = closure.call(element);
    return (Boolean)InvokerHelper.invokeMethod(value, "asBoolean", null);
}
 
Example 15
Source File: GStringTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testConstructor() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);

    //Statement printStatement = createPrintlnStatement(new VariableExpression("str"));

    // simulate "Hello ${user}!"
    GStringExpression compositeStringExpr = new GStringExpression("hello ${user}!");
    compositeStringExpr.addString(new ConstantExpression("Hello "));
    compositeStringExpr.addValue(new VariableExpression("user"));
    compositeStringExpr.addString(new ConstantExpression("!"));
    BlockStatement block = new BlockStatement();
    block.addStatement(
            new ExpressionStatement(
                    new DeclarationExpression(
                            new VariableExpression("user"),
                            Token.newSymbol("=", -1, -1),
                            new ConstantExpression("World"))));
    block.addStatement(
            new ExpressionStatement(
                    new DeclarationExpression(new VariableExpression("str"), Token.newSymbol("=", -1, -1), compositeStringExpr)));
    block.addStatement(
            new ExpressionStatement(
                    new MethodCallExpression(VariableExpression.THIS_EXPRESSION, "println", new VariableExpression("str"))));

    block.addStatement(
            new ExpressionStatement(
                    new DeclarationExpression(
                            new VariableExpression("text"),
                            Token.newSymbol("=", -1, -1),
                            new MethodCallExpression(new VariableExpression("str"), "toString", MethodCallExpression.NO_ARGUMENTS))));

    block.addStatement(
            new AssertStatement(
                    new BooleanExpression(
                            new BinaryExpression(
                                    new VariableExpression("text"),
                                    Token.newSymbol("==", -1, -1),
                                    new ConstantExpression("Hello World!"))),
                    new ConstantExpression("Assertion failed") // TODO FIX if empty, AssertionWriter fails because source text is null
            )
    );
    classNode.addMethod(new MethodNode("stringDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block));

    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);

    Object bean = fooClass.getDeclaredConstructor().newInstance();
    assertTrue("Managed to create bean", bean != null);

    //Object[] array = { new Integer(1234), "abc", "def" };

    try {
        InvokerHelper.invokeMethod(bean, "stringDemo", null);
    }
    catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
}
 
Example 16
Source File: GroovyBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void leftShift(Object value) {
	InvokerHelper.invokeMethod(this.propertyValue, "leftShift", value);
	updateDeferredProperties(value);
}
 
Example 17
Source File: HtmlRenderContext.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private boolean groovyTrue(Object equal) {
    return (Boolean) InvokerHelper.invokeMethod(equal, "asBoolean", null);
}
 
Example 18
Source File: GroovyResultSetProxy.java    From groovy with Apache License 2.0 3 votes vote down vote up
/**
 * Invokes a method for the GroovyResultSet.
 * This will try to invoke the given method first on the extension
 * and then on the result set given as proxy parameter.
 *
 * @param proxy  the result set
 * @param method the method name of this method will be used
 *               to make a call on the extension. If this fails the call will be
 *               done on the proxy instead
 * @param args   for the call
 * @see ResultSet
 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
 */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    String name = method.getName();
    if (method.getDeclaringClass() == GroovyObject.class) {
        if (name.equals("getMetaClass")) {
            return getMetaClass();
        } else if (name.equals("setMetaClass")) {
            return setMetaClass((MetaClass) args[0]);
        }
    }

    return InvokerHelper.invokeMethod(extension, method.getName(), args);
}
 
Example 19
Source File: GroovyTestCase.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Asserts that the value of inspect() on the given object matches the
 * given text string
 *
 * @param value    the object to be output to the console
 * @param expected the expected String representation
 */
protected void assertInspect(Object value, String expected) {
    Object console = InvokerHelper.invokeMethod(value, "inspect", null);
    assertEquals("inspect() on value: " + value, expected, console);
}
 
Example 20
Source File: GroovyTestCase.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Asserts that the value of toString() on the given object matches the
 * given text string
 *
 * @param value    the object to be output to the console
 * @param expected the expected String representation
 */
protected void assertToString(Object value, String expected) {
    Object console = InvokerHelper.invokeMethod(value, "toString", null);
    assertEquals("toString() on value: " + value, expected, console);
}