public class

ReflectiveMethod<T>

extends java.lang.Object

 java.lang.Object

↳androidx.test.internal.platform.reflect.ReflectiveMethod<T>

Gradle dependencies

compile group: 'androidx.test', name: 'monitor', version: '1.6.0-alpha03'

  • groupId: androidx.test
  • artifactId: monitor
  • version: 1.6.0-alpha03

Artifact androidx.test:monitor:1.6.0-alpha03 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test:monitor com.android.support.test:monitor

Overview

Helper class for making more performant reflection method invocations.

Lazy initializes and caches Method object to attempt to reduce reflection overhead.

Summary

Constructors
publicReflectiveMethod(java.lang.String className, java.lang.String methodName, java.lang.Class<java.lang.Object> paramTypes[])

Creates a ReflectiveMethod.

Methods
public java.lang.Objectinvoke(java.lang.Object object, java.lang.Object paramValues[])

Invoke the instance method.

public java.lang.ObjectinvokeStatic(java.lang.Object paramValues[])

Invoke th static method.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public ReflectiveMethod(java.lang.String className, java.lang.String methodName, java.lang.Class<java.lang.Object> paramTypes[])

Creates a ReflectiveMethod.

Parameters:

className: the fully qualified class name that defines the method
methodName: the method name to call
paramTypes: the list of types of the method parameters, in order.

Methods

public java.lang.Object invoke(java.lang.Object object, java.lang.Object paramValues[])

Invoke the instance method.

See invoke

Parameters:

object: the object the underlying method is invoked from
paramValues: the arguments used for the method call

Returns:

the return value of the method

public java.lang.Object invokeStatic(java.lang.Object paramValues[])

Invoke th static method.

See invoke

Parameters:

paramValues: the arguments used for the method call

Returns:

the return value of the method

Source

/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * 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.
 */
package androidx.test.internal.platform.reflect;

import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Helper class for making more performant reflection method invocations.
 *
 * <p>Lazy initializes and caches Method object to attempt to reduce reflection overhead.
 *
 * @hide
 */
@RestrictTo(Scope.LIBRARY_GROUP)
public class ReflectiveMethod<T> {
  private final String className;
  private final String methodName;
  private final Class<?>[] paramTypes;

  // lazy init
  private boolean initialized = false;
  private Method method;

  /**
   * Creates a ReflectiveMethod.
   *
   * @param className the fully qualified class name that defines the method
   * @param methodName the method name to call
   * @param paramTypes the list of types of the method parameters, in order.
   */
  public ReflectiveMethod(String className, String methodName, Class<?>... paramTypes) {
    this.className = className;
    this.paramTypes = paramTypes;
    this.methodName = methodName;
  }

  /**
   * Invoke the instance method.
   *
   * <p>See {@link java.lang.reflect.Method#invoke(Object, Object...)}
   *
   * @param object the object the underlying method is invoked from
   * @param paramValues the arguments used for the method call
   * @return the return value of the method
   * @throws ReflectionException if call could not be completed
   */
  public T invoke(Object object, Object... paramValues) throws ReflectionException {
    try {
      initIfNecessary();
      return (T) method.invoke(object, paramValues);
    } catch (ClassNotFoundException
        | InvocationTargetException
        | IllegalAccessException
        | NoSuchMethodException e) {
      throw new ReflectionException(e);
    }
  }

  /**
   * Invoke th static method.
   *
   * <p>See {@link java.lang.reflect.Method#invoke(Object, Object...)}
   *
   * @param paramValues the arguments used for the method call
   * @return the return value of the method
   * @throws ReflectionException if call could not be completed
   */
  public T invokeStatic(Object... paramValues) throws ReflectionException {
    return invoke(null, paramValues);
  }

  private synchronized void initIfNecessary() throws ClassNotFoundException, NoSuchMethodException {
    if (initialized) {
      return;
    }
    method = Class.forName(className).getDeclaredMethod(methodName, paramTypes);
    method.setAccessible(true);
    initialized = true;
  }
}