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

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

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

ReflectionHelpers.loadClass介绍

[英]Load a class.
[中]加载一个类。

代码示例

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

@Implementation
 protected static LayoutInflater makeNewLayoutInflater(Context context) {
  Class<LayoutInflater> phoneLayoutInflaterClass =
    (Class<LayoutInflater>)
      ReflectionHelpers.loadClass(
        ShadowPolicyManager.class.getClassLoader(),
        "com.android.internal.policy.impl.PhoneLayoutInflater");
  return ReflectionHelpers.callConstructor(
    phoneLayoutInflaterClass, ClassParameter.from(Context.class, context));
 }
}

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

Class<?> clazz = ReflectionHelpers.loadClass(RobolectricTestRunner.class.getClassLoader(),
  "org.robolectric.plugins.CachedMavenDependencyResolver");
dependencyResolver = (DependencyResolver) ReflectionHelpers.callConstructor(clazz);

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

@Implementation
public static Object stat(String path) throws ErrnoException {
 if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.LOLLIPOP) {
  return new StructStat(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 } else {
  return ReflectionHelpers.newInstance(ReflectionHelpers.loadClass(ShadowPosix.class.getClassLoader(), "libcore.io.StructStat"));
 }
}

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

@Resetter
public static void reset() {
 String prefsCacheFieldName =
   RuntimeEnvironment.getApiLevel() >= N ? "sSharedPrefsCache" : "sSharedPrefs";
 Object prefsDefaultValue = RuntimeEnvironment.getApiLevel() >= KITKAT ? null : new HashMap<>();
 Class<?> contextImplClass =
   ReflectionHelpers.loadClass(
     ShadowContextImpl.class.getClassLoader(), "android.app.ContextImpl");
 ReflectionHelpers.setStaticField(contextImplClass, prefsCacheFieldName, prefsDefaultValue);
 if (RuntimeEnvironment.getApiLevel() <= VERSION_CODES.LOLLIPOP_MR1) {
  HashMap<String, Object> fetchers =
    ReflectionHelpers.getStaticField(contextImplClass, "SYSTEM_SERVICE_MAP");
  Class staticServiceFetcherClass =
    ReflectionHelpers.loadClass(
      ShadowContextImpl.class.getClassLoader(),
      "android.app.ContextImpl$StaticServiceFetcher");
  for (Object o : fetchers.values()) {
   if (staticServiceFetcherClass.isInstance(o)) {
    ReflectionHelpers.setField(staticServiceFetcherClass, o, "mCachedInstance", null);
   }
  }
  if (RuntimeEnvironment.getApiLevel() >= KITKAT) {
   Object windowServiceFetcher = fetchers.get(Context.WINDOW_SERVICE);
   ReflectionHelpers.setField(
     windowServiceFetcher.getClass(), windowServiceFetcher, "mDefaultDisplay", null);
  }
 }
}

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

/** internal only */
public static void configureDefaultDisplayForJBOnly(
  Configuration configuration, DisplayMetrics displayMetrics) {
 Class<?> arg2Type = ReflectionHelpers.loadClass(ShadowWindowManagerImpl.class.getClassLoader(),
   "android.view.CompatibilityInfoHolder");
 defaultDisplayJB = ReflectionHelpers.callConstructor(Display.class,
   ClassParameter.from(int.class, 0),
   ClassParameter.from(arg2Type, null));
 ShadowDisplay shadowDisplay = Shadow.extract(defaultDisplayJB);
 shadowDisplay.configureForJBOnly(configuration, displayMetrics);
}

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

private static MethodHandle getMethodHandle(String className, String methodName, MethodType type) {
 Interceptor interceptor = INTERCEPTORS.findInterceptor(className, methodName);
 if (interceptor != null) {
  try {
   // reload interceptor in sandbox...
   Class<Interceptor> theClass =
     (Class<Interceptor>) ReflectionHelpers.loadClass(
       RobolectricInternals.getClassLoader(),
       interceptor.getClass().getName()).asSubclass(Interceptor.class);
   return ReflectionHelpers.newInstance(theClass).getMethodHandle(methodName, type);
  } catch (NoSuchMethodException | IllegalAccessException e) {
   throw new RuntimeException(e);
  }
 }
 if (type.parameterCount() != 0) {
  return dropArguments(NOTHING, 0, type.parameterArray());
 } else {
  return NOTHING;
 }
}

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

