diff --git a/jdk/src/java.logging/share/classes/java/util/logging/LogRecord.java b/jdk/src/java.logging/share/classes/java/util/logging/LogRecord.java index 7cf7aee1eec..4a75c82bcee 100644 --- a/jdk/src/java.logging/share/classes/java/util/logging/LogRecord.java +++ b/jdk/src/java.logging/share/classes/java/util/logging/LogRecord.java @@ -468,12 +468,11 @@ public class LogRecord implements java.io.Serializable { * @implSpec This is equivalent to calling * {@link #getInstant() getInstant().toEpochMilli()}. * - * @deprecated To get the full nanosecond resolution event time, + * @apiNote To get the full nanosecond resolution event time, * use {@link #getInstant()}. * * @see #getInstant() */ - @Deprecated public long getMillis() { return instant.toEpochMilli(); } @@ -487,8 +486,10 @@ public class LogRecord implements java.io.Serializable { * {@link #setInstant(java.time.Instant) * setInstant(Instant.ofEpochMilli(millis))}. * - * @deprecated To set event time with nanosecond resolution, - * use {@link #setInstant(java.time.Instant)}. + * @deprecated LogRecord maintains timestamps with nanosecond resolution, + * using {@link Instant} values. For this reason, + * {@link #setInstant(java.time.Instant) setInstant()} + * should be used in preference to {@code setMillis()}. * * @see #setInstant(java.time.Instant) */ @@ -510,14 +511,23 @@ public class LogRecord implements java.io.Serializable { /** * Sets the instant that the event occurred. + *
+ * If the given {@code instant} represents a point on the time-line too + * far in the future or past to fit in a {@code long} milliseconds and + * nanoseconds adjustment, then an {@code ArithmeticException} will be + * thrown. * * @param instant the instant that the event occurred. * * @throws NullPointerException if {@code instant} is null. + * @throws ArithmeticException if numeric overflow would occur while + * calling {@link Instant#toEpochMilli() instant.toEpochMilli()}. + * * @since 1.9 */ public void setInstant(Instant instant) { - this.instant = Objects.requireNonNull(instant); + instant.toEpochMilli(); + this.instant = instant; } /** diff --git a/jdk/test/java/util/logging/HigherResolutionTimeStamps/LogRecordWithNanosAPI.java b/jdk/test/java/util/logging/HigherResolutionTimeStamps/LogRecordWithNanosAPI.java index 0c6cfce652b..a858a8a7c9c 100644 --- a/jdk/test/java/util/logging/HigherResolutionTimeStamps/LogRecordWithNanosAPI.java +++ b/jdk/test/java/util/logging/HigherResolutionTimeStamps/LogRecordWithNanosAPI.java @@ -33,7 +33,7 @@ import java.util.logging.SimpleFormatter; /** * @test - * @bug 8072645 + * @bug 8072645 8144262 * @summary tests the new methods added to LogRecord. * @run main LogRecordWithNanosAPI * @author danielfuchs @@ -299,10 +299,37 @@ public class LogRecordWithNanosAPI { try { record.setInstant(null); + throw new RuntimeException("Expected NullPointerException not thrown"); } catch (NullPointerException x) { System.out.println("Got expected NPE when trying to call record.setInstant(null): " + x); } + // This instant is the biggest for which toEpochMilli will not throw... + final Instant max = Instant.ofEpochMilli(Long.MAX_VALUE).plusNanos(999_999L); + record.setInstant(max); + assertEquals(Long.MAX_VALUE / 1000L, + record.getInstant().getEpochSecond(), + "max instant seconds [record.getInstant().getEpochSecond()]"); + assertEquals(Long.MAX_VALUE, + record.getInstant().toEpochMilli(), + "max instant millis [record.getInstant().toEpochMilli()]"); + assertEquals(Long.MAX_VALUE, record.getMillis(), + "max instant millis [record.getMillis()]"); + assertEquals((Long.MAX_VALUE % 1000L)*1000_000L + 999_999L, + record.getInstant().getNano(), + "max instant nanos [record.getInstant().getNano()]"); + + // Too big by 1 ns. + final Instant tooBig = max.plusNanos(1L); + try { + record.setInstant(tooBig); + throw new RuntimeException("Expected ArithmeticException not thrown"); + } catch (ArithmeticException x) { + System.out.println("Got expected ArithmeticException when trying" + + " to call record.setInstant(Instant.ofEpochMilli(Long.MAX_VALUE)" + + ".plusNanos(999_999L).plusNanos(1L)): " + x); + } + } }