Skip to content

Commit

Permalink
Explicitly allow logVarargs to take an array containing null objects.
Browse files Browse the repository at this point in the history
This is already supported for log() calls which take @NullableDect Object
params and then pass them into logImpl (see
http://third_party/java_src/flogger/api/src/main/java/com/google/common/flogger/LogContext.java?rcl=289240473&l=896).

This is so a @FormatMethod which uses "@nullable Object... args" can pass the
args directly to logVarargs() without a static analysis test failing.

RELNOTES=Explicitly allow an array containing null objects in logVarargs().

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=291245533
  • Loading branch information
cgbatgoogle authored and cpovirk committed Jan 28, 2020
1 parent 91d4388 commit 0805f96
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ public final void log(String message, double p1, double p2) {
}

@Override
public final void logVarargs(String message, Object[] params) {
public final void logVarargs(String message, @NullableDecl Object[] params) {
if (shouldLog()) {
// Copy the varargs array (because we didn't create it and this is quite a rare case).
logImpl(message, Arrays.copyOf(params, params.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ API withInjectedLogSite(
* @param message the message template string containing a single argument placeholder.
* @param varargs the non-null array of arguments to be formatted.
*/
void logVarargs(String message, Object[] varargs);
void logVarargs(String message, @NullableDecl Object[] varargs);

/**
* Terminal log statement when a message is not required. A {@code log} method must terminate all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ public void testExplicitVarargs() {
FakeLoggerBackend backend = new FakeLoggerBackend();
FluentLogger logger = new FluentLogger(backend);

Object[] args = new Object[] {"foo", "bar", "baz"};
Object[] args = new Object[] {"foo", null, "baz"};
logger.atInfo().logVarargs("Any message ...", args);

backend.assertLastLogged().hasArguments("foo", "bar", "baz");
backend.assertLastLogged().hasArguments("foo", null, "baz");
// Make sure we took a copy of the arguments rather than risk re-using them.
assertThat(backend.getLoggedCount()).isEqualTo(1);
assertThat(backend.getLogged(0).getArguments()).isNotSameInstanceAs(args);
Expand Down

0 comments on commit 0805f96

Please sign in to comment.