Skip to content

Commit 131babe

Browse files
j2objc-copybaracopybara-github
authored andcommitted
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
1 parent 83dcb8c commit 131babe

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

jre_emul/Classes/java/nio/file/attribute/FileTime.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package java.nio.file.attribute;
1919

20+
import java.time.Instant;
2021
import java.util.concurrent.TimeUnit;
2122

2223
/**
@@ -52,6 +53,40 @@ public static FileTime fromMillis(long value) {
5253
return new FileTime(value);
5354
}
5455

56+
public native Instant toInstant() /*-[
57+
id p = [JavaNioFileAttributeFileTime getProvider];
58+
if (p) {
59+
return [p toInstantWithJavaNioFileAttributeFileTime:self];
60+
}
61+
@throw create_JavaLangUnsupportedOperationException_initWithNSString_(@"java.time is not available");
62+
]-*/;
63+
64+
public static FileTime from(Instant instant) {
65+
return getProvider().fromInstant(instant);
66+
}
67+
68+
private static Provider provider;
69+
70+
private static Provider getProvider() {
71+
if (provider == null) {
72+
try {
73+
Class.forName("java.nio.file.attribute.FileTimeProvider").newInstance();
74+
} catch (Exception e) {
75+
throw new UnsupportedOperationException("java.time is not available", e);
76+
}
77+
}
78+
return provider;
79+
}
80+
81+
public interface Provider {
82+
Object toInstant(FileTime fileTime);
83+
FileTime fromInstant(Object instant);
84+
}
85+
86+
public static void setProvider(Provider p) {
87+
provider = p;
88+
}
89+
5590
@Override
5691
public int compareTo(FileTime o) {
5792
if (o.millis < millis) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package java.nio.file.attribute;
2+
3+
import java.time.Instant;
4+
5+
public class FileTimeProvider implements FileTime.Provider {
6+
public FileTimeProvider() {
7+
FileTime.setProvider(this);
8+
}
9+
10+
@Override
11+
public Object toInstant(FileTime fileTime) {
12+
return Instant.ofEpochMilli(fileTime.toMillis());
13+
}
14+
15+
@Override
16+
public FileTime fromInstant(Object instant) {
17+
return FileTime.fromMillis(((Instant) instant).toEpochMilli());
18+
}
19+
}

0 commit comments

Comments
 (0)