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 static com.google.common.math.LongMath.saturatedAdd;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.time.Instant;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link InstantTimeProvider} resolves InstantTimeProvider which implements {@link TimeProvider}.
|
* {@link InstantTimeProvider} resolves InstantTimeProvider which implements {@link TimeProvider}.
|
||||||
*/
|
*/
|
||||||
final class InstantTimeProvider implements 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
|
@Override
|
||||||
|
@IgnoreJRERequirement
|
||||||
public long currentTimeNanos() {
|
public long currentTimeNanos() {
|
||||||
try {
|
Instant now = Instant.now();
|
||||||
Object instant = now.invoke(null);
|
long epochSeconds = now.getEpochSecond();
|
||||||
int nanos = (int) getNano.invoke(instant);
|
return saturatedAdd(TimeUnit.SECONDS.toNanos(epochSeconds), now.getNano());
|
||||||
long epochSeconds = (long) getEpochSecond.invoke(instant);
|
|
||||||
return saturatedAdd(TimeUnit.SECONDS.toNanos(epochSeconds), nanos);
|
|
||||||
} catch (IllegalAccessException | InvocationTargetException ex) {
|
|
||||||
throw new RuntimeException(ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,8 @@ package io.grpc.internal;
|
||||||
final class TimeProviderResolverFactory {
|
final class TimeProviderResolverFactory {
|
||||||
static TimeProvider resolveTimeProvider() {
|
static TimeProvider resolveTimeProvider() {
|
||||||
try {
|
try {
|
||||||
Class<?> instantClass = Class.forName("java.time.Instant");
|
Class.forName("java.time.Instant");
|
||||||
return new InstantTimeProvider(instantClass);
|
return new InstantTimeProvider();
|
||||||
} catch (ClassNotFoundException ex) {
|
} catch (ClassNotFoundException ex) {
|
||||||
return new ConcurrentTimeProvider();
|
return new ConcurrentTimeProvider();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,7 @@ public class InstantTimeProviderTest {
|
||||||
@Test
|
@Test
|
||||||
public void testInstantCurrentTimeNanos() throws Exception {
|
public void testInstantCurrentTimeNanos() throws Exception {
|
||||||
|
|
||||||
InstantTimeProvider instantTimeProvider = new InstantTimeProvider(
|
InstantTimeProvider instantTimeProvider = new InstantTimeProvider();
|
||||||
Class.forName("java.time.Instant"));
|
|
||||||
|
|
||||||
// Get the current time from the InstantTimeProvider
|
// Get the current time from the InstantTimeProvider
|
||||||
long actualTimeNanos = instantTimeProvider.currentTimeNanos();
|
long actualTimeNanos = instantTimeProvider.currentTimeNanos();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue