aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java
diff options
context:
space:
mode:
authorGravatar corysmith <corysmith@google.com>2018-07-11 12:43:21 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-11 12:44:55 -0700
commit1a3c6c94921e63f7e55c9167d356d0b6f6abe6cf (patch)
tree92c6655ba15a4efaeffc456e2d40fd271efdfedf /src/tools/android/java
parentfb8332ff6ee990fccc98548e12b200359b3114f8 (diff)
Cleanup Optional usage
RELNOTES:None PiperOrigin-RevId: 204170706
Diffstat (limited to 'src/tools/android/java')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java48
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/aapt2/CompiledResources.java17
2 files changed, 31 insertions, 34 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java b/src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java
index 3c599e4279..80ed2dc8ca 100644
--- a/src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java
+++ b/src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java
@@ -27,7 +27,6 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.zip.ZipEntry;
@@ -41,14 +40,11 @@ public class ResourcesZip {
private final Path resourcesRoot;
private final Path assetsRoot;
- private final Optional<Path> apkWithAssets;
- private final Optional<Path> ids;
+ @Nullable private final Path apkWithAssets;
+ @Nullable private final Path ids;
private ResourcesZip(
- Path resourcesRoot,
- Path assetsRoot,
- Optional<Path> ids,
- Optional<Path> apkWithAssets) {
+ Path resourcesRoot, Path assetsRoot, @Nullable Path ids, @Nullable Path apkWithAssets) {
this.resourcesRoot = resourcesRoot;
this.assetsRoot = assetsRoot;
this.ids = ids;
@@ -60,7 +56,7 @@ public class ResourcesZip {
* @param assetsRoot The root of the raw assets.
*/
public static ResourcesZip from(Path resourcesRoot, Path assetsRoot) {
- return new ResourcesZip(resourcesRoot, assetsRoot, Optional.empty(), Optional.empty());
+ return new ResourcesZip(resourcesRoot, assetsRoot, null, null);
}
/**
@@ -72,8 +68,8 @@ public class ResourcesZip {
return new ResourcesZip(
resourcesRoot,
assetsRoot,
- Optional.of(resourceIds).filter(Files::exists),
- Optional.empty());
+ resourceIds != null && Files.exists(resourceIds) ? resourceIds : null,
+ null);
}
/**
@@ -81,12 +77,13 @@ public class ResourcesZip {
* @param apkWithAssets The apk containing assets.
* @param resourceIds Optional path to a file containing the resource ids.
*/
- public static ResourcesZip fromApk(Path resourcesRoot, Path apkWithAssets, Path resourceIds) {
+ public static ResourcesZip fromApk(
+ Path resourcesRoot, Path apkWithAssets, @Nullable Path resourceIds) {
return new ResourcesZip(
resourcesRoot,
/* assetsRoot= */ null,
- Optional.of(resourceIds).filter(Files::exists),
- Optional.of(apkWithAssets));
+ resourceIds != null && Files.exists(resourceIds) ? resourceIds : null,
+ apkWithAssets);
}
/** Creates a ResourcesZip from an archive by expanding into the workingDirectory. */
@@ -139,17 +136,15 @@ public class ResourcesZip {
visitor.writeEntries();
}
- if (apkWithAssets.isPresent()){
- ZipFile apkZip = new ZipFile(apkWithAssets.get().toString());
+ if (apkWithAssets != null) {
+ ZipFile apkZip = new ZipFile(apkWithAssets.toString());
apkZip
.stream()
.filter(entry -> entry.getName().startsWith("assets/"))
.forEach(
entry -> {
try {
- zip.addEntry(
- entry,
- ByteStreams.toByteArray(apkZip.getInputStream(entry)));
+ zip.addEntry(entry, ByteStreams.toByteArray(apkZip.getInputStream(entry)));
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -163,14 +158,13 @@ public class ResourcesZip {
visitor.writeEntries();
}
- ids.ifPresent(
- p -> {
- try {
- zip.addEntry("ids.txt", Files.readAllBytes(p), ZipEntry.STORED);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- });
+ if (ids != null) {
+ try {
+ zip.addEntry("ids.txt", Files.readAllBytes(ids), ZipEntry.STORED);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
}
}
@@ -189,7 +183,7 @@ public class ResourcesZip {
packages, rTxt, classJar, manifest, proguardMapping, resourcesRoot, logFile)
.shrink(workingDirectory);
return ShrunkResources.of(
- new ResourcesZip(workingDirectory, assetsRoot, ids, Optional.empty()),
+ new ResourcesZip(workingDirectory, assetsRoot, ids, null),
new UnvalidatedAndroidData(
ImmutableList.of(workingDirectory), ImmutableList.of(assetsRoot), manifest));
}
diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/CompiledResources.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/CompiledResources.java
index 65200f6b52..06daa81754 100644
--- a/src/tools/android/java/com/google/devtools/build/android/aapt2/CompiledResources.java
+++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/CompiledResources.java
@@ -33,9 +33,9 @@ import javax.annotation.Nullable;
public class CompiledResources implements ManifestContainer {
private final Path resources;
- private final Path manifest;
+ @Nullable private final Path manifest;
private final List<Path> assetsDirs;
- private final Optional<Path> stableIds;
+ @Nullable private final Path stableIds;
/**
* @param resources The path to the zip file containing the compiled resource result of aapt2
@@ -44,7 +44,7 @@ public class CompiledResources implements ManifestContainer {
* @param stableIds The path to the file containing resource ID's to keep stable
*/
private CompiledResources(
- Path resources, Path manifest, List<Path> assetsDirs, Optional<Path> stableIds) {
+ Path resources, @Nullable Path manifest, List<Path> assetsDirs, @Nullable Path stableIds) {
this.resources = resources;
this.manifest = manifest;
this.assetsDirs = assetsDirs;
@@ -62,7 +62,7 @@ public class CompiledResources implements ManifestContainer {
public static CompiledResources from(
Path resources, @Nullable Path manifest, @Nullable List<Path> assetDirs) {
return new CompiledResources(
- resources, manifest, assetDirs != null ? assetDirs : ImmutableList.of(), Optional.empty());
+ resources, manifest, assetDirs != null ? assetDirs : ImmutableList.of(), null);
}
/** This zip file contains resource flat files that are the result of aapt2 compile */
@@ -77,8 +77,11 @@ public class CompiledResources implements ManifestContainer {
}
@Override
+ @Nullable
public Path getManifest() {
- Preconditions.checkState(Files.exists(manifest));
+ if (manifest != null) {
+ Preconditions.checkState(Files.exists(manifest));
+ }
return manifest;
}
@@ -98,11 +101,11 @@ public class CompiledResources implements ManifestContainer {
return new CompiledResources(resources, processManifest.apply(manifest), assetsDirs, stableIds);
}
- public CompiledResources addStableIds(Optional<Path> stableIds) {
+ public CompiledResources addStableIds(Path stableIds) {
return new CompiledResources(resources, manifest, assetsDirs, stableIds);
}
public Optional<Path> getStableIds() {
- return stableIds;
+ return Optional.ofNullable(stableIds);
}
}