private Class<?> loadClass(Class<?> clazz) throws ClassNotFoundException {
  if (classLoader == null) {
   classLoader = new SandboxClassLoader(configureBuilder().build());
  }

  setStaticField(classLoader.loadClass(InvokeDynamicSupport.class.getName()), "INTERCEPTORS",
    new Interceptors(Collections.<Interceptor>emptyList()));
  setStaticField(classLoader.loadClass(Shadow.class.getName()), "SHADOW_IMPL",
    newInstance(classLoader.loadClass(ShadowImpl.class.getName())));

  ShadowInvalidator invalidator = Mockito.mock(ShadowInvalidator.class);
  when(invalidator.getSwitchPoint(any(Class.class))).thenReturn(new SwitchPoint());

  String className = RobolectricInternals.class.getName();
  Class<?> robolectricInternalsClass = ReflectionHelpers.loadClass(classLoader, className);
  ReflectionHelpers.setStaticField(robolectricInternalsClass, "classHandler", classHandler);
  ReflectionHelpers.setStaticField(robolectricInternalsClass, "shadowInvalidator", invalidator);

  return classLoader.loadClass(clazz.getName());
 }
}

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

@Override
public AttributeSet build() {
 Class<?> xmlBlockClass = ReflectionHelpers
   .loadClass(this.getClass().getClassLoader(), "android.content.res.XmlBlock");

代码示例来源:origin: hidroh/materialistic

private void resetWindowManager() {
    Class clazz = ReflectionHelpers.loadClass(getClass().getClassLoader(), "android.view.WindowManagerGlobal");
    Object instance = ReflectionHelpers.callStaticMethod(clazz, "getInstance");

    // We essentially duplicate what's in {@link WindowManagerGlobal#closeAll} with what's below.
    // The closeAll method has a bit of a bug where it's iterating through the "roots" but
    // bases the number of objects to iterate through by the number of "views." This can result in
    // an {@link java.lang.IndexOutOfBoundsException} being thrown.
    Object lock = ReflectionHelpers.getField(instance, "mLock");

    ArrayList<Object> roots = ReflectionHelpers.getField(instance, "mRoots");
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (lock) {
      for (int i = 0; i < roots.size(); i++) {
        ReflectionHelpers.callInstanceMethod(instance, "removeViewLocked",
            ReflectionHelpers.ClassParameter.from(int.class, i),
            ReflectionHelpers.ClassParameter.from(boolean.class, false));
      }
    }

    // Views will still be held by this array. We need to clear it out to ensure
    // everything is released.
    Collection<View> dyingViews = ReflectionHelpers.getField(instance, "mDyingViews");
    dyingViews.clear();

  }
}

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

Class<?> mavenDependencyResolverClass = ReflectionHelpers.loadClass(RobolectricTestRunner.class.getClassLoader(),
  "org.robolectric.internal.dependency.MavenDependencyResolver");
DependencyResolver dependencyResolver = (DependencyResolver) ReflectionHelpers.callConstructor(mavenDependencyResolverClass);

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

@Override
public AttributeSet build() {
 Class<?> xmlBlockClass = ReflectionHelpers
   .loadClass(this.getClass().getClassLoader(), "android.content.res.XmlBlock");

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

ReflectionHelpers.loadClass(
  getClass().getClassLoader(), ShadowContextImpl.CLASS_NAME);

代码示例来源:origin: robospock/RoboSpock

public static void injectClassHandler(ClassLoader robolectricClassLoader, ClassHandler classHandler) {
  String className = RobolectricInternals.class.getName();
  Class<?> robolectricInternalsClass = ReflectionHelpers.loadClass(robolectricClassLoader, className);
  ReflectionHelpers.setStaticField(robolectricInternalsClass, "classHandler", classHandler);
}

代码示例来源:origin: org.robolectric/shadows-framework

@Implementation
 protected static LayoutInflater makeNewLayoutInflater(Context context) {
  Class<LayoutInflater> phoneLayoutInflaterClass =
    (Class<LayoutInflater>)
      ReflectionHelpers.loadClass(
        ShadowPolicyManager.class.getClassLoader(),
        "com.android.internal.policy.impl.PhoneLayoutInflater");
  return ReflectionHelpers.callConstructor(
    phoneLayoutInflaterClass, ClassParameter.from(Context.class, context));
 }
}

