public class

ReflectiveField<T>

extends java.lang.Object

 java.lang.Object

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

Gradle dependencies

compile group: 'androidx.test', name: 'orchestrator', version: '1.5.0'

  • groupId: androidx.test
  • artifactId: orchestrator
  • version: 1.5.0

Artifact androidx.test:orchestrator:1.5.0 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test:orchestrator com.android.support.test:orchestrator

Overview

Helper class for making more performant reflection field access.

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

Summary

Constructors
publicReflectiveField(java.lang.Class<java.lang.Object> clazz, java.lang.String fieldName)

publicReflectiveField(java.lang.String className, java.lang.String fieldName)

Creates a ReflectiveField.

Methods
public java.lang.Objectget(java.lang.Object object)

Retrieves the field's value, initializing if necessary.

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

Constructors

public ReflectiveField(java.lang.String className, java.lang.String fieldName)

Creates a ReflectiveField.

Parameters:

className: the fully qualified class name that defines the field
fieldName: the field name

public ReflectiveField(java.lang.Class<java.lang.Object> clazz, java.lang.String fieldName)

Methods

public java.lang.Object get(java.lang.Object object)

Retrieves the field's value, initializing if necessary.

Parameters:

object: the object that holds the field's value

Returns:

the field's value

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.Field;

/**
 * Helper class for making more performant reflection field access.
 *
 * <p>Lazy initializes and caches Method object ro attempt to reduce reflection overhead.
 *
 * @hide
 */
@RestrictTo(Scope.LIBRARY_GROUP)
public class ReflectiveField<T> {
  private final String className;
  private final String fieldName;
  private final Class<?> clazz;

  // lazy init
  private boolean initialized = false;
  private Field field;

  /**
   * Creates a ReflectiveField.
   *
   * @param className the fully qualified class name that defines the field
   * @param fieldName the field name
   */
  public ReflectiveField(String className, String fieldName) {
    this.clazz = null;
    this.className = className;
    this.fieldName = fieldName;
  }

  public ReflectiveField(Class<?> clazz, String fieldName) {
    this.clazz = clazz;
    this.className = null;
    this.fieldName = fieldName;
  }

  /**
   * Retrieves the field's value, initializing if necessary.
   *
   * @param object the object that holds the field's value
   * @return the field's value
   * @throws ReflectionException if field could not be accessed
   */
  public T get(Object object) throws ReflectionException {
    try {
      initIfNecessary();
      return (T) field.get(object);
    } catch (ClassNotFoundException e) {
      throw new ReflectionException(e);
    } catch (NoSuchFieldException e) {
      throw new ReflectionException(e);
    } catch (IllegalAccessException e) {
      throw new ReflectionException(e);
    }
  }

  private synchronized void initIfNecessary() throws ClassNotFoundException, NoSuchFieldException {
    if (initialized) {
      return;
    }
    field = getClazz().getDeclaredField(fieldName);
    field.setAccessible(true);
    initialized = true;
  }

  private Class<?> getClazz() throws ClassNotFoundException {
    if (clazz == null) {
      return Class.forName(className);
    } else {
      return clazz;
    }
  }
}