org.robolectric.util.Util类的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(176)

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

Util介绍

[英]Generic collection of utility methods.
[中]实用程序方法的通用集合。

代码示例

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

protected byte[] getByteCode(String className) throws ClassNotFoundException {
 String classFilename = className.replace('.', '/') + ".class";
 try (InputStream classBytesStream = getClassBytesAsStreamPreferringLocalUrls(classFilename)) {
  if (classBytesStream == null) {
   throw new ClassNotFoundException(className);
  }
  return Util.readBytes(classBytesStream);
 } catch (IOException e) {
  throw new ClassNotFoundException("couldn't load " + className, e);
 }
}

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

public static File file(String... pathParts) {
 return file(new File("."), pathParts);
}

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

/**
 * This method consumes an input stream and returns its content.
 *
 * @param is The input stream to read from.
 * @return The bytes read from the stream.
 * @throws IOException Error reading from stream.
 */
public static byte[] readBytes(InputStream is) throws IOException {
 try (ByteArrayOutputStream bos = new ByteArrayOutputStream(is.available())) {
  copy(is, bos);
  return bos.toByteArray();
 }
}

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

int findValueFor(String key) {
  String valueFor = attrData.getValueFor(key);
  if (valueFor == null) {
   // Maybe they have passed in the value directly, rather than the name.
   if (attrData.isValue(key)) {
    valueFor = key;
   } else {
    throw new RuntimeException("no value found for " + key);
   }
  }
  return Util.parseInt(valueFor);
 }
}

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

public static Collection<Interceptor> all() {
 List<Interceptor> interceptors = new ArrayList<>(asList(
   new LinkedHashMapEldestInterceptor(),
   new PolicyManagerMakeNewWindowInterceptor(),
   new SystemTimeInterceptor(),
   new SystemArrayCopyInterceptor(),
   new LocaleAdjustLanguageCodeInterceptor(),
   new SystemLogEInterceptor(),
   new NoOpInterceptor()
 ));
 if (Util.getJavaVersion() >= 9) {
  interceptors.add(new CleanerInterceptor());
 }
 return interceptors;
}

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

protected int findValueFor(String key) {
  key = (key == null) ? null : key.trim();
  String valueFor = attrData.getValueFor(key);
  if (valueFor == null) {
   // Maybe they have passed in the value directly, rather than the name.
   if (attrData.isValue(key)) {
    valueFor = key;
   } else {
    throw new Resources.NotFoundException("no value found for " + key);
   }
  }
  return Util.parseInt(valueFor);
 }
}

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

.addClassNameTranslation("java.util.jar.StrictJarFile", Object.class.getName());
if (Util.getJavaVersion() >= 9) {
 builder.addClassNameTranslation("sun.misc.Cleaner", "java.lang.ref.Cleaner$Cleanable");

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

public static byte[] getBytes(Path path) throws IOException {
 return Util.readBytes(getInputStream(path));
}

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

private static Path resourcesBaseDir() {
 // Try to locate the manifest file as a classpath resource.
 final String resourceName = "/src/test/resources/AndroidManifest.xml";
 final URL resourceUrl = TestRunnerWithManifest.class.getResource(resourceName);
 if (resourceUrl != null && "file".equals(resourceUrl.getProtocol())) {
  // Construct a path to the manifest file relative to the current working directory.
  final URI workingDirectory = URI.create(System.getProperty("user.dir"));
  final URI absolutePath = URI.create(resourceUrl.getPath());
  final URI relativePath = workingDirectory.relativize(absolutePath);
  return new File(relativePath.toString()).getParentFile().toPath();
 }
 // Return a path relative to the current working directory.
 return Util.file("src", "test", "resources").toPath();
}

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

int findValueFor(String key) {
  String valueFor = attrData.getValueFor(key);
  if (valueFor == null) {
   // Maybe they have passed in the value directly, rather than the name.
   if (attrData.isValue(key)) {
    valueFor = key;
   } else {
    throw new RuntimeException("no value found for " + key);
   }
  }
  return Util.parseInt(valueFor);
 }
}

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

Util.copy(jarFile.getInputStream(jarEntry), jarOut);
nonClassCount++;

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

private static byte[] getClassBytes(String className, JarFile jarFile)
  throws ClassNotFoundException {
 String classFilename = className.replace('.', '/') + ".class";
 ZipEntry entry = jarFile.getEntry(classFilename);
 try {
  InputStream inputStream;
  if (entry == null) {
   inputStream = JarInstrumentor.class.getClassLoader().getResourceAsStream(classFilename);
  } else {
   inputStream = jarFile.getInputStream(entry);
  }
  if (inputStream == null) {
   throw new ClassNotFoundException("Couldn't find " + className.replace('/', '.'));
  }
  return Util.readBytes(inputStream);
 } catch (IOException e) {
  throw new ClassNotFoundException("Couldn't load " + className.replace('/', '.'), e);
 }
}

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

