Top Description Fields Constructors Methods
com.sun.jna.internal

public Class ReflectionUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Constructor, .InvocationTargetException, .Method, java.util.Arrays, java.util.logging.Level, .Logger

Helper class to invoke default method reflectively.

This class is intented to be used only be JNA itself.

This implementation is inspired by: Correct Reflective Access to Interface Default Methods in Java 8, 9, 10

Field Summary

Modifier and TypeField and Description
private static Constructor<T>
private static final Logger
private static final Method
private static final Method
private static final Method
private static final Method
private static final Method
private static final Method
private static final Method
private static final Method
private static final Method

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static Object
private static Object
private static Constructor<T>
public static Object

Returns:

method handle that can be used to invoke the supplied method
getMethodHandle
(Method method)

Retrieve the method handle, that can be used to invoke the provided method.

public static Object

Returns:

result of the invokation
invokeDefaultMethod
(Object
object to invoke the supplied method handle on
target
,
Object methodHandle, Object... args)

Invokes a default method reflectively.

public static boolean

Returns:

true if JVM supports default methods and method is a default method
isDefault
(Method method)

Check if the supplied method object represents a default method.

private static Class<T>
private static Constructor<T>
lookupDeclaredConstructor(Class<T> clazz, Class<T>... arguments)

private static Method
lookupMethod(Class<T> clazz, String methodName, Class<T>... arguments)

private static Object
mhViaFindSpecial(Object lookup, Method method)

private static Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONSTRUCTOR_LOOKUP_CLASSback to summary
private static Constructor<T> CONSTRUCTOR_LOOKUP_CLASS
LOGback to summary
private static final Logger LOG
METHOD_HANDLES_BIND_TOback to summary
private static final Method METHOD_HANDLES_BIND_TO
METHOD_HANDLES_INVOKE_WITH_ARGUMENTSback to summary
private static final Method METHOD_HANDLES_INVOKE_WITH_ARGUMENTS
METHOD_HANDLES_LOOKUPback to summary
private static final Method METHOD_HANDLES_LOOKUP
METHOD_HANDLES_LOOKUP_FIND_SPECIALback to summary
private static final Method METHOD_HANDLES_LOOKUP_FIND_SPECIAL
METHOD_HANDLES_LOOKUP_INback to summary
private static final Method METHOD_HANDLES_LOOKUP_IN
METHOD_HANDLES_LOOKUP_UNREFLECT_SPECIALback to summary
private static final Method METHOD_HANDLES_LOOKUP_UNREFLECT_SPECIAL
METHOD_HANDLES_PRIVATE_LOOKUP_INback to summary
private static final Method METHOD_HANDLES_PRIVATE_LOOKUP_IN
METHOD_IS_DEFAULTback to summary
private static final Method METHOD_IS_DEFAULT
METHOD_TYPEback to summary
private static final Method METHOD_TYPE

Constructor Detail

ReflectionUtilsback to summary
public ReflectionUtils()

Method Detail

createLookupback to summary
private static Object createLookup() throws Exception
createPrivateLookupInback to summary
private static Object createPrivateLookupIn(Class<T> type, Object lookup) throws Exception
getConstructorLookupClassback to summary
private static Constructor<T> getConstructorLookupClass()
getMethodHandleback to summary
public static Object getMethodHandle(Method method) throws Exception

Retrieve the method handle, that can be used to invoke the provided method. It is only intended to be used to call default methods on interfaces.

Returns:Object

method handle that can be used to invoke the supplied method

invokeDefaultMethodback to summary
public static Object invokeDefaultMethod(Object target, Object methodHandle, Object... args) throws Throwable

Invokes a default method reflectively. The method must be called with the method handle for a default method on an interfaces.

Parameters
target:Object

object to invoke the supplied method handle on

methodHandle:Object

retrieved via getMethodHandle(java.lang.reflect.Method)

Returns:Object

result of the invokation

isDefaultback to summary
public static boolean isDefault(Method method)

Check if the supplied method object represents a default method.

This is the reflective equivalent of method.isDefault().

Returns:boolean

true if JVM supports default methods and method is a default method

lookupClassback to summary
private static Class<T> lookupClass(String name)
lookupDeclaredConstructorback to summary
private static Constructor<T> lookupDeclaredConstructor(Class<T> clazz, Class<T>... arguments)
lookupMethodback to summary
private static Method lookupMethod(Class<T> clazz, String methodName, Class<T>... arguments)
mhViaFindSpecialback to summary
private static Object mhViaFindSpecial(Object lookup, Method method) throws Exception
mhViaUnreflectSpecialback to summary
private static Object mhViaUnreflectSpecial(Object lookup, Method method) throws Exception