From b689c8b15fdcfca78861da04b179266505613111 Mon Sep 17 00:00:00 2001 From: "David P. Baker" Date: Wed, 18 Dec 2024 13:41:02 -0800 Subject: [PATCH] Annotate `MetadataKey.emit...` methods to indicate that the value can be null. Fix implementations to account for null values (by ignoring nulls). RELNOTES=n/a PiperOrigin-RevId: 707654820 --- .../main/java/com/google/common/flogger/LogContext.java | 7 +++++-- .../main/java/com/google/common/flogger/MetadataKey.java | 8 ++++---- .../java/com/google/common/flogger/MetadataKeyTest.java | 2 +- .../flogger/backend/system/SimpleLogRecordTest.java | 3 ++- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/com/google/common/flogger/LogContext.java b/api/src/main/java/com/google/common/flogger/LogContext.java index 4ef485d0..9656884c 100644 --- a/api/src/main/java/com/google/common/flogger/LogContext.java +++ b/api/src/main/java/com/google/common/flogger/LogContext.java @@ -106,7 +106,7 @@ private Key() {} public static final MetadataKey LOG_SITE_GROUPING_KEY = new MetadataKey("group_by", Object.class, true) { @Override - public void emitRepeated(Iterator keys, KeyValueHandler out) { + public void emitRepeated(Iterator keys, KeyValueHandler out) { if (keys.hasNext()) { Object first = keys.next(); if (!keys.hasNext()) { @@ -175,7 +175,10 @@ public void emitRepeated(Iterator keys, KeyValueHandler out) { public static final MetadataKey TAGS = new MetadataKey("tags", Tags.class, false) { @Override - public void emit(Tags tags, KeyValueHandler out) { + public void emit(@Nullable Tags tags, KeyValueHandler out) { + if (tags == null) { + return; + } for (Map.Entry> e : tags.asMap().entrySet()) { Set values = e.getValue(); if (!values.isEmpty()) { diff --git a/api/src/main/java/com/google/common/flogger/MetadataKey.java b/api/src/main/java/com/google/common/flogger/MetadataKey.java index f671922f..072e6a59 100644 --- a/api/src/main/java/com/google/common/flogger/MetadataKey.java +++ b/api/src/main/java/com/google/common/flogger/MetadataKey.java @@ -171,7 +171,7 @@ public final boolean canRepeat() { * Emits one or more key/value pairs for the given metadata value. Call this method in preference * to using {@link #emit} directly to protect against unbounded reentrant logging. */ - public final void safeEmit(T value, KeyValueHandler kvh) { + public final void safeEmit(@Nullable T value, KeyValueHandler kvh) { if (isCustom && Platform.getCurrentRecursionDepth() > MAX_CUSTOM_METADATAKEY_RECURSION_DEPTH) { // Recursive logging detected, possibly caused by custom metadata keys triggering reentrant // logging. To halt recursion, emit the keys in the default non-custom format without invoking @@ -187,7 +187,7 @@ public final void safeEmit(T value, KeyValueHandler kvh) { * in preference to using {@link #emitRepeated} directly to protect against unbounded reentrant * logging. */ - public final void safeEmitRepeated(Iterator values, KeyValueHandler kvh) { + public final void safeEmitRepeated(Iterator values, KeyValueHandler kvh) { checkState(canRepeat, "non repeating key"); if (isCustom && Platform.getCurrentRecursionDepth() > MAX_CUSTOM_METADATAKEY_RECURSION_DEPTH) { // Recursive logging detected, possibly caused by custom metadata keys triggering reentrant @@ -236,7 +236,7 @@ public final void safeEmitRepeated(Iterator values, KeyValueHandler kvh) { * *

By default this method just calls {@code out.handle(getLabel(), value)}. */ - protected void emit(T value, KeyValueHandler kvh) { + protected void emit(@Nullable T value, KeyValueHandler kvh) { kvh.handle(getLabel(), value); } @@ -253,7 +253,7 @@ protected void emit(T value, KeyValueHandler kvh) { *

See the {@link #emit(Object,KeyValueHandler)} method for additional caveats for custom * implementations. */ - protected void emitRepeated(Iterator values, KeyValueHandler kvh) { + protected void emitRepeated(Iterator values, KeyValueHandler kvh) { while (values.hasNext()) { emit(values.next(), kvh); } diff --git a/api/src/test/java/com/google/common/flogger/MetadataKeyTest.java b/api/src/test/java/com/google/common/flogger/MetadataKeyTest.java index 278c3eaa..f37ad8d8 100644 --- a/api/src/test/java/com/google/common/flogger/MetadataKeyTest.java +++ b/api/src/test/java/com/google/common/flogger/MetadataKeyTest.java @@ -51,7 +51,7 @@ protected void emit(Object value, KeyValueHandler kvh) { } @Override - protected void emitRepeated(Iterator values, KeyValueHandler kvh) { + protected void emitRepeated(Iterator values, KeyValueHandler kvh) { // Hack for test to preserve the given values past a single use. In normal logging there // would be a new Metadata instance created for each of the reentrant logging calls. ImmutableList copy = ImmutableList.copyOf(values); diff --git a/api/src/test/java/com/google/common/flogger/backend/system/SimpleLogRecordTest.java b/api/src/test/java/com/google/common/flogger/backend/system/SimpleLogRecordTest.java index b59cc4df..a8e5ecdd 100644 --- a/api/src/test/java/com/google/common/flogger/backend/system/SimpleLogRecordTest.java +++ b/api/src/test/java/com/google/common/flogger/backend/system/SimpleLogRecordTest.java @@ -31,6 +31,7 @@ import java.util.Iterator; import java.util.logging.Level; import java.util.logging.LogRecord; +import org.jspecify.annotations.Nullable; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -42,7 +43,7 @@ public class SimpleLogRecordTest { private static final MetadataKey PATH_KEY = new MetadataKey("path", String.class, true) { @Override - public void emitRepeated(Iterator values, KeyValueHandler out) { + public void emitRepeated(Iterator values, KeyValueHandler out) { out.handle(getLabel(), Joiner.on('/').join(values)); } };