private static Path resourcesBaseDir() {
 // Try to locate the manifest file as a classpath resource.
 final String resourceName = "/src/test/resources/AndroidManifest.xml";
 final URL resourceUrl = TestRunnerWithManifest.class.getResource(resourceName);
 if (resourceUrl != null && "file".equals(resourceUrl.getProtocol())) {
  // Construct a path to the manifest file relative to the current working directory.
  final URI workingDirectory = URI.create(System.getProperty("user.dir"));
  final URI absolutePath = URI.create(resourceUrl.getPath());
  final URI relativePath = workingDirectory.relativize(absolutePath);
  return new File(relativePath.toString()).getParentFile().toPath();
 }
 // Return a path relative to the current working directory.
 return Util.file("src", "test", "resources").toPath();
}

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

protected int findValueFor(String key) {
  String valueFor = attrData.getValueFor(key);
  if (valueFor == null) {
   // Maybe they have passed in the value directly, rather than the name.
   if (attrData.isValue(key)) {
    valueFor = key;
   } else {
    throw new RuntimeException("no value found for " + key);
   }
  }
  return Util.parseInt(valueFor);
 }
}

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

/**
 * This method consumes an input stream and returns its content.
 *
 * @param is The input stream to read from.
 * @return The bytes read from the stream.
 * @throws IOException Error reading from stream.
 */
public static byte[] readBytes(InputStream is) throws IOException {
 try (ByteArrayOutputStream bos = new ByteArrayOutputStream(is.available())) {
  copy(is, bos);
  return bos.toByteArray();
 }
}

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

@Nonnull
 private List<Path> getDirectoriesFromProperty(String property) {
  if (property == null) {
   return Collections.emptyList();
  }

  List<String> dirs;
  if (property.startsWith("@")) {
   String filename = property.substring(1);
   try {
    dirs = Arrays.asList(
      new String(Util.readBytes(new FileInputStream(filename)), UTF_8).split("\\n"));
   } catch (IOException e) {
    throw new RuntimeException("Cannot read file " + filename);
   }
  } else {
   dirs = Arrays.asList(property.split(File.pathSeparator));
  }

  List<Path> files = new ArrayList<>();
  for (String dir : dirs) {
   files.add(Fs.fromUrl(dir));
  }
  return files;
 }
}

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

public static File file(String... pathParts) {
 return file(new File("."), pathParts);
}

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

protected int findValueFor(String key) {
  String valueFor = attrData.getValueFor(key);
  if (valueFor == null) {
   // Maybe they have passed in the value directly, rather than the name.
   if (attrData.isValue(key)) {
    valueFor = key;
   } else {
    throw new RuntimeException("no value found for " + key);
   }
  }
  return Util.parseInt(valueFor);
 }
}

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

/**
 * This method consumes an input stream and returns its content.
 *
 * @param is The input stream to read from.
 * @return The bytes read from the stream.
 * @throws IOException Error reading from stream.
 */
public static byte[] readBytes(InputStream is) throws IOException {
 try (ByteArrayOutputStream bos = new ByteArrayOutputStream(is.available())) {
  copy(is, bos);
  return bos.toByteArray();
 }
}

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

private void interceptResponseContent(HttpResponse response) {
  HttpEntity entity = response.getEntity();
  if (entity instanceof HttpEntityWrapper) {
   HttpEntityWrapper entityWrapper = (HttpEntityWrapper) entity;
   try {
    Field wrappedEntity = HttpEntityWrapper.class.getDeclaredField("wrappedEntity");
    wrappedEntity.setAccessible(true);
    entity = (HttpEntity) wrappedEntity.get(entityWrapper);
   } catch (Exception e) {
    // fail to record
   }
  }
  if (entity instanceof BasicHttpEntity) {
   BasicHttpEntity basicEntity = (BasicHttpEntity) entity;
   try {
    Field contentField = BasicHttpEntity.class.getDeclaredField("content");
    contentField.setAccessible(true);
    InputStream content = (InputStream) contentField.get(basicEntity);

    byte[] buffer = Util.readBytes(content);

    FakeHttp.getFakeHttpLayer().addHttpResponseContent(buffer);
    contentField.set(basicEntity, new ByteArrayInputStream(buffer));
   } catch (Exception e) {
    // fail to record
   }
  }
 }
}

相关文章