org.robolectric.util.ReflectionHelpers.getStaticField()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(61)

本文整理了Java中org.robolectric.util.ReflectionHelpers.getStaticField方法的一些代码示例,展示了ReflectionHelpers.getStaticField的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ReflectionHelpers.getStaticField方法的具体详情如下:
包路径:org.robolectric.util.ReflectionHelpers
类名称:ReflectionHelpers
方法名:getStaticField

ReflectionHelpers.getStaticField介绍

[英]Reflectively get the value of a static field.
[中]反射式获取静态场的值。

代码示例

代码示例来源:origin: robolectric/robolectric

public static boolean isValidRingerMode(int ringerMode) {
 return ringerMode >= 0
   && ringerMode
     <= (int) ReflectionHelpers.getStaticField(AudioManager.class, "RINGER_MODE_MAX");
}

代码示例来源:origin: robolectric/robolectric

private static int getActionTypeMaskFromFramework() {
 // Get the mask to determine whether an int is a legit ID for an action, defined by Android
 return (int)ReflectionHelpers.getStaticField(AccessibilityNodeInfo.class, "ACTION_TYPE_MASK");
}

代码示例来源:origin: robolectric/robolectric

private static int getLastLegacyActionFromFrameWork() {
 return (int)ReflectionHelpers.getStaticField(AccessibilityNodeInfo.class, "LAST_LEGACY_STANDARD_ACTION");
}

代码示例来源:origin: robolectric/robolectric

/**
 * Reflectively get the value of a static field.
 *
 * @param clazz Target class.
 * @param fieldName The field name.
 * @param <R> The return type.
 * @return Value of the field.
 */
public static <R> R getStaticField(Class<?> clazz, String fieldName) {
 try {
  return getStaticField(clazz.getDeclaredField(fieldName));
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: robolectric/robolectric

/** Adds a USB port to UsbManager. */
public void addPort(String portId) {
 usbPorts.put(
   callConstructor(UsbPort.class,
     from(String.class, portId),
     from(int.class, getStaticField(UsbPort.class, "MODE_DUAL"))),
   new UsbPortStatus(
     getStaticField(UsbPort.class, "MODE_DUAL"),
     getStaticField(UsbPort.class, "POWER_ROLE_SINK"),
     getStaticField(UsbPort.class, "DATA_ROLE_DEVICE"),
     0));
}

代码示例来源:origin: robolectric/robolectric

@Implementation
protected void __constructor__(int id, CharSequence label) {
 if (((id & (int)ReflectionHelpers.getStaticField(AccessibilityNodeInfo.class, "ACTION_TYPE_MASK")) == 0) && Integer.bitCount(id) != 1) {
  throw new IllegalArgumentException("Invalid standard action id");
 }
 this.id = id;
 this.label = label;
}

代码示例来源:origin: robolectric/robolectric

private static RouteInfo getBluetoothA2dpRoute() {
 return ReflectionHelpers.getField(
   ReflectionHelpers.getStaticField(MediaRouter.class, "sStatic"), "mBluetoothA2dpRoute");
}

代码示例来源:origin: robolectric/robolectric

@Resetter
public static void reset() {
 if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP_MR1) {
  Map<String, Object> sSubInstances =
    ReflectionHelpers.getStaticField(SmsManager.class, "sSubInstances");
  sSubInstances.clear();
 }
}

代码示例来源:origin: robolectric/robolectric

@Resetter
public static void reset() {
 /* ValueAnimator.sAnimationHandler is a static thread local that otherwise would survive between
  * tests. The AnimationHandler.mAnimationScheduled is set to true when the scheduleAnimation() is
  * called and the reset to false when run() is called by the Choreographer. If an animation is
  * already scheduled, it will not post to the Choreographer. This is a problem if a previous
  * test leaves animations on the Choreographers callback queue without running them as it will
  * cause the AnimationHandler not to post a callback. We reset the thread local here so a new
  * one will be created for each test with a fresh state.
  */
 if (RuntimeEnvironment.getApiLevel() >= N) {
  ThreadLocal<AnimationHandler> animatorHandlerTL =
    ReflectionHelpers.getStaticField(AnimationHandler.class, "sAnimatorHandler");
  animatorHandlerTL.remove();
 } else {
  ReflectionHelpers.callStaticMethod(ValueAnimator.class, "clearAllAnimations");
  ThreadLocal<AnimationHandler> animatorHandlerTL =
    ReflectionHelpers.getStaticField(ValueAnimator.class, "sAnimationHandler");
  animatorHandlerTL.remove();
 }
}

代码示例来源:origin: robolectric/robolectric

private static void resetStateApi26() {
 Handler queuedWorkHandler = ReflectionHelpers.getStaticField(QueuedWork.class, "sHandler");
 if (queuedWorkHandler != null) {
  queuedWorkHandler.removeCallbacksAndMessages(null);
 }
 _QueuedWork_ _queuedWorkStatic_ = reflector(_QueuedWork_.class);
 _queuedWorkStatic_.getFinishers().clear();
 _queuedWorkStatic_.getWork().clear();
 _queuedWorkStatic_.setNumWaits(0);
}

代码示例来源:origin: robolectric/robolectric

@Resetter
 public static void reset() {
  defaultDisplayJB = null;
  views.clear();
  if (RuntimeEnvironment.getApiLevel() <= VERSION_CODES.JELLY_BEAN) {
   ReflectionHelpers.setStaticField(
     WindowManagerImpl.class,
     "sWindowManager",
     ReflectionHelpers.newInstance(WindowManagerImpl.class));
   HashMap windowManagers =
     ReflectionHelpers.getStaticField(WindowManagerImpl.class, "sCompatWindowManagers");
   windowManagers.clear();
  }
 }
}

