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

The following examples show how to use groovy.lang.GroovyObject#getClass() . 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: CallSiteArray.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static CallSite createCallCurrentSite(CallSite callSite, GroovyObject receiver, Object[] args, Class sender) {
    CallSite site;
    if (receiver instanceof GroovyInterceptable) {
        site = new PogoInterceptableSite(callSite);
    } else {
        MetaClass metaClass = receiver.getMetaClass();
        Class theClass = metaClass.getTheClass();
        if (receiver.getClass() != theClass && !theClass.isInterface()) {
            site = new PogoInterceptableSite(callSite);
        } else if (metaClass instanceof MetaClassImpl) {
            site = ((MetaClassImpl) metaClass).createPogoCallCurrentSite(callSite, sender, args);
        } else {
            site = new PogoMetaClassSite(callSite, metaClass);
        }
    }

    replaceCallSite(callSite, site);
    return site;
}
 
Example 2
Source File: PogoMetaClassSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object callCurrent(final GroovyObject receiver, final Object[] args) throws Throwable {
    if (checkCall(receiver)) {
        try {
            try {
                return metaClass.invokeMethod(array.owner, receiver, name, args, false, true);
            } catch (MissingMethodException e) {
                if (e instanceof MissingMethodExecutionFailed) {
                    throw (MissingMethodException) e.getCause();
                } else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) {
                    // in case there's nothing else, invoke the receiver's own invokeMethod()
                    try {
                        return receiver.invokeMethod(name, args);
                    } catch (MissingMethodException mme) {
                        if (mme instanceof MissingMethodExecutionFailed)
                            throw (MissingMethodException) mme.getCause();
                        // GROOVY-9387: in rare cases, this form still works
                        return metaClass.invokeMethod(receiver, name, args);
                    }
                } else {
                    throw e;
                }
            }
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else {
        return CallSiteArray.defaultCallCurrent(this, receiver, args);
    }
}
 
Example 3
Source File: ScriptBytecodeAdapter.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static Object invokeMethodOnCurrentN(Class senderClass, GroovyObject receiver, String messageName, Object[] messageArguments) throws Throwable {
    Object result = null;
    boolean intercepting = receiver instanceof GroovyInterceptable;
    try {
        try {
            // if it's a pure interceptable object (even intercepting toString(), clone(), ...)
            if (intercepting) {
                result = receiver.invokeMethod(messageName, messageArguments);
            }
            //else if there's a statically typed method or a GDK method
            else {
                result = receiver.getMetaClass().invokeMethod(senderClass, receiver, messageName, messageArguments, false, true);
            }
        } catch (MissingMethodException e) {
            if (e instanceof MissingMethodExecutionFailed) {
                throw (MissingMethodException)e.getCause();
            } else if (!intercepting && receiver.getClass() == e.getType() && e.getMethod().equals(messageName)) {
                // in case there's nothing else, invoke the object's own invokeMethod()
                result = receiver.invokeMethod(messageName, messageArguments);
            } else {
                throw e;
            }
        }
    } catch (GroovyRuntimeException gre) {
        throw unwrap(gre);
    }
    return result;
}
 
Example 4
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);
}