代码示例来源:origin: org.robolectric/framework

@Resetter
 public static void reset() {
  String prefsCacheFieldName = RuntimeEnvironment.getApiLevel() >= N ? "sSharedPrefsCache" : "sSharedPrefs";
  Object prefsDefaultValue = RuntimeEnvironment.getApiLevel() >= KITKAT ? null : new HashMap<>();
  Class<?> contextImplClass = ReflectionHelpers.loadClass(ShadowContextImpl.class.getClassLoader(), "android.app.ContextImpl");
  ReflectionHelpers.setStaticField(contextImplClass, prefsCacheFieldName, prefsDefaultValue);
 }
}

代码示例来源:origin: org.robolectric/shadows-framework

@Implementation
public static Object stat(String path) throws ErrnoException {
 if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.LOLLIPOP) {
  return new StructStat(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 } else {
  return ReflectionHelpers.newInstance(ReflectionHelpers.loadClass(ShadowPosix.class.getClassLoader(), "libcore.io.StructStat"));
 }
}

代码示例来源:origin: org.robolectric/framework

@Implementation
 public static Object stat(String path) throws ErrnoException {
  if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.LOLLIPOP) {
   return new StructStat(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  } else {
   return ReflectionHelpers.newInstance(ReflectionHelpers.loadClass(ShadowPosix.class.getClassLoader(), "libcore.io.StructStat"));
  }
 }
}

代码示例来源:origin: org.robolectric/shadows-framework

Object prefsDefaultValue = RuntimeEnvironment.getApiLevel() >= KITKAT ? null : new HashMap<>();
Class<?> contextImplClass =
  ReflectionHelpers.loadClass(
    ShadowContextImpl.class.getClassLoader(), "android.app.ContextImpl");
ReflectionHelpers.setStaticField(contextImplClass, prefsCacheFieldName, prefsDefaultValue);
   ReflectionHelpers.getStaticField(contextImplClass, "SYSTEM_SERVICE_MAP");
 Class staticServiceFetcherClass =
   ReflectionHelpers.loadClass(
     ShadowContextImpl.class.getClassLoader(),
     "android.app.ContextImpl$StaticServiceFetcher");
    ReflectionHelpers.loadClass(
      ShadowContextImpl.class.getClassLoader(), "android.app.ContextImpl$ServiceFetcher");

代码示例来源:origin: org.robolectric/shadows-framework

/** internal only */
public static void configureDefaultDisplayForJBOnly(
  Configuration configuration, DisplayMetrics displayMetrics) {
 Class<?> arg2Type = ReflectionHelpers.loadClass(ShadowWindowManagerImpl.class.getClassLoader(),
   "android.view.CompatibilityInfoHolder");
 defaultDisplayJB = ReflectionHelpers.callConstructor(Display.class,
   ClassParameter.from(int.class, 0),
   ClassParameter.from(arg2Type, null));
 ShadowDisplay shadowDisplay = Shadow.extract(defaultDisplayJB);
 shadowDisplay.configureForJBOnly(configuration, displayMetrics);
}

代码示例来源:origin: org.robolectric/robolectric-sandbox

private static MethodHandle getMethodHandle(String className, String methodName, MethodType type) {
 Interceptor interceptor = INTERCEPTORS.findInterceptor(className, methodName);
 if (interceptor != null) {
  try {
   // reload interceptor in sandbox...
   Class<Interceptor> theClass =
     (Class<Interceptor>) ReflectionHelpers.loadClass(
       RobolectricInternals.getClassLoader(),
       interceptor.getClass().getName()).asSubclass(Interceptor.class);
   return ReflectionHelpers.newInstance(theClass).getMethodHandle(methodName, type);
  } catch (NoSuchMethodException | IllegalAccessException e) {
   throw new RuntimeException(e);
  }
 }
 if (type.parameterCount() != 0) {
  return dropArguments(NOTHING, 0, type.parameterArray());
 } else {
  return NOTHING;
 }
}

相关文章