Here you can find the source of invokeDefaultMethod(final Method method, final Object[] args, final Object proxy)
Parameter | Description |
---|---|
method | a parameter |
args | a parameter |
proxy | a parameter |
Parameter | Description |
---|---|
Throwable | (whatever the invoked method throws) |
public static Object invokeDefaultMethod(final Method method, final Object[] args, final Object proxy) throws Throwable
//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); } } }