Close some previously unclosed resources.
Summary:
This change closes previously unclosed resources.
This reflects https://github.com/facebook/buck/pull/46
Test Plan: buck test --all
diff --git a/src/com/facebook/buck/android/ExtractResourcesStep.java b/src/com/facebook/buck/android/ExtractResourcesStep.java
index f821f66..977dbb0 100644
--- a/src/com/facebook/buck/android/ExtractResourcesStep.java
+++ b/src/com/facebook/buck/android/ExtractResourcesStep.java
@@ -51,8 +51,7 @@
public int execute(ExecutionContext context) {
File outputDirectory = new File(extractedResourcesDir);
for (String path : pathsToThirdPartyJars) {
- try {
- final JarFile jar = new JarFile(path);
+ try (final JarFile jar = new JarFile(path)) {
for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); ) {
final JarEntry entry = entries.nextElement();
String name = entry.getName();
diff --git a/src/com/facebook/buck/android/MergeAndroidResourcesStep.java b/src/com/facebook/buck/android/MergeAndroidResourcesStep.java
index d3b6655..0dd7094 100644
--- a/src/com/facebook/buck/android/MergeAndroidResourcesStep.java
+++ b/src/com/facebook/buck/android/MergeAndroidResourcesStep.java
@@ -163,28 +163,29 @@
// Read the symbols file and parse each line as a Resource.
Readable readable = filePathToReadable.apply(symbolsFile);
- Scanner scanner = new Scanner(readable);
- while (scanner.hasNext()) {
- String line = scanner.nextLine();
- Matcher matcher = TEXT_SYMBOLS_LINE.matcher(line);
- boolean isMatch = matcher.matches();
- Preconditions.checkState(isMatch, "Should be able to match '%s'.", line);
- String idType = matcher.group(1);
- String type = matcher.group(2);
- String name = matcher.group(3);
- String idValue = matcher.group(4);
+ try (Scanner scanner = new Scanner(readable)) {
+ while (scanner.hasNext()) {
+ String line = scanner.nextLine();
+ Matcher matcher = TEXT_SYMBOLS_LINE.matcher(line);
+ boolean isMatch = matcher.matches();
+ Preconditions.checkState(isMatch, "Should be able to match '%s'.", line);
+ String idType = matcher.group(1);
+ String type = matcher.group(2);
+ String name = matcher.group(3);
+ String idValue = matcher.group(4);
- // We're only doing the remapping so Roboelectric is happy and it is already ignoring the
- // id references found in the styleable section. So let's do that as well so we don't have
- // to get fancier than is needed. That is, just re-enumerate all app-level resource ids
- // and ignore everything else, allowing the styleable references to be messed up.
- String idValueToUse = idValue;
- if (reenumerate && idValue.startsWith("0x7f")) {
- idValueToUse = String.format("0x%08x", enumerator.next());
+ // We're only doing the remapping so Roboelectric is happy and it is already ignoring the
+ // id references found in the styleable section. So let's do that as well so we don't have
+ // to get fancier than is needed. That is, just re-enumerate all app-level resource ids
+ // and ignore everything else, allowing the styleable references to be messed up.
+ String idValueToUse = idValue;
+ if (reenumerate && idValue.startsWith("0x7f")) {
+ idValueToUse = String.format("0x%08x", enumerator.next());
+ }
+
+ Resource resource = new Resource(idType, type, name, idValue, idValueToUse);
+ rDotJavaPackageToSymbolsFiles.put(packageName, resource);
}
-
- Resource resource = new Resource(idType, type, name, idValue, idValueToUse);
- rDotJavaPackageToSymbolsFiles.put(packageName, resource);
}
}
return rDotJavaPackageToSymbolsFiles;
diff --git a/src/com/facebook/buck/android/SplitZipStep.java b/src/com/facebook/buck/android/SplitZipStep.java
index 6e7b7d4..cb1b877 100644
--- a/src/com/facebook/buck/android/SplitZipStep.java
+++ b/src/com/facebook/buck/android/SplitZipStep.java
@@ -233,11 +233,12 @@
}
private static String findAnyClass(File jarFile) throws IOException {
- ZipFile inZip = new ZipFile(jarFile);
- for (ZipEntry entry : Collections.list(inZip.entries())) {
- Matcher m = classFilePattern.matcher(entry.getName());
- if (m.matches()) {
- return m.group(1).replace('/', '.');
+ try (ZipFile inZip = new ZipFile(jarFile)) {
+ for (ZipEntry entry : Collections.list(inZip.entries())) {
+ Matcher m = classFilePattern.matcher(entry.getName());
+ if (m.matches()) {
+ return m.group(1).replace('/', '.');
+ }
}
}
// TODO(user): It's possible for this to happen by chance, so we should handle it better.
diff --git a/src/com/facebook/buck/cli/AuditRulesCommand.java b/src/com/facebook/buck/cli/AuditRulesCommand.java
index 2fb5f8a..34900fe 100644
--- a/src/com/facebook/buck/cli/AuditRulesCommand.java
+++ b/src/com/facebook/buck/cli/AuditRulesCommand.java
@@ -86,37 +86,41 @@
@Override
int runCommandWithOptionsInternal(AuditRulesOptions options) throws IOException {
ProjectFilesystem projectFilesystem = getProjectFilesystem();
- ProjectBuildFileParser parser = new ProjectBuildFileParser(projectFilesystem,
+ try (ProjectBuildFileParser parser = new ProjectBuildFileParser(projectFilesystem,
options.getBuckConfig().getDefaultIncludes(),
- options.getBuckConfig().getPythonInterpreter());
- PrintStream out = console.getStdOut();
- for (String pathToBuildFile : options.getArguments()) {
- // Print a comment with the path to the build file.
- out.printf("# %s\n\n", pathToBuildFile);
+ options.getBuckConfig().getPythonInterpreter())) {
+ PrintStream out = console.getStdOut();
+ for (String pathToBuildFile : options.getArguments()) {
+ // Print a comment with the path to the build file.
+ out.printf("# %s\n\n", pathToBuildFile);
- // Resolve the path specified by the user.
- Path path = Paths.get(pathToBuildFile);
- if (!path.isAbsolute()) {
- Path root = projectFilesystem.getRootPath();
- path = root.resolve(path);
- }
+ // Resolve the path specified by the user.
+ Path path = Paths.get(pathToBuildFile);
+ if (!path.isAbsolute()) {
+ Path root = projectFilesystem.getProjectRoot().toPath();
+ path = root.resolve(path);
+ }
- // Parse the rules from the build file.
- List<Map<String, Object>> rawRules;
- try {
- rawRules = parser.getAllRules(path.toString());
- } catch (BuildFileParseException e) {
- throw new HumanReadableException(e);
- }
+ // Parse the rules from the build file.
+ List<Map<String, Object>> rawRules;
+ try {
+ rawRules = parser.getAllRules(path.toString());
+ } catch (BuildFileParseException e) {
+ throw new HumanReadableException(e);
+ }
- // Format and print the rules from the raw data, filtered by type.
- final ImmutableSet<String> types = options.getTypes();
- Predicate<String> includeType = new Predicate<String>() {
+ // Format and print the rules from the raw data, filtered by type.
+ final ImmutableSet<String> types = options.getTypes();
+ Predicate<String> includeType = new Predicate<String>() {
@Override
public boolean apply(String type) {
return types.isEmpty() || types.contains(type);
- }};
- printRulesToStdout(rawRules, includeType);
+ }
+ };
+ printRulesToStdout(rawRules, includeType);
+ }
+ } catch (BuildFileParseException e) {
+ throw new HumanReadableException("Unable to create parser");
}
return 0;
diff --git a/test/com/facebook/buck/android/ProGuardObfuscateStepTest.java b/test/com/facebook/buck/android/ProGuardObfuscateStepTest.java
index 2583ec0..aeab1eb 100644
--- a/test/com/facebook/buck/android/ProGuardObfuscateStepTest.java
+++ b/test/com/facebook/buck/android/ProGuardObfuscateStepTest.java
@@ -39,14 +39,15 @@
ProGuardObfuscateStep.createEmptyZip(tmpFile);
// Try to read it.
- ZipFile zipFile = new ZipFile(tmpFile);
- int totalSize = 0;
- List<? extends ZipEntry> entries = Collections.list(zipFile.entries());
+ try (ZipFile zipFile = new ZipFile(tmpFile)) {
+ int totalSize = 0;
+ List<? extends ZipEntry> entries = Collections.list(zipFile.entries());
- assertTrue("Expected either 0 or 1 entry", entries.size() <= 1);
- for (ZipEntry entry : entries) {
- totalSize += entry.getSize();
+ assertTrue("Expected either 0 or 1 entry", entries.size() <= 1);
+ for (ZipEntry entry : entries) {
+ totalSize += entry.getSize();
+ }
+ assertEquals("Zip file should have zero-length contents", 0, totalSize);
}
- assertEquals("Zip file should have zero-length contents", 0, totalSize);
}
}
diff --git a/test/com/facebook/buck/cli/ProjectCommandTest.java b/test/com/facebook/buck/cli/ProjectCommandTest.java
index bf6030e..fd77f9a 100644
--- a/test/com/facebook/buck/cli/ProjectCommandTest.java
+++ b/test/com/facebook/buck/cli/ProjectCommandTest.java
@@ -70,8 +70,7 @@
private static final ArtifactCache artifactCache = new NoopArtifactCache();
@Test
- public void testBasicProjectCommand()
- throws IOException, NoSuchBuildTargetException, NoSuchMethodException {
+ public void testBasicProjectCommand() throws Exception {
BuildRuleResolver ruleResolver = new BuildRuleResolver();
BuildTarget javaLibraryTargetName = BuildTargetFactory.newInstance("//javasrc:java-library");
@@ -137,7 +136,7 @@
}
private PartialGraph createGraphFromBuildRules(List<BuildRule> rules) {
- MutableDirectedGraph<BuildRule> graph = new MutableDirectedGraph<BuildRule>();
+ MutableDirectedGraph<BuildRule> graph = new MutableDirectedGraph<>();
for (BuildRule rule : rules) {
for (BuildRule dep : rule.getDeps()) {
graph.addEdge(rule, dep);
diff --git a/test/com/facebook/buck/zip/UnzipStepTest.java b/test/com/facebook/buck/zip/UnzipStepTest.java
index 0e00a13..8694c4a 100644
--- a/test/com/facebook/buck/zip/UnzipStepTest.java
+++ b/test/com/facebook/buck/zip/UnzipStepTest.java
@@ -61,12 +61,13 @@
File bin2 = new File(extractFolder.getAbsolutePath() + "/subdir/2.bin");
assertTrue(bin2.exists());
assertTrue(new File(extractFolder.getAbsolutePath() + "/emptydir").isDirectory());
- FileInputStream input = new FileInputStream(bin2);
- byte[] buffer = new byte[DUMMY_FILE_CONTENTS.length];
- int bytesRead = input.read(buffer, 0, DUMMY_FILE_CONTENTS.length);
- assertEquals(DUMMY_FILE_CONTENTS.length, bytesRead);
- for (int i = 0; i < DUMMY_FILE_CONTENTS.length; i++) {
- assertEquals(DUMMY_FILE_CONTENTS[i], buffer[i]);
+ try (FileInputStream input = new FileInputStream(bin2)) {
+ byte[] buffer = new byte[DUMMY_FILE_CONTENTS.length];
+ int bytesRead = input.read(buffer, 0, DUMMY_FILE_CONTENTS.length);
+ assertEquals(DUMMY_FILE_CONTENTS.length, bytesRead);
+ for (int i = 0; i < DUMMY_FILE_CONTENTS.length; i++) {
+ assertEquals(DUMMY_FILE_CONTENTS[i], buffer[i]);
+ }
}
}
}
diff --git a/test/com/facebook/buck/zip/ZipDirectoryWithMaxDeflateStepTest.java b/test/com/facebook/buck/zip/ZipDirectoryWithMaxDeflateStepTest.java
index e211f9e..04cf2d5 100644
--- a/test/com/facebook/buck/zip/ZipDirectoryWithMaxDeflateStepTest.java
+++ b/test/com/facebook/buck/zip/ZipDirectoryWithMaxDeflateStepTest.java
@@ -66,23 +66,23 @@
assertTrue(outputApk.exists());
- ZipFile resultZip = new ZipFile(outputApk.getAbsoluteFile());
- ZipEntry storedFile = resultZip.getEntry("StoredFile");
- assertEquals("StoredFile should have been STORED else Froyo will crash on launch.",
- storedFile.getMethod(),
- ZipEntry.STORED);
+ try (ZipFile resultZip = new ZipFile(outputApk.getAbsoluteFile())) {
+ ZipEntry storedFile = resultZip.getEntry("StoredFile");
+ assertEquals("StoredFile should have been STORED else Froyo will crash on launch.",
+ storedFile.getMethod(),
+ ZipEntry.STORED);
- ZipEntry deflatedFile = resultZip.getEntry("subDir/DeflatedFile");
- assertEquals(
- "DeflatedFile should have been DEFLATED",
- deflatedFile.getMethod(),
- ZipEntry.DEFLATED);
+ ZipEntry deflatedFile = resultZip.getEntry("subDir/DeflatedFile");
+ assertEquals(
+ "DeflatedFile should have been DEFLATED",
+ deflatedFile.getMethod(),
+ ZipEntry.DEFLATED);
- ZipEntry compressedFile = resultZip.getEntry("CompressedFile.xz");
- assertEquals("CompressedFile.xz should have been STORED, it's already compressed",
- compressedFile.getMethod(),
- ZipEntry.STORED);
-
+ ZipEntry compressedFile = resultZip.getEntry("CompressedFile.xz");
+ assertEquals("CompressedFile.xz should have been STORED, it's already compressed",
+ compressedFile.getMethod(),
+ ZipEntry.STORED);
+ }
EasyMock.verify(executionContext);
}