This is an automated email from the ASF dual-hosted git repository. mattsicker pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 4c91f087bb64d4be037cfeb974908d5c250b1c93 Author: Matt Sicker <[email protected]> AuthorDate: Fri Nov 3 14:53:44 2023 -0500 Simplify KeyValuePair plugin bindings Signed-off-by: Matt Sicker <[email protected]> --- .../logging/log4j/core/util/KeyValuePair.java | 80 ++++------------------ .../log4j/jackson/json/layout/JsonLayoutTest.java | 6 +- 2 files changed, 15 insertions(+), 71 deletions(-) diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/KeyValuePair.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/KeyValuePair.java index 52c8452fdd..29c389265b 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/KeyValuePair.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/KeyValuePair.java @@ -16,10 +16,12 @@ */ package org.apache.logging.log4j.core.util; +import java.util.Objects; + import org.apache.logging.log4j.plugins.Configurable; +import org.apache.logging.log4j.plugins.Inject; import org.apache.logging.log4j.plugins.Plugin; -import org.apache.logging.log4j.plugins.PluginBuilderAttribute; -import org.apache.logging.log4j.plugins.PluginFactory; +import org.apache.logging.log4j.plugins.PluginAttribute; /** * Key/Value pair configuration item. @@ -34,11 +36,12 @@ public final class KeyValuePair { private final String value; /** - * Constructs a key/value pair. The constructor should only be called from test classes. + * Constructs a key/value pair. * @param key The key. * @param value The value. */ - public KeyValuePair(final String key, final String value) { + @Inject + public KeyValuePair(@PluginAttribute final String key, @PluginAttribute final String value) { this.key = key; this.value = value; } @@ -64,71 +67,16 @@ public final class KeyValuePair { return key + '=' + value; } - @PluginFactory - public static Builder newBuilder() { - return new Builder(); - } - - public static class Builder implements org.apache.logging.log4j.plugins.util.Builder<KeyValuePair> { - - @PluginBuilderAttribute - private String key; - - @PluginBuilderAttribute - private String value; - - public Builder setKey(final String aKey) { - this.key = aKey; - return this; - } - - public Builder setValue(final String aValue) { - this.value = aValue; - return this; - } - - @Override - public KeyValuePair build() { - return new KeyValuePair(key, value); - } - - } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((key == null) ? 0 : key.hashCode()); - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final KeyValuePair that = (KeyValuePair) o; + return Objects.equals(key, that.key) && Objects.equals(value, that.value); } @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final KeyValuePair other = (KeyValuePair) obj; - if (key == null) { - if (other.key != null) { - return false; - } - } else if (!key.equals(other.key)) { - return false; - } - if (value == null) { - if (other.value != null) { - return false; - } - } else if (!value.equals(other.value)) { - return false; - } - return true; + public int hashCode() { + return Objects.hash(key, value); } } diff --git a/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java b/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java index e10df9c5bc..af252875fc 100644 --- a/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java +++ b/log4j-layout-jackson-json/src/test/java/org/apache/logging/log4j/jackson/json/layout/JsonLayoutTest.java @@ -639,11 +639,7 @@ public class JsonLayoutTest { // Create the layout. final KeyValuePair[] additionalFields = { - KeyValuePair - .newBuilder() - .setKey("who") - .setValue("${ctx:WHO}") - .build() + new KeyValuePair("who", "${ctx:WHO}") }; final JsonLayout layout = JsonLayout .newBuilder()
