This CL adds missing Instant-based methods to FileTime in J2ObjC JRE emulation to satisfy symbol resolution during transpilation of apache_commons_io. Context: yaqs/4512214300974120960 This can unblock users from adding a j2objc version of apache_commons_io through external annotations (see https://fusion2.corp.google.com/invocations/acf259d0-e208-4c8a-89da-442f24d83f05). PiperOrigin-RevId: 896703197
diff --git a/jre_emul/Classes/java/nio/file/attribute/FileTime.java b/jre_emul/Classes/java/nio/file/attribute/FileTime.java index 61100c8..16d290b 100644 --- a/jre_emul/Classes/java/nio/file/attribute/FileTime.java +++ b/jre_emul/Classes/java/nio/file/attribute/FileTime.java
@@ -17,6 +17,7 @@ package java.nio.file.attribute; +import java.time.Instant; import java.util.concurrent.TimeUnit; /** @@ -52,6 +53,40 @@ return new FileTime(value); } + public native Instant toInstant() /*-[ + id p = [JavaNioFileAttributeFileTime getProvider]; + if (p) { + return [p toInstantWithJavaNioFileAttributeFileTime:self]; + } + @throw create_JavaLangUnsupportedOperationException_initWithNSString_(@"java.time is not available"); + ]-*/; + + public static FileTime from(Instant instant) { + return getProvider().fromInstant(instant); + } + + private static Provider provider; + + private static Provider getProvider() { + if (provider == null) { + try { + Class.forName("java.nio.file.attribute.FileTimeProvider").newInstance(); + } catch (Exception e) { + throw new UnsupportedOperationException("java.time is not available", e); + } + } + return provider; + } + + public interface Provider { + Object toInstant(FileTime fileTime); + FileTime fromInstant(Object instant); + } + + public static void setProvider(Provider p) { + provider = p; + } + @Override public int compareTo(FileTime o) { if (o.millis < millis) {
diff --git a/jre_emul/Classes/java/nio/file/attribute/FileTimeProvider.java b/jre_emul/Classes/java/nio/file/attribute/FileTimeProvider.java new file mode 100644 index 0000000..99c6236 --- /dev/null +++ b/jre_emul/Classes/java/nio/file/attribute/FileTimeProvider.java
@@ -0,0 +1,19 @@ +package java.nio.file.attribute; + +import java.time.Instant; + +public class FileTimeProvider implements FileTime.Provider { + public FileTimeProvider() { + FileTime.setProvider(this); + } + + @Override + public Object toInstant(FileTime fileTime) { + return Instant.ofEpochMilli(fileTime.toMillis()); + } + + @Override + public FileTime fromInstant(Object instant) { + return FileTime.fromMillis(((Instant) instant).toEpochMilli()); + } +}