BoundaryInterfaceReflectionUtil

public class BoundaryInterfaceReflectionUtil


A set of utility methods used for calling across the support library boundary.

Summary

Public constructors

Public methods

static @Nullable T
<T> castToSuppLibClass(
    @NonNull Class<T> clazz,
    @Nullable InvocationHandler invocationHandler
)

Returns an implementation of the boundary interface named clazz, by delegating method calls to the InvocationHandler invocationHandler.

static boolean
containsFeature(String[] features, String soughtFeature)
static boolean
containsFeature(Collection<String> features, String soughtFeature)

Check whether a set of features {@param features} contains a certain feature {@param * soughtFeature}.

static @Nullable InvocationHandler
@RequiresApi(value = Build.VERSION_CODES.KITKAT)
createInvocationHandlerFor(@Nullable Object delegate)

Create an InvocationHandler that delegates method calls to delegate, making sure that the Method and parameters being passed exist in the same as delegate.

static @Nullable InvocationHandler[]
@RequiresApi(value = Build.VERSION_CODES.KITKAT)
createInvocationHandlersForArray(@Nullable Object[] delegates)

Plural version of createInvocationHandlerFor.

static Method
dupeMethod(Method method, ClassLoader delegateLoader)

Utility method for fetching a method from {@param delegateLoader}, with the same signature (package + class + method name + parameters) as a given method defined in another classloader.

static @Nullable Object
@RequiresApi(value = Build.VERSION_CODES.KITKAT)
getDelegateFromInvocationHandler(
    @Nullable InvocationHandler invocationHandler
)

Assuming that the given InvocationHandler was created in the current classloader and is an InvocationHandlerWithDelegateGetter, return the object the InvocationHandler delegates its method calls to.

static boolean

Check if an object is an instance of className, resolving className in the object's own ClassLoader.

Public constructors

BoundaryInterfaceReflectionUtil

public BoundaryInterfaceReflectionUtil()

Public methods

castToSuppLibClass

public static @Nullable T <T> castToSuppLibClass(
    @NonNull Class<T> clazz,
    @Nullable InvocationHandler invocationHandler
)

Returns an implementation of the boundary interface named clazz, by delegating method calls to the InvocationHandler invocationHandler.

A nullInvocationHandler is treated as representing a null object.

Parameters
@NonNull Class<T> clazz

a Class object representing the desired boundary interface.

@Nullable InvocationHandler invocationHandler

an InvocationHandler compatible with this boundary interface.

containsFeature

public static boolean containsFeature(String[] features, String soughtFeature)

containsFeature

public static boolean containsFeature(Collection<String> features, String soughtFeature)

Check whether a set of features {@param features} contains a certain feature {@param * soughtFeature}.

createInvocationHandlerFor

@RequiresApi(value = Build.VERSION_CODES.KITKAT)
public static @Nullable InvocationHandler createInvocationHandlerFor(@Nullable Object delegate)

Create an InvocationHandler that delegates method calls to delegate, making sure that the Method and parameters being passed exist in the same as delegate.

A null delegate is represented with a nullInvocationHandler.

Parameters
@Nullable Object delegate

the object which the resulting InvocationHandler should delegate method calls to.

Returns
@Nullable InvocationHandler

an InvocationHandlerWithDelegateGetter wrapping delegate

createInvocationHandlersForArray

@RequiresApi(value = Build.VERSION_CODES.KITKAT)
public static @Nullable InvocationHandler[] createInvocationHandlersForArray(@Nullable Object[] delegates)

Plural version of createInvocationHandlerFor. The resulting array will be the same length as delegates, where the nth InvocationHandler wraps the nth delegate object.

A null array of delegates is represented with a null array of s. Any individual null delegate is represented with a nullInvocationHandler.

Parameters
@Nullable Object[] delegates

an array of objects to which to delegate.

Returns
@Nullable InvocationHandler[]

an array of InvocationHandlerWithDelegateGetter instances, each delegating to the corresponding member of delegates.

dupeMethod

public static Method dupeMethod(Method method, ClassLoader delegateLoader)

Utility method for fetching a method from {@param delegateLoader}, with the same signature (package + class + method name + parameters) as a given method defined in another classloader.

getDelegateFromInvocationHandler

@RequiresApi(value = Build.VERSION_CODES.KITKAT)
public static @Nullable Object getDelegateFromInvocationHandler(
    @Nullable InvocationHandler invocationHandler
)

Assuming that the given InvocationHandler was created in the current classloader and is an InvocationHandlerWithDelegateGetter, return the object the InvocationHandler delegates its method calls to.

A nullInvocationHandler is treated as wrapping a null delegate.

Parameters
@Nullable InvocationHandler invocationHandler

a Nullable InvocationHandlerWithDelegateGetter.

Returns
@Nullable Object

the corresponding delegate.

instanceOfInOwnClassLoader

public static boolean instanceOfInOwnClassLoader(Object obj, String className)

Check if an object is an instance of className, resolving className in the object's own ClassLoader. This is useful when obj may have been created in a ClassLoader other than the current one (in which case obj instanceof Foo would fail but instanceOfInOwnClassLoader(obj, "Foo") may succeed).