Skip to content

Commit 7507a9e

Browse files
authored
core: Use java.time.Time.getNano in InstantTimeProvider without reflection (#11977)
Fixes #11975
1 parent a332edd commit 7507a9e

File tree

3 files changed

+9
-28
lines changed

3 files changed

+9
-28
lines changed

core/src/main/java/io/grpc/internal/InstantTimeProvider.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,19 @@
1818

1919
import static com.google.common.math.LongMath.saturatedAdd;
2020

21-
import java.lang.reflect.InvocationTargetException;
22-
import java.lang.reflect.Method;
21+
import java.time.Instant;
2322
import java.util.concurrent.TimeUnit;
23+
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
2424

2525
/**
2626
* {@link InstantTimeProvider} resolves InstantTimeProvider which implements {@link TimeProvider}.
2727
*/
2828
final class InstantTimeProvider implements TimeProvider {
29-
private Method now;
30-
private Method getNano;
31-
private Method getEpochSecond;
32-
33-
public InstantTimeProvider(Class<?> instantClass) {
34-
try {
35-
this.now = instantClass.getMethod("now");
36-
this.getNano = instantClass.getMethod("getNano");
37-
this.getEpochSecond = instantClass.getMethod("getEpochSecond");
38-
} catch (NoSuchMethodException ex) {
39-
throw new AssertionError(ex);
40-
}
41-
}
42-
4329
@Override
30+
@IgnoreJRERequirement
4431
public long currentTimeNanos() {
45-
try {
46-
Object instant = now.invoke(null);
47-
int nanos = (int) getNano.invoke(instant);
48-
long epochSeconds = (long) getEpochSecond.invoke(instant);
49-
return saturatedAdd(TimeUnit.SECONDS.toNanos(epochSeconds), nanos);
50-
} catch (IllegalAccessException | InvocationTargetException ex) {
51-
throw new RuntimeException(ex);
52-
}
32+
Instant now = Instant.now();
33+
long epochSeconds = now.getEpochSecond();
34+
return saturatedAdd(TimeUnit.SECONDS.toNanos(epochSeconds), now.getNano());
5335
}
5436
}

core/src/main/java/io/grpc/internal/TimeProviderResolverFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
final class TimeProviderResolverFactory {
2424
static TimeProvider resolveTimeProvider() {
2525
try {
26-
Class<?> instantClass = Class.forName("java.time.Instant");
27-
return new InstantTimeProvider(instantClass);
26+
Class.forName("java.time.Instant");
27+
return new InstantTimeProvider();
2828
} catch (ClassNotFoundException ex) {
2929
return new ConcurrentTimeProvider();
3030
}

core/src/test/java/io/grpc/internal/InstantTimeProviderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public class InstantTimeProviderTest {
3434
@Test
3535
public void testInstantCurrentTimeNanos() throws Exception {
3636

37-
InstantTimeProvider instantTimeProvider = new InstantTimeProvider(
38-
Class.forName("java.time.Instant"));
37+
InstantTimeProvider instantTimeProvider = new InstantTimeProvider();
3938

4039
// Get the current time from the InstantTimeProvider
4140
long actualTimeNanos = instantTimeProvider.currentTimeNanos();

0 commit comments

Comments
 (0)