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

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

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

ReflectionHelpers.newInstance介绍

[英]Create a new instance of a class
[中]创建类的新实例

代码示例

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

/** Add custom preview sizes to supportedPreviewSizes. */
public void addSupportedPreviewSize(int width, int height) {
 Camera.Size newSize = ReflectionHelpers.newInstance(Camera.class).new Size(width, height);
 supportedPreviewSizes.add(newSize);
}

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

@Implementation
protected Channel initialize(
  Context context, Looper looper, WifiP2pManager.ChannelListener listener) {
 handler = new Handler(looper);
 return ReflectionHelpers.newInstance(Channel.class);
}

代码示例来源: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

@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

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

@Implementation
 protected void filter(CharSequence constraint, Filter.FilterListener listener) {
  try {
   Class<?> forName = Class.forName("android.widget.Filter$FilterResults");
   Object filtering;
   try {
    filtering = ReflectionHelpers.callInstanceMethod(realObject, "performFiltering",
      ClassParameter.from(CharSequence.class, constraint));
   } catch (Exception e) {
    e.printStackTrace();
    filtering = ReflectionHelpers.newInstance(forName);
   }

   ReflectionHelpers.callInstanceMethod(realObject, "publishResults",
     ClassParameter.from(CharSequence.class, constraint),
     ClassParameter.from(forName, filtering));

   if (listener != null) {
    int count = filtering == null ? -1 : (int) ReflectionHelpers.getField(filtering, "count");
    listener.onFilterComplete(count);
   }
  } catch (ClassNotFoundException e) {
   throw new RuntimeException("Cannot load android.widget.Filter$FilterResults");
  }
 }
}

代码示例来源: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

roboMethod.testLifecycle = ReflectionHelpers.newInstance(cl);

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

public void configure(ClassHandler classHandler, Interceptors interceptors) {
  this.classHandler = classHandler;

  ClassLoader robolectricClassLoader = getRobolectricClassLoader();
  Class<?> robolectricInternalsClass = bootstrappedClass(RobolectricInternals.class);
  if (InvokeDynamic.ENABLED) {
   ShadowInvalidator invalidator = getShadowInvalidator();
   setStaticField(robolectricInternalsClass, "shadowInvalidator", invalidator);
  }

  setStaticField(robolectricInternalsClass, "classHandler", classHandler);
  setStaticField(robolectricInternalsClass, "classLoader", robolectricClassLoader);

  Class<?> invokeDynamicSupportClass = bootstrappedClass(InvokeDynamicSupport.class);
  setStaticField(invokeDynamicSupportClass, "INTERCEPTORS", interceptors);

  Class<?> shadowClass = bootstrappedClass(Shadow.class);
  setStaticField(shadowClass, "SHADOW_IMPL", newInstance(bootstrappedClass(ShadowImpl.class)));
 }
}

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

final ActivityThread activityThread = ReflectionHelpers.newInstance(ActivityThread.class);
RuntimeEnvironment.setActivityThread(activityThread);
final _ActivityThread_ _activityThread_ = reflector(_ActivityThread_.class, activityThread);
  throw new RuntimeException(e);
 final Object appBindData = ReflectionHelpers.newInstance(appBindDataClass);
 final _AppBindData_ _appBindData_ = reflector(_AppBindData_.class, appBindData);
 _appBindData_.setProcessName(parsedPackage.packageName);

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

roboMethod.testLifecycle = ReflectionHelpers.newInstance(cl);

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

ActivityThread activityThread = ReflectionHelpers.newInstance(ActivityThread.class);
RuntimeEnvironment.setActivityThread(activityThread);
  throw new RuntimeException(e);
 Object data = ReflectionHelpers.newInstance(appBindDataClass);
 ReflectionHelpers.setField(data, "processName", "org.robolectric");
 ReflectionHelpers.setField(data, "appInfo", applicationInfo);

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

/** Add custom preview sizes to supportedPreviewSizes. */
public void addSupportedPreviewSize(int width, int height) {
 Camera.Size newSize = ReflectionHelpers.newInstance(Camera.class).new Size(width, height);
 supportedPreviewSizes.add(newSize);
}

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

@Implementation
protected Channel initialize(
  Context context, Looper looper, WifiP2pManager.ChannelListener listener) {
 handler = new Handler(looper);
 return ReflectionHelpers.newInstance(Channel.class);
}

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

@Implementation
public Channel initialize(Context context, Looper looper, WifiP2pManager.ChannelListener listener) {
 handler = new Handler(looper);
 return ReflectionHelpers.newInstance(Channel.class);
}

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

@Implementation
public Channel initialize(Context context, Looper looper, WifiP2pManager.ChannelListener listener) {
 handler = new Handler(looper);
 return ReflectionHelpers.newInstance(Channel.class);
}

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

@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: 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

@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/robolectric-sandbox

public void configure(ClassHandler classHandler, Interceptors interceptors) {
  this.classHandler = classHandler;

  ClassLoader robolectricClassLoader = getRobolectricClassLoader();
  ShadowInvalidator invalidator = getShadowInvalidator();

  Class<?> robolectricInternalsClass = bootstrappedClass(RobolectricInternals.class);
  setStaticField(robolectricInternalsClass, "classHandler", classHandler);
  setStaticField(robolectricInternalsClass, "shadowInvalidator", invalidator);
  setStaticField(robolectricInternalsClass, "classLoader", robolectricClassLoader);

  Class<?> invokeDynamicSupportClass = bootstrappedClass(InvokeDynamicSupport.class);
  setStaticField(invokeDynamicSupportClass, "INTERCEPTORS", interceptors);

  Class<?> shadowClass = bootstrappedClass(Shadow.class);
  setStaticField(shadowClass, "SHADOW_IMPL", newInstance(bootstrappedClass(ShadowImpl.class)));
 }
}

相关文章