Skip to content

Commit

Permalink
Refactor IdlClass.java to not require JarCreator
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 722757040
Change-Id: I5227b25d67a14ec1f42b09feb181f047b743d36e
  • Loading branch information
ted-xie authored and copybara-github committed Feb 3, 2025
1 parent 6b60537 commit f7b6b9b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
19 changes: 1 addition & 18 deletions src/tools/java/com/google/devtools/build/android/idlclass/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@rules_java//java:defs.bzl", "java_binary", "java_import", "java_library")
load("//tools/android:defs.bzl", "run_singlejar")
load("@rules_java//java:defs.bzl", "java_binary", "java_library")

package(
default_applicable_licenses = ["//:license"],
Expand All @@ -13,21 +12,6 @@ java_binary(
runtime_deps = [":idlclass_lib"],
)

# NOTE: jarhelper actually exists in @bazel_tools, but its visibility is set too restrictively.
run_singlejar(
name = "jarhelper_jar_gen",
srcs = ["@android_tools//:all_android_tools_deploy.jar"],
out = "jarhelper.jar",
include_prefixes = [
"com/google/devtools/build/buildjar/jarhelper/",
],
)

java_import(
name = "jarhelper_jar",
jars = [":jarhelper.jar"],
)

java_library(
name = "idlclass_lib",
srcs = glob(["*.java"]),
Expand All @@ -37,7 +21,6 @@ java_library(
"//src/tools/java/com/google/devtools/build/android:__pkg__",
],
deps = [
":jarhelper_jar",
"//src/tools/java/com/google/devtools/build/android:android_builder_lib",
"//src/tools/java/com/google/devtools/build/android:android_options_utils",
"@bazel_tools//src/main/protobuf:java_compilation_java_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,47 @@

package com.google.devtools.build.android.idlclass;

import static java.util.stream.Collectors.toCollection;

import com.beust.jcommander.JCommander;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.hash.Hashing;
import com.google.devtools.build.android.AndroidOptionsUtils;
import com.google.devtools.build.buildjar.jarhelper.JarCreator;
import com.google.devtools.build.buildjar.proto.JavaCompilation.CompilationUnit;
import com.google.devtools.build.buildjar.proto.JavaCompilation.Manifest;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
* IdlClass post-processes the output of a Java compilation, and produces
* a jar containing only the class files for sources that were generated
* from idl processing.
*/
public class IdlClass {
// Cribbed from AarGeneratorAction.java
private static final Instant DEFAULT_TIMESTAMP =
LocalDateTime.of(2010, 1, 1, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant();

public static void main(String[] args) throws IOException {
IdlClassOptions idlClassOptions = new IdlClassOptions();
Expand Down Expand Up @@ -160,10 +174,31 @@ private static void extractIdlClasses(

/** Writes the generated class files to the output jar. */
private static void writeOutputJar(Path outputJar, Path tempDir) throws IOException {
JarCreator output = new JarCreator(outputJar.toString());
output.setCompression(true);
output.setNormalize(true);
output.addDirectory(tempDir.toString());
output.execute();
// Zip up the contents of tempDir into outputJar.

try (final ZipOutputStream zip =
new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(outputJar)))) {
zip.setMethod(ZipOutputStream.STORED);

List<Path> files;
try (Stream<Path> stream = Files.walk(tempDir).filter(Files::isRegularFile)) {
files = stream.collect(toCollection(ArrayList::new));
}

for (Path path : files) {
ZipEntry entry = new ZipEntry(tempDir.relativize(path).toString());
entry.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
entry.setSize(Files.size(path));
entry.setCompressedSize(Files.size(path));
entry.setCrc(Hashing.crc32().hashBytes(Files.readAllBytes(path)).asInt());
zip.putNextEntry(entry);
zip.write(Files.readAllBytes(path));
zip.closeEntry();
}
zip.close();
} catch (IOException e) {
throw new IOException("Failed to write output jar: " + outputJar, e);
}
Files.setLastModifiedTime(outputJar, FileTime.from(DEFAULT_TIMESTAMP));
}
}

0 comments on commit f7b6b9b

Please sign in to comment.