Java Method Call invokeDefaultMethod(final Method method, final Object[] args, final Object proxy)

Here you can find the source of invokeDefaultMethod(final Method method, final Object[] args, final Object proxy)

Description

invoke Default Method

License

Apache License

Parameter

Parameter Description
method a parameter
args a parameter
proxy a parameter

Exception

Parameter Description
Throwable (whatever the invoked method throws)

Return

return value of invoked method

Declaration

public static Object invokeDefaultMethod(final Method method, final Object[] args, final Object proxy)
        throws Throwable 

Method Source Code

//package com.java2s;
/**//from   www  .j  ava 2  s  . c om
 *  Copyright 2013 Sven Ewald
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    /**
     * @param method
     * @param args
     * @param proxy
     * @return return value of invoked method
     * @throws Throwable
     *             (whatever the invoked method throws)
     */
    public static Object invokeDefaultMethod(final Method method, final Object[] args, final Object proxy)
            throws Throwable {
        try {
            Class<?> MHclass = Class.forName("java.lang.invoke.MethodHandles");
            Object lookup = MHclass.getMethod("lookup", (Class<?>[]) null).invoke(null, (Object[]) null);

            Constructor<?> constructor = lookup.getClass().getDeclaredConstructor(Class.class);
            constructor.setAccessible(true);
            Object newLookupInstance = constructor.newInstance(method.getDeclaringClass());

            Object in = newLookupInstance.getClass().getMethod("in", new Class<?>[] { Class.class })
                    .invoke(newLookupInstance, method.getDeclaringClass());

            Object unreflectSpecial = in.getClass()
                    .getMethod("unreflectSpecial", new Class<?>[] { Method.class, Class.class })
                    .invoke(in, method, method.getDeclaringClass());
            Object bindTo = unreflectSpecial.getClass().getMethod("bindTo", Object.class).invoke(unreflectSpecial,
                    proxy);
            try {
                Object result = bindTo.getClass().getMethod("invokeWithArguments", Object[].class).invoke(bindTo,
                        new Object[] { args });
                return result;
            } catch (InvocationTargetException e) {
                if (e.getCause() != null) {
                    throw e.getCause();
                }
                throw e;
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
  2. invokeDeclared(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
  3. invokeDeclaredMethod(Object object, String methodName, Object... parameters)
  4. invokeDeclaredMethod(String methodName, Object target, Class[] parameterTypes, Object[] parameters)
  5. invokeDeclaredMethodWithAnnotation(Class annotationClass, Object target)
  6. invokedMethod(java.lang.Object closure, java.lang.Class targetClass, java.lang.String targetMethod)
  7. invokeExactField(Object object, String fieldName)
  8. invokeExactMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)
  9. invokeFunction(Object iFunction, String funcName, String param)