apache / logging-log4j2

Apache Log4j 2 is a versatile, feature-rich, efficient logging API and backend for Java.
https://logging.apache.org/log4j/2.x/
Apache License 2.0
3.37k stars 1.61k forks source link

Add support for injecting `null` values for wrapped primitive values #2362

Open ppkarwasz opened 7 months ago

ppkarwasz commented 7 months ago

If a component builder has a method:

public void setIncludeLocation(@PluginAttribute @Nullable Boolean includeLocation);

the instance factory will inject Boolean.FALSE instead of the expected null if the value is not present.

ppkarwasz commented 7 months ago

The problem seems to be in the @PluginAttribute annotation that has plethora of defaultFoo methods. The instance factory is not able therefore to distinguish between:

@PluginAttribute(defaulBoolean = false)

from:

@PluginAttribute

Personally I think we should reduce the complexity of the annotation to only include:

public @interface PluginAttribute {

    // The default value of the attribute or {@code null} if empty.
    String defaultValue() default String.EMPTY;

    // If not empty, gives the name of the system property that gives the default value.
    String defaultProperty() default String.EMPTY;

    String value() default Strings.EMPTY;

    boolean sensitive() default false;
}

Remark: trimming down the defaultFoo() is also good for documentation purposes. Using the aforementioned defaultValue/defaultProperty we can easily document the default value of the attribute in log4j-docgen.

ppkarwasz commented 7 months ago

BTW: I think that we can entirely eliminate the @PluginBuilderAttribute (replaced by @PluginAttribute) and @PluginFactory (replaced by @Factory) annotations.