BoundaryInterfaceReflectionUtil

public class BoundaryInterfaceReflectionUtil


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

Summary

Public methods

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

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

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

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

static @NonNull boolean
containsFeature(
    @NonNull Array<@NonNull String> features,
    @NonNull String soughtFeature
)
static @RequiresApi(value = Build.VERSION_CODES.KITKAT) @Nullable InvocationHandler

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

static @RequiresApi(value = Build.VERSION_CODES.KITKAT) @Nullable Array<@NonNull InvocationHandler>

Plural version of createInvocationHandlerFor.

static @NonNull Method
dupeMethod(@NonNull Method method, @NonNull 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

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 @NonNull boolean

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

Public methods

castToSuppLibClass

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

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

A {@code null}InvocationHandler is treated as representing a {@code null} object.

Parameters
@NonNull Class<@NonNull T> clazz

a Class object representing the desired boundary interface.

@Nullable InvocationHandler invocationHandler

an InvocationHandler compatible with this boundary interface.

containsFeature

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

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

containsFeature

@NonNull
public static boolean containsFeature(
    @NonNull Array<@NonNull String> features,
    @NonNull String soughtFeature
)

createInvocationHandlerFor

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

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

A {@code null} delegate is represented with a {@code null}InvocationHandler.

Parameters
@Nullable Object delegate

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

Returns
@RequiresApi(value = Build.VERSION_CODES.KITKAT) InvocationHandler

an InvocationHandlerWithDelegateGetter wrapping {@code delegate}

createInvocationHandlersForArray

@RequiresApi(value = Build.VERSION_CODES.KITKAT)
@Nullable
@NonNull
public static @RequiresApi(value = Build.VERSION_CODES.KITKAT) Array<@NonNull InvocationHandlercreateInvocationHandlersForArray(
    @Nullable Array<@NonNull Object> delegates
)

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

A {@code null} array of delegates is represented with a {@code null} array of s. Any individual {@code null} delegate is represented with a {@code null}InvocationHandler.

Parameters
@Nullable Array<@NonNull Object> delegates

an array of objects to which to delegate.

Returns
@RequiresApi(value = Build.VERSION_CODES.KITKAT) Array<@NonNull InvocationHandler>

an array of InvocationHandlerWithDelegateGetter instances, each delegating to the corresponding member of {@code delegates}.

dupeMethod

@NonNull
public static Method dupeMethod(@NonNull Method method, @NonNull 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

@Nullable
@NonNull
public static 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 {@code null}InvocationHandler is treated as wrapping a {@code null} delegate.

Parameters
@Nullable InvocationHandler invocationHandler

a Nullable InvocationHandlerWithDelegateGetter.

Returns
Object

the corresponding delegate.

instanceOfInOwnClassLoader

@NonNull
public static boolean instanceOfInOwnClassLoader(@NonNull Object obj, @NonNull String className)

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