代码示例来源:origin: robolectric/robolectric

private void callUpdateAudioRoutes(AudioRoutesInfo routesInfo) {
 ReflectionHelpers.callInstanceMethod(
   ReflectionHelpers.getStaticField(MediaRouter.class, "sStatic"),
   RuntimeEnvironment.getApiLevel() <= JELLY_BEAN
     ? "updateRoutes"
     : "updateAudioRoutes",
   ClassParameter.from(AudioRoutesInfo.class, routesInfo));
}

代码示例来源:origin: robolectric/robolectric

@Test
public void getStaticFieldReflectively_withField_getsStaticField() throws Exception {
 Field field = ExampleDescendant.class.getDeclaredField("DESCENDANT");
 int result = ReflectionHelpers.getStaticField(field);
 assertThat(result).isEqualTo(6);
}

代码示例来源:origin: robolectric/robolectric

/**
 * Int version of {@link #setMode(String, int, String, int)}.
 *
 * <p>This method is public for testing {@link #checkOpNoThrow}. If {@link #checkOpNoThrow} is *
 * called afterwards with the {@code op}, {@code ui}, and {@code packageName} provided, it will *
 * return the {@code mode} set here.
 */
@Implementation(minSdk = KITKAT)
@HiddenApi
@RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
public void setMode(int op, int uid, String packageName, int mode) {
 Integer oldMode = appModeMap.put(getOpMapKey(uid, packageName, op), mode);
 OnOpChangedListener listener = appOpListeners.get(getListenerKey(op, packageName));
 if (listener != null && !Objects.equals(oldMode, mode)) {
  String[] sOpToString = ReflectionHelpers.getStaticField(AppOpsManager.class, "sOpToString");
  listener.onOpChanged(sOpToString[op], packageName);
 }
}

代码示例来源:origin: robolectric/robolectric

private SQLiteConnection getSQLiteConnection(SQLiteDatabase database) {
  ptr = ShadowSQLiteConnection.nativeOpen(databasePath.getPath(), 0, "test connection", false, false).longValue();
  CONNECTIONS = ReflectionHelpers.getStaticField(ShadowSQLiteConnection.class, "CONNECTIONS");
  return CONNECTIONS.getConnection(ptr);
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void getStaticFieldReflectively_withFieldName_getsStaticField() {
 assertThat((int) ReflectionHelpers.getStaticField(ExampleDescendant.class, "DESCENDANT"))
   .isEqualTo(6);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = M, maxSdk = P)
public void setPortRoles_sinkHost_shouldSetPortStatus() {
 final int powerRoleSink = getStaticField(UsbPort.class, "POWER_ROLE_SINK");
 final int dataRoleHost = getStaticField(UsbPort.class, "DATA_ROLE_HOST");
 shadowOf(usbManager).addPort("port1");
 List<UsbPort> usbPorts = getUsbPorts();
 _usbManager_().setPortRoles(usbPorts.get(0), powerRoleSink, dataRoleHost);
 UsbPortStatus usbPortStatus = _usbManager_().getPortStatus(usbPorts.get(0));
 assertThat(usbPortStatus.getCurrentPowerRole()).isEqualTo(powerRoleSink);
 assertThat(usbPortStatus.getCurrentDataRole()).isEqualTo(dataRoleHost);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void setStaticFieldReflectively_withField_setsStaticFields() throws Exception {
 Field field = ExampleDescendant.class.getDeclaredField("DESCENDANT");
 int startingValue = ReflectionHelpers.getStaticField(field);
 ReflectionHelpers.setStaticField(field, 7);
 assertThat(startingValue).named("startingValue").isEqualTo(6);
 assertThat(ExampleDescendant.DESCENDANT).named("DESCENDENT").isEqualTo(7);
 /// Reset the value to avoid test pollution
 ReflectionHelpers.setStaticField(field, startingValue);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testSetDataSourceExceptionWithWrongExceptionTypeAsserts() {
 boolean fail = false;
 Map<DataSource,Exception> exceptions = ReflectionHelpers.getStaticField(ShadowMediaPlayer.class, "exceptions");
 DataSource ds = toDataSource("dummy");
 Exception e = new CloneNotSupportedException(); // just a convenient, non-RuntimeException in java.lang
 exceptions.put(ds, e);
 try {
  shadowMediaPlayer.setDataSource(ds);
  fail = true;
 } catch (AssertionError a) {
 } catch (IOException ioe) {
  fail("Got exception <" + ioe + ">; expecting assertion");
 }
 if (fail) {
  fail("setDataSource() should assert with non-IOException,non-RuntimeException");
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void setStaticFieldReflectively_withFieldName_setsStaticFields() {
 int startingValue = ReflectionHelpers.getStaticField(ExampleDescendant.class, "DESCENDANT");
 ReflectionHelpers.setStaticField(ExampleDescendant.class, "DESCENDANT", 7);
 assertThat(startingValue).named("startingValue").isEqualTo(6);
 assertThat(ExampleDescendant.DESCENDANT).named("DESCENDENT").isEqualTo(7);
 // Reset the value to avoid test pollution
 ReflectionHelpers.setStaticField(ExampleDescendant.class, "DESCENDANT", startingValue);
}

相关文章