diff options
author | Philipp Wollermann <philwo@google.com> | 2017-09-04 22:23:30 +0200 |
---|---|---|
committer | Yun Peng <pcloudy@google.com> | 2017-09-05 09:54:51 +0200 |
commit | fd4f5ac67942c0a29e75903d04c403edb6ce4d7d (patch) | |
tree | 2bd3836daf989d70927e59c08c91c983e6ea828f | |
parent | 97abb524bacc6d8527744657642f79c25c843c59 (diff) |
Rewrite all code to use the new Java 8 java.time classes.
This removes our dependency on third_party/joda_time, which can be removed in the next commit.
Change-Id: Ibda131d34d0abdc2d675db4bfbd2e99480c055ee
PiperOrigin-RevId: 167515260
10 files changed, 61 insertions, 52 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/BUILD b/src/main/java/com/google/devtools/build/lib/BUILD index d29bee0988..cf061fe3c3 100644 --- a/src/main/java/com/google/devtools/build/lib/BUILD +++ b/src/main/java/com/google/devtools/build/lib/BUILD @@ -242,7 +242,6 @@ java_library( srcs = ["util/SingleLineFormatter.java"], deps = [ "//third_party:guava", - "//third_party:joda_time", ], ) @@ -637,7 +636,6 @@ java_library( "//third_party:android_common_25_0_0", "//third_party:auto_value", "//third_party:guava", - "//third_party:joda_time", "//third_party:jsr305", ], ) @@ -872,7 +870,6 @@ java_library( "//src/main/protobuf:extra_actions_base_java_proto", "//third_party:auto_value", "//third_party:guava", - "//third_party:joda_time", "//third_party:jsr305", "//third_party/protobuf:protobuf_java", ], @@ -957,7 +954,6 @@ java_library( "//src/main/protobuf:extra_actions_base_java_proto", "//third_party:auto_value", "//third_party:guava", - "//third_party:joda_time", "//third_party:jsr305", ], ) @@ -1105,7 +1101,6 @@ java_library( "//src/main/protobuf:invocation_policy_java_proto", "//src/main/protobuf:test_status_java_proto", "//third_party:guava", - "//third_party:joda_time", "//third_party:jsr305", "//third_party/grpc:grpc-jar", "//third_party/protobuf:protobuf_java", @@ -1295,5 +1290,4 @@ java_library( srcs = [ "analysis/BuildInfo.java", ], - deps = ["//third_party:joda_time"], ) diff --git a/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BUILD b/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BUILD index 91c8e523b4..940c62d023 100644 --- a/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BUILD +++ b/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BUILD @@ -19,7 +19,6 @@ java_library( "//third_party:auth", "//third_party:gson", "//third_party:guava", - "//third_party:joda_time", "//third_party:jsr305", "//third_party:netty", "//third_party/grpc:grpc-jar", diff --git a/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BuildEventServiceGrpcClient.java b/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BuildEventServiceGrpcClient.java index 6a32ddef90..7fd3cc20ff 100644 --- a/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BuildEventServiceGrpcClient.java +++ b/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BuildEventServiceGrpcClient.java @@ -34,15 +34,15 @@ import io.grpc.Status; import io.grpc.StatusRuntimeException; import io.grpc.stub.AbstractStub; import io.grpc.stub.StreamObserver; +import java.time.Duration; import java.util.concurrent.atomic.AtomicReference; import javax.annotation.Nullable; -import org.joda.time.Duration; /** Implementation of BuildEventServiceClient that uploads data using gRPC. */ public class BuildEventServiceGrpcClient implements BuildEventServiceClient { /** Max wait time for a single non-streaming RPC to finish */ - private static final Duration RPC_TIMEOUT = Duration.standardSeconds(15); + private static final Duration RPC_TIMEOUT = Duration.ofSeconds(15); private final PublishBuildEventStub besAsync; private final PublishBuildEventBlockingStub besBlocking; @@ -69,7 +69,7 @@ public class BuildEventServiceGrpcClient implements BuildEventServiceClient { @Override public Status publish(PublishLifecycleEventRequest lifecycleEvent) throws Exception { besBlocking - .withDeadlineAfter(RPC_TIMEOUT.getMillis(), MILLISECONDS) + .withDeadlineAfter(RPC_TIMEOUT.toMillis(), MILLISECONDS) .publishLifecycleEvent(lifecycleEvent); return Status.OK; } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBuildInfoFactory.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBuildInfoFactory.java index 7adaea6372..be7abfbb36 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBuildInfoFactory.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBuildInfoFactory.java @@ -24,12 +24,11 @@ import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.cmdline.RepositoryName; import com.google.devtools.build.lib.rules.java.WriteBuildInfoPropertiesAction.TimestampFormatter; import com.google.devtools.build.lib.vfs.PathFragment; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; /** * Java build info creation - generates properties file that contain the corresponding build-info @@ -46,16 +45,19 @@ public abstract class JavaBuildInfoFactory implements BuildInfoFactory { PathFragment.create("build-info-redacted.properties"); private static final DateTimeFormatter DEFAULT_TIME_FORMAT = - DateTimeFormat.forPattern("EEE MMM d HH:mm:ss yyyy"); + DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss yyyy"); // A default formatter that returns a date in UTC format. - private static final TimestampFormatter DEFAULT_FORMATTER = new TimestampFormatter() { - @Override - public String format(long timestamp) { - return new DateTime(timestamp, DateTimeZone.UTC).toString(DEFAULT_TIME_FORMAT) + " (" - + timestamp / 1000 + ')'; - } - }; + private static final TimestampFormatter DEFAULT_FORMATTER = + new TimestampFormatter() { + @Override + public String format(long timestamp) { + return Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC).format(DEFAULT_TIME_FORMAT) + + " (" + + timestamp / 1000 + + ')'; + } + }; @Override public final BuildInfoCollection create(BuildInfoContext context, BuildConfiguration config, diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandEventHandler.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandEventHandler.java index 081d76d527..6863b9045d 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandEventHandler.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandEventHandler.java @@ -27,12 +27,12 @@ import com.google.devtools.common.options.OptionsBase; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.EnumSet; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; /** * BlazeCommandEventHandler: an event handler established for the duration of a @@ -252,7 +252,7 @@ public class BlazeCommandEventHandler implements EventHandler { } private static final DateTimeFormatter TIMESTAMP_FORMAT = - DateTimeFormat.forPattern("(MM-dd HH:mm:ss.SSS) "); + DateTimeFormatter.ofPattern("(MM-dd HH:mm:ss.SSS) "); protected final OutErr outErr; @@ -359,6 +359,6 @@ public class BlazeCommandEventHandler implements EventHandler { * @return a string representing the current time, eg "04-26 13:47:32.124". */ protected String timestamp() { - return TIMESTAMP_FORMAT.print(System.currentTimeMillis()); + return TIMESTAMP_FORMAT.format(ZonedDateTime.now()); } } diff --git a/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalEventHandler.java b/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalEventHandler.java index def5a1e0a8..5756cbdf3e 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalEventHandler.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalEventHandler.java @@ -49,11 +49,12 @@ import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Logger; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; /** An experimental new output stream. */ public class ExperimentalEventHandler implements EventHandler { @@ -73,7 +74,8 @@ public class ExperimentalEventHandler implements EventHandler { static final long LONG_REFRESH_MILLIS = 20000L; private static final DateTimeFormatter TIMESTAMP_FORMAT = - DateTimeFormat.forPattern("(HH:mm:ss) "); + DateTimeFormatter.ofPattern("(HH:mm:ss) "); + private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("YYYY-MM-dd"); private final boolean cursorControl; private final Clock clock; @@ -266,7 +268,9 @@ public class ExperimentalEventHandler implements EventHandler { Event.info( null, "Current date is " - + DateTimeFormat.forPattern("YYYY-MM-dd").print(clock.currentTimeMillis()))); + + DATE_FORMAT.format( + Instant.ofEpochMilli(clock.currentTimeMillis()) + .atZone(ZoneId.systemDefault())))); } @Override @@ -350,7 +354,10 @@ public class ExperimentalEventHandler implements EventHandler { crlf(); } if (showTimestamp) { - terminal.writeString(TIMESTAMP_FORMAT.print(clock.currentTimeMillis())); + terminal.writeString( + TIMESTAMP_FORMAT.format( + Instant.ofEpochMilli(clock.currentTimeMillis()) + .atZone(ZoneId.systemDefault()))); } setEventKindColor(event.getKind()); terminal.writeString(event.getKind() + ": "); @@ -807,7 +814,9 @@ public class ExperimentalEventHandler implements EventHandler { } String timestamp = null; if (showTimestamp) { - timestamp = TIMESTAMP_FORMAT.print(clock.currentTimeMillis()); + timestamp = + TIMESTAMP_FORMAT.format( + Instant.ofEpochMilli(clock.currentTimeMillis()).atZone(ZoneId.systemDefault())); } stateTracker.writeProgressBar( terminalWriter, diff --git a/src/main/java/com/google/devtools/build/lib/runtime/FancyTerminalEventHandler.java b/src/main/java/com/google/devtools/build/lib/runtime/FancyTerminalEventHandler.java index fa74355b31..5996d3c6d0 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/FancyTerminalEventHandler.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/FancyTerminalEventHandler.java @@ -26,14 +26,14 @@ import com.google.devtools.build.lib.util.io.LineCountingAnsiTerminalWriter; import com.google.devtools.build.lib.util.io.LineWrappingAnsiTerminalWriter; import com.google.devtools.build.lib.util.io.OutErr; import java.io.IOException; +import java.time.Duration; +import java.time.Instant; import java.util.Calendar; import java.util.Iterator; import java.util.concurrent.ThreadLocalRandom; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.joda.time.Duration; -import org.joda.time.Instant; /** * An event handler for ANSI terminals which uses control characters to @@ -135,8 +135,9 @@ public class FancyTerminalEventHandler extends BlazeCommandEventHandler { if (!eventMask.contains(event.getKind())) { return; } - if (trySpecial && !EventKind.ERRORS_AND_WARNINGS_AND_OUTPUT.contains(event.getKind()) - && skipUntil.isAfterNow()) { + if (trySpecial + && !EventKind.ERRORS_AND_WARNINGS_AND_OUTPUT.contains(event.getKind()) + && skipUntil.isAfter(Instant.now())) { // Short-circuit here to avoid wiping out previous terminal contents. return; } @@ -151,7 +152,7 @@ public class FancyTerminalEventHandler extends BlazeCommandEventHandler { case START: { String message = event.getMessage(); - Pair<String,String> progressPair = matchProgress(message); + Pair<String, String> progressPair = matchProgress(message); if (progressPair != null) { progress(progressPair.getFirst(), progressPair.getSecond()); if (trySpecial && ThreadLocalRandom.current().nextInt(0, 20) == 0) { @@ -161,8 +162,9 @@ public class FancyTerminalEventHandler extends BlazeCommandEventHandler { previousLineErased = maybeOverwritePreviousMessage(); progress(progressPair.getFirst(), message); // Skip unimportant messages for a bit so that this message gets some exposure. - skipUntil = Instant.now().plus( - Duration.millis(ThreadLocalRandom.current().nextInt(3000, 8000))); + skipUntil = + Instant.now() + .plus(Duration.ofMillis(ThreadLocalRandom.current().nextInt(3000, 8000))); } } } else { diff --git a/src/main/java/com/google/devtools/build/lib/util/SingleLineFormatter.java b/src/main/java/com/google/devtools/build/lib/util/SingleLineFormatter.java index 6748917bdc..4155d2d5ed 100644 --- a/src/main/java/com/google/devtools/build/lib/util/SingleLineFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/util/SingleLineFormatter.java @@ -17,11 +17,12 @@ import com.google.common.collect.ImmutableRangeMap; import com.google.common.collect.Range; import java.io.PrintWriter; import java.io.StringWriter; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.util.logging.Formatter; import java.util.logging.Level; import java.util.logging.LogRecord; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; /** * Formatter to write java.util.logging messages out in single-line format. @@ -42,14 +43,15 @@ public class SingleLineFormatter extends Formatter { /** A thread safe, immutable formatter that can be used by all without contention. */ private static final DateTimeFormatter DATE_TIME_FORMAT = - DateTimeFormat.forPattern("yyMMdd HH:mm:ss.SSS").withZoneUTC(); + DateTimeFormatter.ofPattern("yyMMdd HH:mm:ss.SSS").withZone(ZoneOffset.UTC); @Override public String format(LogRecord rec) { StringBuilder buf = new StringBuilder(); // Timestamp - buf.append(DATE_TIME_FORMAT.print(rec.getMillis())) + buf.append( + DATE_TIME_FORMAT.format(Instant.ofEpochMilli(rec.getMillis()).atZone(ZoneOffset.UTC))) .append(':'); // One character code for level diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD index e238ab952b..f5e9c9d0b8 100644 --- a/src/test/java/com/google/devtools/build/lib/BUILD +++ b/src/test/java/com/google/devtools/build/lib/BUILD @@ -180,7 +180,6 @@ java_test( "//src/main/java/com/google/devtools/common/options", "//third_party:guava", "//third_party:guava-testlib", - "//third_party:joda_time", "//third_party:junit4", "//third_party:truth", ], diff --git a/src/test/java/com/google/devtools/build/lib/util/SingleLineFormatterTest.java b/src/test/java/com/google/devtools/build/lib/util/SingleLineFormatterTest.java index 3c9f38aca9..36881f7c93 100644 --- a/src/test/java/com/google/devtools/build/lib/util/SingleLineFormatterTest.java +++ b/src/test/java/com/google/devtools/build/lib/util/SingleLineFormatterTest.java @@ -14,11 +14,12 @@ package com.google.devtools.build.lib.util; import static com.google.common.truth.Truth.assertThat; +import static java.time.temporal.ChronoUnit.MILLIS; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.logging.Level; import java.util.logging.LogRecord; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -26,8 +27,8 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class SingleLineFormatterTest { - private static final DateTime TIMESTAMP = - new DateTime(2017, 04, 01, 17, 03, 43, 142, DateTimeZone.UTC); + private static final ZonedDateTime TIMESTAMP = + ZonedDateTime.of(2017, 04, 01, 17, 03, 43, 0, ZoneOffset.UTC).plus(142, MILLIS); @Test public void testFormat() { @@ -48,7 +49,8 @@ public class SingleLineFormatterTest { public void testTime() { LogRecord logRecord = createLogRecord( - Level.SEVERE, new DateTime(1999, 11, 30, 03, 04, 05, 722, DateTimeZone.UTC)); + Level.SEVERE, + ZonedDateTime.of(1999, 11, 30, 03, 04, 05, 0, ZoneOffset.UTC).plus(722, MILLIS)); assertThat(new SingleLineFormatter().format(logRecord)).contains("991130 03:04:05.722"); } @@ -63,14 +65,14 @@ public class SingleLineFormatterTest { + "\tat com.google.devtools.build.lib.util.SingleLineFormatterTest.testStackTrace"); } - private static LogRecord createLogRecord(Level level, DateTime dateTime) { + private static LogRecord createLogRecord(Level level, ZonedDateTime dateTime) { return createLogRecord(level, dateTime, null); } private static LogRecord createLogRecord( - Level level, DateTime dateTime, RuntimeException thrown) { + Level level, ZonedDateTime dateTime, RuntimeException thrown) { LogRecord record = new LogRecord(level, "some message"); - record.setMillis(dateTime.getMillis()); + record.setMillis(dateTime.toInstant().toEpochMilli()); record.setSourceClassName("SomeSourceClass"); record.setSourceMethodName("aSourceMethod"); record.setThreadID(543); |