Merge "Fix JdkObsolete issues with Date (part 1)"
diff --git a/java/com/google/gerrit/gpg/PublicKeyChecker.java b/java/com/google/gerrit/gpg/PublicKeyChecker.java
index 27530e7..0a96212 100644
--- a/java/com/google/gerrit/gpg/PublicKeyChecker.java
+++ b/java/com/google/gerrit/gpg/PublicKeyChecker.java
@@ -32,9 +32,9 @@
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.common.GpgKeyInfo.Status;
import java.io.IOException;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -62,7 +62,7 @@
private PublicKeyStore store;
private Map<Long, Fingerprint> trusted;
private int maxTrustDepth;
- private Date effectiveTime = new Date();
+ private Instant effectiveTime = Instant.now();
/**
* Enable web-of-trust checks.
@@ -111,12 +111,12 @@
* @param effectiveTime effective time.
* @return a reference to this object.
*/
- public PublicKeyChecker setEffectiveTime(Date effectiveTime) {
+ public PublicKeyChecker setEffectiveTime(Instant effectiveTime) {
this.effectiveTime = effectiveTime;
return this;
}
- protected Date getEffectiveTime() {
+ protected Instant getEffectiveTime() {
return effectiveTime;
}
@@ -183,13 +183,13 @@
return CheckResult.create(status, problems);
}
- private CheckResult checkBasic(PGPPublicKey key, Date now) {
+ private CheckResult checkBasic(PGPPublicKey key, Instant now) {
List<String> problems = new ArrayList<>(2);
gatherRevocationProblems(key, now, problems);
long validMs = key.getValidSeconds() * 1000;
if (validMs != 0) {
- long msSinceCreation = now.getTime() - key.getCreationTime().getTime();
+ long msSinceCreation = now.toEpochMilli() - getCreationTime(key).toEpochMilli();
if (msSinceCreation > validMs) {
problems.add("Key is expired");
}
@@ -197,7 +197,7 @@
return CheckResult.create(problems);
}
- private void gatherRevocationProblems(PGPPublicKey key, Date now, List<String> problems) {
+ private void gatherRevocationProblems(PGPPublicKey key, Instant now, List<String> problems) {
try {
List<PGPSignature> revocations = new ArrayList<>();
Map<Long, RevocationKey> revokers = new HashMap<>();
@@ -216,7 +216,7 @@
}
private static boolean isRevocationValid(
- PGPSignature revocation, RevocationReason reason, Date now) {
+ PGPSignature revocation, RevocationReason reason, Instant now) {
// RFC4880 states:
// "If a key has been revoked because of a compromise, all signatures
// created by that key are suspect. However, if it was merely superseded or
@@ -226,11 +226,14 @@
// consider the revocation reason and timestamp when checking whether a
// signature (data or certification) is valid.
return reason.getRevocationReason() == KEY_COMPROMISED
- || revocation.getCreationTime().before(now);
+ || PushCertificateChecker.getCreationTime(revocation).isBefore(now);
}
private PGPSignature scanRevocations(
- PGPPublicKey key, Date now, List<PGPSignature> revocations, Map<Long, RevocationKey> revokers)
+ PGPPublicKey key,
+ Instant now,
+ List<PGPSignature> revocations,
+ Map<Long, RevocationKey> revokers)
throws PGPException {
@SuppressWarnings("unchecked")
Iterator<PGPSignature> allSigs = key.getSignatures();
@@ -305,7 +308,7 @@
if (rk.getAlgorithm() != revoker.getAlgorithm()) {
continue;
}
- if (!checkBasic(rk, revocation.getCreationTime()).isOk()) {
+ if (!checkBasic(rk, PushCertificateChecker.getCreationTime(revocation)).isOk()) {
// Revoker's key was expired or revoked at time of revocation, so the
// revocation is invalid.
continue;
@@ -469,4 +472,9 @@
}
return null;
}
+
+ @SuppressWarnings("JdkObsolete")
+ private static Instant getCreationTime(PGPPublicKey key) {
+ return key.getCreationTime().toInstant();
+ }
}
diff --git a/java/com/google/gerrit/gpg/PushCertificateChecker.java b/java/com/google/gerrit/gpg/PushCertificateChecker.java
index 1f88dfb..17ca5a4 100644
--- a/java/com/google/gerrit/gpg/PushCertificateChecker.java
+++ b/java/com/google/gerrit/gpg/PushCertificateChecker.java
@@ -25,6 +25,7 @@
import com.google.gerrit.extensions.common.GpgKeyInfo.Status;
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.bcpg.ArmoredInputStream;
@@ -205,7 +206,7 @@
null, CheckResult.bad("Signature by " + keyIdToString(sig.getKeyID()) + " is not valid"));
}
CheckResult result =
- publicKeyChecker.setStore(store).setEffectiveTime(sig.getCreationTime()).check(signer);
+ publicKeyChecker.setStore(store).setEffectiveTime(getCreationTime(sig)).check(signer);
if (!result.getProblems().isEmpty()) {
StringBuilder err =
new StringBuilder("Invalid public key ")
@@ -216,4 +217,9 @@
}
return new Result(signer, result);
}
+
+ @SuppressWarnings("JdkObsolete")
+ public static Instant getCreationTime(PGPSignature signature) {
+ return signature.getCreationTime().toInstant();
+ }
}
diff --git a/java/com/google/gerrit/server/git/WorkQueue.java b/java/com/google/gerrit/server/git/WorkQueue.java
index 0cab14a..3032bfe 100644
--- a/java/com/google/gerrit/server/git/WorkQueue.java
+++ b/java/com/google/gerrit/server/git/WorkQueue.java
@@ -31,9 +31,9 @@
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.lang.reflect.Field;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
@@ -496,7 +496,7 @@
private final Executor executor;
private final int taskId;
private final AtomicBoolean running;
- private final Date startTime;
+ private final Instant startTime;
Task(Runnable runnable, RunnableScheduledFuture<V> task, Executor executor, int taskId) {
this.runnable = runnable;
@@ -504,7 +504,7 @@
this.executor = executor;
this.taskId = taskId;
this.running = new AtomicBoolean();
- this.startTime = new Date();
+ this.startTime = Instant.now();
}
public int getTaskId() {
@@ -527,7 +527,7 @@
return State.SLEEPING;
}
- public Date getStartTime() {
+ public Instant getStartTime() {
return startTime;
}
diff --git a/java/com/google/gerrit/server/restapi/config/ListTasks.java b/java/com/google/gerrit/server/restapi/config/ListTasks.java
index eac9653..a12cb5b 100644
--- a/java/com/google/gerrit/server/restapi/config/ListTasks.java
+++ b/java/com/google/gerrit/server/restapi/config/ListTasks.java
@@ -36,7 +36,7 @@
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
-import java.sql.Timestamp;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -119,7 +119,7 @@
public static class TaskInfo {
public String id;
public Task.State state;
- public Timestamp startTime;
+ public Instant startTime;
public long delay;
public String command;
public String remoteName;
@@ -129,7 +129,7 @@
public TaskInfo(Task<?> task) {
this.id = HexFormat.fromInt(task.getTaskId());
this.state = task.getState();
- this.startTime = new Timestamp(task.getStartTime().getTime());
+ this.startTime = task.getStartTime();
this.delay = task.getDelay(TimeUnit.MILLISECONDS);
this.command = task.toString();
this.queueName = task.getQueueName();
diff --git a/java/com/google/gerrit/sshd/commands/ShowCaches.java b/java/com/google/gerrit/sshd/commands/ShowCaches.java
index 02956f7..62bdb32 100644
--- a/java/com/google/gerrit/sshd/commands/ShowCaches.java
+++ b/java/com/google/gerrit/sshd/commands/ShowCaches.java
@@ -44,8 +44,8 @@
import com.google.inject.Inject;
import java.io.IOException;
import java.text.SimpleDateFormat;
+import java.time.Instant;
import java.util.Collection;
-import java.util.Date;
import java.util.Map;
import org.apache.sshd.common.io.IoAcceptor;
import org.apache.sshd.common.io.IoSession;
@@ -114,13 +114,14 @@
protected void run() throws Failure {
enableGracefulStop();
nw = columns - 50;
- Date now = new Date();
+ Instant now = Instant.now();
stdout.format(
"%-25s %-20s now %16s\n",
"Gerrit Code Review",
Version.getVersion() != null ? Version.getVersion() : "",
new SimpleDateFormat("HH:mm:ss zzz").format(now));
- stdout.format("%-25s %-20s uptime %16s\n", "", "", uptime(now.getTime() - serverStarted));
+ stdout.format(
+ "%-25s %-20s uptime %16s\n", "", "", uptime(now.toEpochMilli() - serverStarted));
stdout.print('\n');
try {
diff --git a/java/com/google/gerrit/sshd/commands/ShowConnections.java b/java/com/google/gerrit/sshd/commands/ShowConnections.java
index 7eeb770..5efeb42 100644
--- a/java/com/google/gerrit/sshd/commands/ShowConnections.java
+++ b/java/com/google/gerrit/sshd/commands/ShowConnections.java
@@ -34,8 +34,9 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
import java.util.Optional;
import org.apache.sshd.common.io.IoAcceptor;
import org.apache.sshd.common.io.IoSession;
@@ -171,10 +172,13 @@
}
private static String time(long now, long time) {
+ Instant instant = Instant.ofEpochMilli(time);
if (now - time < 24 * 60 * 60 * 1000L) {
- return new SimpleDateFormat("HH:mm:ss").format(new Date(time));
+ return DateTimeFormatter.ofPattern("HH:mm:ss")
+ .withZone(ZoneId.systemDefault())
+ .format(instant);
}
- return new SimpleDateFormat("MMM-dd").format(new Date(time));
+ return DateTimeFormatter.ofPattern("MMM-dd").withZone(ZoneId.systemDefault()).format(instant);
}
private static String age(long age) {
diff --git a/java/com/google/gerrit/sshd/commands/ShowQueue.java b/java/com/google/gerrit/sshd/commands/ShowQueue.java
index 779f2df..928cdda 100644
--- a/java/com/google/gerrit/sshd/commands/ShowQueue.java
+++ b/java/com/google/gerrit/sshd/commands/ShowQueue.java
@@ -35,8 +35,9 @@
import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject;
import java.io.IOException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import org.apache.sshd.server.Environment;
@@ -178,19 +179,23 @@
}
private static String time(long now, long delay) {
- Date when = new Date(now + delay);
+ Instant when = Instant.ofEpochMilli(now + delay);
return format(when, delay);
}
- private static String startTime(Date when) {
- return format(when, TimeUtil.nowMs() - when.getTime());
+ private static String startTime(Instant when) {
+ return format(when, TimeUtil.nowMs() - when.toEpochMilli());
}
- private static String format(Date when, long timeFromNow) {
+ private static String format(Instant when, long timeFromNow) {
if (timeFromNow < 24 * 60 * 60 * 1000L) {
- return new SimpleDateFormat("HH:mm:ss.SSS").format(when);
+ return DateTimeFormatter.ofPattern("HH:mm:ss.SSS")
+ .withZone(ZoneId.systemDefault())
+ .format(when);
}
- return new SimpleDateFormat("MMM-dd HH:mm").format(when);
+ return DateTimeFormatter.ofPattern("MMM-dd HH:mm")
+ .withZone(ZoneId.systemDefault())
+ .format(when);
}
private static String format(Task.State state) {
diff --git a/javatests/com/google/gerrit/gpg/PublicKeyCheckerTest.java b/javatests/com/google/gerrit/gpg/PublicKeyCheckerTest.java
index 7703fb0..8bafafe 100644
--- a/javatests/com/google/gerrit/gpg/PublicKeyCheckerTest.java
+++ b/javatests/com/google/gerrit/gpg/PublicKeyCheckerTest.java
@@ -38,11 +38,12 @@
import static org.junit.Assert.assertEquals;
import com.google.gerrit.gpg.testing.TestKey;
-import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
-import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -212,9 +213,11 @@
String problem = "Key is revoked (key material has been compromised): test6 compromised";
assertProblems(k, problem);
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- PublicKeyChecker checker =
- new PublicKeyChecker().setStore(store).setEffectiveTime(df.parse("2010-01-01 12:00:00"));
+ Instant instant =
+ DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
+ .withZone(ZoneId.systemDefault())
+ .parse("2010-01-01 12:00:00", Instant::from);
+ PublicKeyChecker checker = new PublicKeyChecker().setStore(store).setEffectiveTime(instant);
assertProblems(checker, k, problem);
}
@@ -360,8 +363,8 @@
+ " is valid, but key is not trusted";
}
- private static Date parseDate(String str) throws Exception {
- return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(str);
+ private static Instant parseDate(String str) throws Exception {
+ return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z").parse(str, Instant::from);
}
private static List<String> list(String first, String[] rest) {