Skip to content

Commit

Permalink
Merge pull request #4265 from robolectric/kill-fs-file
Browse files Browse the repository at this point in the history
Kill FsFile et al. in favor of java.nio.file.Path
  • Loading branch information
xian committed Dec 17, 2018
2 parents 3d15952 + 9cc4569 commit e04e04c
Show file tree
Hide file tree
Showing 64 changed files with 854 additions and 1,179 deletions.
@@ -1,6 +1,8 @@
package org.robolectric.manifest;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -16,7 +18,7 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.robolectric.UsesSdk;
import org.robolectric.res.FsFile;
import org.robolectric.res.Fs;
import org.robolectric.res.ResourcePath;
import org.robolectric.res.ResourceTable;
import org.w3c.dom.Document;
Expand All @@ -28,13 +30,14 @@
* A wrapper for an Android App Manifest, which represents information about one's App to an Android system.
* @see <a href="https://developer.android.com/guide/topics/manifest/manifest-intro.html">Android App Manifest</a>
*/
@SuppressWarnings("NewApi")
public class AndroidManifest implements UsesSdk {
private final FsFile androidManifestFile;
private final FsFile resDirectory;
private final FsFile assetsDirectory;
private final Path androidManifestFile;
private final Path resDirectory;
private final Path assetsDirectory;
private final String overridePackageName;
private final List<AndroidManifest> libraryManifests;
private final FsFile apkFile;
private final Path apkFile;

private boolean manifestIsParsed;

Expand Down Expand Up @@ -69,7 +72,7 @@ public class AndroidManifest implements UsesSdk {
* @param resDirectory Location of the res directory.
* @param assetsDirectory Location of the assets directory.
*/
public AndroidManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory) {
public AndroidManifest(Path androidManifestFile, Path resDirectory, Path assetsDirectory) {
this(androidManifestFile, resDirectory, assetsDirectory, null);
}

Expand All @@ -81,7 +84,7 @@ public AndroidManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile a
* @param assetsDirectory Location of the assets directory.
* @param overridePackageName Application package name.
*/
public AndroidManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory,
public AndroidManifest(Path androidManifestFile, Path resDirectory, Path assetsDirectory,
String overridePackageName) {
this(androidManifestFile, resDirectory, assetsDirectory, Collections.emptyList(), overridePackageName);
}
Expand All @@ -95,7 +98,7 @@ public AndroidManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile a
* @param libraryManifests List of dependency library manifests.
* @param overridePackageName Application package name.
*/
public AndroidManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory,
public AndroidManifest(Path androidManifestFile, Path resDirectory, Path assetsDirectory,
@Nonnull List<AndroidManifest> libraryManifests, String overridePackageName) {
this(
androidManifestFile,
Expand All @@ -106,8 +109,8 @@ public AndroidManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile a
null);
}

public AndroidManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory,
@Nonnull List<AndroidManifest> libraryManifests, String overridePackageName, FsFile apkFile) {
public AndroidManifest(Path androidManifestFile, Path resDirectory, Path assetsDirectory,
@Nonnull List<AndroidManifest> libraryManifests, String overridePackageName, Path apkFile) {
this.androidManifestFile = androidManifestFile;
this.resDirectory = resDirectory;
this.assetsDirectory = assetsDirectory;
Expand Down Expand Up @@ -147,12 +150,12 @@ void parseAndroidManifest() {
return;
}

if (androidManifestFile != null && androidManifestFile.exists()) {
if (androidManifestFile != null && Files.exists(androidManifestFile)) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();
InputStream inputStream = androidManifestFile.getInputStream();
InputStream inputStream = Fs.getInputStream(androidManifestFile);
Document manifestDocument = db.parse(inputStream);
inputStream.close();

Expand Down Expand Up @@ -208,7 +211,7 @@ void parseAndroidManifest() {
}
} else {
if (androidManifestFile != null) {
System.out.println("WARNING: No manifest file found at " + androidManifestFile.getPath() + ".");
System.out.println("WARNING: No manifest file found at " + androidManifestFile + ".");
System.out.println("Falling back to the Android OS resources only.");
System.out.println("To remove this warning, annotate your test class with @Config(manifest=Config.NONE).");
}
Expand Down Expand Up @@ -669,15 +672,15 @@ private void addTransitiveManifests(Set<AndroidManifest> unique, List<AndroidMan
}
}

public FsFile getResDirectory() {
public Path getResDirectory() {
return resDirectory;
}

public FsFile getAssetsDirectory() {
public Path getAssetsDirectory() {
return assetsDirectory;
}

public FsFile getAndroidManifestFile() {
public Path getAndroidManifestFile() {
return androidManifestFile;
}

Expand Down Expand Up @@ -799,7 +802,7 @@ public Map<String, PermissionGroupItemData> getPermissionGroups() {
return null;
}

public FsFile getApkFile() {
public Path getApkFile() {
return apkFile;
}

Expand All @@ -813,7 +816,7 @@ public boolean supportsLegacyResourcesMode() {
@Deprecated
synchronized public boolean supportsBinaryResourcesMode() {
if (supportsBinaryResourcesMode == null) {
supportsBinaryResourcesMode = apkFile != null && apkFile.exists();
supportsBinaryResourcesMode = apkFile != null && Files.exists(apkFile);
}
return supportsBinaryResourcesMode;
}
Expand Down
35 changes: 35 additions & 0 deletions resources/src/main/java/org/robolectric/res/DirBaseNameFilter.java
@@ -0,0 +1,35 @@
package org.robolectric.res;

import java.io.File;
import java.nio.file.Path;
import java.util.function.Predicate;

@SuppressWarnings({"NewApi", "AndroidJdkLibsChecker"})
class DirBaseNameFilter implements Predicate<Path> {
private final String prefix;
private final String prefixDash;

DirBaseNameFilter(String prefix) {
this.prefix = prefix;
this.prefixDash = prefix + "-";
}

@Override
public boolean test(Path file) {
String fileName = nameWithoutTrailingSeparator(file);
return fileName.equals(prefix) || fileName.startsWith(prefixDash);
}

/**
* It sure seems like a bug that Path#getFileName() returns "name/" for paths inside a jar,
* but "name" for paths on a regular filesystem.
*/
private String nameWithoutTrailingSeparator(Path file) {
String fileName = file.getFileName().toString();
int trailingSlash = fileName.indexOf(File.separatorChar);
if (trailingSlash != -1) {
fileName = fileName.substring(0, trailingSlash);
}
return fileName;
}
}
31 changes: 12 additions & 19 deletions resources/src/main/java/org/robolectric/res/DocumentLoader.java
@@ -1,38 +1,31 @@
package org.robolectric.res;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.robolectric.util.Logger;

@SuppressWarnings("NewApi")
public abstract class DocumentLoader {
private static final FsFile.Filter ENDS_WITH_XML = new FsFile.Filter() {
@Override public boolean accept(@Nonnull FsFile fsFile) {
return fsFile.getName().endsWith(".xml");
}
};

protected final String packageName;
private final FsFile resourceBase;
private final Path resourceBase;

public DocumentLoader(String packageName, FsFile resourceBase) {
public DocumentLoader(String packageName, Path resourceBase) {
this.packageName = packageName;
this.resourceBase = resourceBase;
}

public void load(String folderBaseName) {
FsFile[] files = resourceBase.listFiles(new StartsWithFilter(folderBaseName));
if (files == null) {
throw new RuntimeException(resourceBase.join(folderBaseName) + " is not a directory");
}
for (FsFile dir : files) {
public void load(String folderBaseName) throws IOException {
for (Path dir : Fs.listFiles(resourceBase, new DirBaseNameFilter(folderBaseName))) {
loadFile(dir);
}
}

private void loadFile(FsFile dir) {
if (!dir.exists()) {
private void loadFile(Path dir) throws IOException {
if (!Files.exists(dir)) {
throw new RuntimeException("no such directory " + dir);
}
if (!dir.isDirectory()) {
if (!Files.isDirectory(dir)) {
return;
}

Expand All @@ -44,7 +37,7 @@ private void loadFile(FsFile dir) {
return;
}

for (FsFile file : dir.listFiles(ENDS_WITH_XML)) {
for (Path file : Fs.listFiles(dir, path -> path.getFileName().toString().endsWith(".xml"))) {
loadResourceXmlFile(new XmlContext(packageName, file, qualifiers));
}
}
Expand Down
@@ -1,48 +1,47 @@
package org.robolectric.res;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.robolectric.util.Logger;

/**
* DrawableResourceLoader
*/
@SuppressWarnings("NewApi")
public class DrawableResourceLoader {
private final PackageResourceTable resourceTable;

DrawableResourceLoader(PackageResourceTable resourceTable) {
this.resourceTable = resourceTable;
}

/**
* Returns a collection of resource IDs for all nine-patch drawables in the project.
*
* @param resourcePath Resource path.
*/
void findDrawableResources(ResourcePath resourcePath) {
FsFile[] files = resourcePath.getResourceBase().listFiles();
void findDrawableResources(ResourcePath resourcePath) throws IOException {
Path[] files = Fs.listFiles(resourcePath.getResourceBase());
if (files != null) {
for (FsFile f : files) {
if (f.isDirectory() && f.getName().startsWith("drawable")) {
for (Path f : files) {
if (Files.isDirectory(f) && f.getFileName().toString().startsWith("drawable")) {
listDrawableResources(f, "drawable");
} else if (f.isDirectory() && f.getName().startsWith("mipmap")) {
} else if (Files.isDirectory(f) && f.getFileName().toString().startsWith("mipmap")) {
listDrawableResources(f, "mipmap");
}
}
}
}

private void listDrawableResources(FsFile dir, String type) {
FsFile[] files = dir.listFiles();
private void listDrawableResources(Path dir, String type) throws IOException {
Path[] files = Fs.listFiles(dir);
if (files != null) {
Qualifiers qualifiers = null;
Qualifiers qualifiers;
try {
qualifiers = Qualifiers.fromParentDir(dir);
} catch (IllegalArgumentException e) {
Logger.warn(dir + ": " + e.getMessage());
return;
}

for (FsFile f : files) {
String name = f.getName();
for (Path f : files) {
String name = f.getFileName().toString();
if (name.startsWith(".")) continue;

String shortName;
Expand All @@ -55,7 +54,7 @@ private void listDrawableResources(FsFile dir, String type) {
shortName = tokens[0];
isNinePatch = true;
} else {
shortName = f.getBaseName();
shortName = Fs.baseNameFor(f);
isNinePatch = false;
}

Expand Down

0 comments on commit e04e04c

Please sign in to comment.