mirror of https://github.com/grpc/grpc-java.git
core: Use java.time.Time.getNano in InstantTimeProvider without reflection (#11977)
Fixes #11975
This commit is contained in:
parent
a332eddc13
commit
7507a9ec06
|
|
@ -18,37 +18,19 @@ package io.grpc.internal;
|
|||
|
||||
import static com.google.common.math.LongMath.saturatedAdd;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
|
||||
|
||||
/**
|
||||
* {@link InstantTimeProvider} resolves InstantTimeProvider which implements {@link TimeProvider}.
|
||||
*/
|
||||
final class InstantTimeProvider implements TimeProvider {
|
||||
private Method now;
|
||||
private Method getNano;
|
||||
private Method getEpochSecond;
|
||||
|
||||
public InstantTimeProvider(Class<?> instantClass) {
|
||||
try {
|
||||
this.now = instantClass.getMethod("now");
|
||||
this.getNano = instantClass.getMethod("getNano");
|
||||
this.getEpochSecond = instantClass.getMethod("getEpochSecond");
|
||||
} catch (NoSuchMethodException ex) {
|
||||
throw new AssertionError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@IgnoreJRERequirement
|
||||
public long currentTimeNanos() {
|
||||
try {
|
||||
Object instant = now.invoke(null);
|
||||
int nanos = (int) getNano.invoke(instant);
|
||||
long epochSeconds = (long) getEpochSecond.invoke(instant);
|
||||
return saturatedAdd(TimeUnit.SECONDS.toNanos(epochSeconds), nanos);
|
||||
} catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
Instant now = Instant.now();
|
||||
long epochSeconds = now.getEpochSecond();
|
||||
return saturatedAdd(TimeUnit.SECONDS.toNanos(epochSeconds), now.getNano());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,8 +23,8 @@ package io.grpc.internal;
|
|||
final class TimeProviderResolverFactory {
|
||||
static TimeProvider resolveTimeProvider() {
|
||||
try {
|
||||
Class<?> instantClass = Class.forName("java.time.Instant");
|
||||
return new InstantTimeProvider(instantClass);
|
||||
Class.forName("java.time.Instant");
|
||||
return new InstantTimeProvider();
|
||||
} catch (ClassNotFoundException ex) {
|
||||
return new ConcurrentTimeProvider();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,8 +34,7 @@ public class InstantTimeProviderTest {
|
|||
@Test
|
||||
public void testInstantCurrentTimeNanos() throws Exception {
|
||||
|
||||
InstantTimeProvider instantTimeProvider = new InstantTimeProvider(
|
||||
Class.forName("java.time.Instant"));
|
||||
InstantTimeProvider instantTimeProvider = new InstantTimeProvider();
|
||||
|
||||
// Get the current time from the InstantTimeProvider
|
||||
long actualTimeNanos = instantTimeProvider.currentTimeNanos();
|
||||
|
|
|
|||
Loading…
Reference in New Issue