Search in sources :

Example 1 with VMPlugin

use of org.codehaus.groovy.vmplugin.VMPlugin in project groovy by apache.

the class ConversionHandler method invoke.

/**
     * This method is a default implementation for the invoke method given in
     * InvocationHandler. Any call to a method with a declaring class that is
     * not Object, excluding toString() and default methods is redirected to invokeCustom.
     * <p>
     * Methods like equals and hashcode are called on the class itself instead
     * of the delegate because they are considered fundamental methods that should
     * not be overwritten. The toString() method gets special treatment as it is
     * deemed to be a method that you might wish to override when called from Groovy.
     * Interface default methods from Java 8 on the other hand are considered being
     * default implementations you don't normally want to change. So they are called
     * directly too
     * </p><p>
     * In many scenarios, it is better to overwrite the invokeCustom method where
     * the core Object related methods are filtered out.
     *</p>
     * @param proxy  the proxy
     * @param method the method
     * @param args   the arguments
     * @return the result of the invocation by method or delegate
     * @throws Throwable if caused by the delegate or the method
     * @see #invokeCustom(Object, Method, Object[])
     * @see InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
     */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (handleCache != null && isDefaultMethod(method)) {
        VMPlugin plugin = VMPluginFactory.getPlugin();
        Object handle = handleCache.get(method);
        if (handle == null) {
            handle = plugin.getInvokeSpecialHandle(method, proxy);
            handleCache.put(method, handle);
        }
        return plugin.invokeHandle(handle, args);
    }
    if (!checkMethod(method)) {
        try {
            if (method.getDeclaringClass() == GroovyObject.class) {
                if ("getMetaClass".equals(method.getName())) {
                    return getMetaClass(proxy);
                } else if ("setMetaClass".equals(method.getName())) {
                    return setMetaClass((MetaClass) args[0]);
                }
            }
            return invokeCustom(proxy, method, args);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    }
    try {
        return method.invoke(this, args);
    } catch (InvocationTargetException ite) {
        throw ite.getTargetException();
    }
}
Also used : MetaClass(groovy.lang.MetaClass) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyObject(groovy.lang.GroovyObject) VMPlugin(org.codehaus.groovy.vmplugin.VMPlugin) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with VMPlugin

use of org.codehaus.groovy.vmplugin.VMPlugin in project groovy-core by groovy.

the class ConversionHandler method invoke.

/**
 * This method is a default implementation for the invoke method given in
 * InvocationHandler. Any call to a method with a declaring class that is
 * not Object, excluding toString() and default methods is redirected to invokeCustom.
 * <p>
 * Methods like equals and hashcode are called on the class itself instead
 * of the delegate because they are considered fundamental methods that should
 * not be overwritten. The toString() method gets special treatment as it is
 * deemed to be a method that you might wish to override when called from Groovy.
 * Interface default methods from Java 8 on the other hand are considered being
 * default implementations you don't normally want to change. So they are called
 * directly too
 * </p><p>
 * In many scenarios, it is better to overwrite the invokeCustom method where
 * the core Object related methods are filtered out.
 *</p>
 * @param proxy  the proxy
 * @param method the method
 * @param args   the arguments
 * @return the result of the invocation by method or delegate
 * @throws Throwable if caused by the delegate or the method
 * @see #invokeCustom(Object, Method, Object[])
 * @see InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
 */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    VMPlugin plugin = VMPluginFactory.getPlugin();
    if (plugin.getVersion() >= 7 && isDefaultMethod(method)) {
        Object handle = handleCache.get(method);
        if (handle == null) {
            handle = plugin.getInvokeSpecialHandle(method, proxy);
            handleCache.put(method, handle);
        }
        return plugin.invokeHandle(handle, args);
    }
    if (!checkMethod(method)) {
        try {
            return invokeCustom(proxy, method, args);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    }
    try {
        return method.invoke(this, args);
    } catch (InvocationTargetException ite) {
        throw ite.getTargetException();
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) VMPlugin(org.codehaus.groovy.vmplugin.VMPlugin) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 VMPlugin (org.codehaus.groovy.vmplugin.VMPlugin)2 GroovyObject (groovy.lang.GroovyObject)1 MetaClass (groovy.lang.MetaClass)1