diff --git a/spring-core/src/main/java/org/springframework/core/log/LogMessage.java b/spring-core/src/main/java/org/springframework/core/log/LogMessage.java index 4546f7a76ab8..c661f4a43e19 100644 --- a/spring-core/src/main/java/org/springframework/core/log/LogMessage.java +++ b/spring-core/src/main/java/org/springframework/core/log/LogMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ * format string ({@link String#format}) in its {@link #toString()}. * * @author Juergen Hoeller + * @author Sebastien Deleuze * @since 5.2 * @see #of(Supplier) * @see #format(String, Object) @@ -87,56 +88,57 @@ public static LogMessage of(Supplier supplier) { /** * Build a lazily formatted message from the given format string and argument. * @param format the format string (following {@link String#format} rules) - * @param arg1 the argument + * @param arg1 the argument (can be {@code null}) * @see String#format(String, Object...) */ - public static LogMessage format(String format, Object arg1) { + public static LogMessage format(String format, @Nullable Object arg1) { return new FormatMessage1(format, arg1); } /** * Build a lazily formatted message from the given format string and arguments. * @param format the format string (following {@link String#format} rules) - * @param arg1 the first argument - * @param arg2 the second argument + * @param arg1 the first argument (can be {@code null}) + * @param arg2 the second argument (can be {@code null}) * @see String#format(String, Object...) */ - public static LogMessage format(String format, Object arg1, Object arg2) { + public static LogMessage format(String format, @Nullable Object arg1, @Nullable Object arg2) { return new FormatMessage2(format, arg1, arg2); } /** * Build a lazily formatted message from the given format string and arguments. * @param format the format string (following {@link String#format} rules) - * @param arg1 the first argument - * @param arg2 the second argument - * @param arg3 the third argument + * @param arg1 the first argument (can be {@code null}) + * @param arg2 the second argument (can be {@code null}) + * @param arg3 the third argument (can be {@code null}) * @see String#format(String, Object...) */ - public static LogMessage format(String format, Object arg1, Object arg2, Object arg3) { + public static LogMessage format(String format, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3) { return new FormatMessage3(format, arg1, arg2, arg3); } /** * Build a lazily formatted message from the given format string and arguments. * @param format the format string (following {@link String#format} rules) - * @param arg1 the first argument - * @param arg2 the second argument - * @param arg3 the third argument - * @param arg4 the fourth argument + * @param arg1 the first argument (can be {@code null}) + * @param arg2 the second argument (can be {@code null}) + * @param arg3 the third argument (can be {@code null}) + * @param arg4 the fourth argument (can be {@code null}) * @see String#format(String, Object...) */ - public static LogMessage format(String format, Object arg1, Object arg2, Object arg3, Object arg4) { + public static LogMessage format(String format, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3, + @Nullable Object arg4) { return new FormatMessage4(format, arg1, arg2, arg3, arg4); } /** * Build a lazily formatted message from the given format string and varargs. * @param format the format string (following {@link String#format} rules) - * @param args the varargs array (costly, prefer individual arguments) + * @param args the varargs array (can be {@code null}, costly, prefer individual arguments) * @see String#format(String, Object...) */ - public static LogMessage format(String format, Object... args) { + public static LogMessage format(String format, @Nullable Object... args) { return new FormatMessageX(format, args); } @@ -170,9 +172,10 @@ private static abstract class FormatMessage extends LogMessage { private static final class FormatMessage1 extends FormatMessage { + @Nullable private final Object arg1; - FormatMessage1(String format, Object arg1) { + FormatMessage1(String format, @Nullable Object arg1) { super(format); this.arg1 = arg1; } @@ -186,11 +189,13 @@ protected String buildString() { private static final class FormatMessage2 extends FormatMessage { + @Nullable private final Object arg1; + @Nullable private final Object arg2; - FormatMessage2(String format, Object arg1, Object arg2) { + FormatMessage2(String format, @Nullable Object arg1, @Nullable Object arg2) { super(format); this.arg1 = arg1; this.arg2 = arg2; @@ -205,13 +210,16 @@ String buildString() { private static final class FormatMessage3 extends FormatMessage { + @Nullable private final Object arg1; + @Nullable private final Object arg2; + @Nullable private final Object arg3; - FormatMessage3(String format, Object arg1, Object arg2, Object arg3) { + FormatMessage3(String format, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3) { super(format); this.arg1 = arg1; this.arg2 = arg2; @@ -227,15 +235,20 @@ String buildString() { private static final class FormatMessage4 extends FormatMessage { + @Nullable private final Object arg1; + @Nullable private final Object arg2; + @Nullable private final Object arg3; + @Nullable private final Object arg4; - FormatMessage4(String format, Object arg1, Object arg2, Object arg3, Object arg4) { + FormatMessage4(String format, @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3, + @Nullable Object arg4) { super(format); this.arg1 = arg1; this.arg2 = arg2; @@ -252,9 +265,10 @@ String buildString() { private static final class FormatMessageX extends FormatMessage { + @Nullable private final Object[] args; - FormatMessageX(String format, Object... args) { + FormatMessageX(String format, @Nullable Object... args) { super(format); this.args = args; }