diff --git a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/Named.java b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/Named.java index 9fe8f7493ae..293f54af462 100644 --- a/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/Named.java +++ b/log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/Named.java @@ -16,17 +16,12 @@ */ package org.apache.logging.log4j.core.test.junit; -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import java.util.Optional; -import org.apache.logging.log4j.plugins.name.AnnotatedElementNameProvider; -import org.apache.logging.log4j.plugins.name.NameProvider; +import org.apache.logging.log4j.plugins.NameProvider; /** * Specifies the name of an {@link org.apache.logging.log4j.core.Appender} to inject into JUnit 5 tests from the specified @@ -38,17 +33,10 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) @Documented -@NameProvider(Named.Provider.class) +@NameProvider public @interface Named { /** * Specifies the name of the configuration item to inject. If blank, uses the name of the annotated parameter. */ String value() default ""; - - class Provider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final Named annotation) { - return ofNullable(trimToNull(annotation.value())); - } - } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAliases.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAliases.java index cd0352821ba..2f6e7f4bdf0 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAliases.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAliases.java @@ -21,8 +21,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.apache.logging.log4j.core.config.plugins.util.PluginAliasesProvider; -import org.apache.logging.log4j.plugins.name.AliasesProvider; +import org.apache.logging.log4j.plugins.AliasesProvider; /** * Identifies a list of aliases for a Plugin, PluginAttribute, or PluginBuilderAttribute. @@ -31,7 +30,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.TYPE, ElementType.FIELD}) -@AliasesProvider(PluginAliasesProvider.class) +@AliasesProvider @Deprecated(since = "3.0", forRemoval = true) @SuppressWarnings("removal") public @interface PluginAliases { diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAttribute.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAttribute.java index bdd50b7a41f..97eb9db19da 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAttribute.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginAttribute.java @@ -21,9 +21,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.apache.logging.log4j.core.config.plugins.util.PluginAttributeNameProvider; +import org.apache.logging.log4j.plugins.NameProvider; import org.apache.logging.log4j.plugins.QualifierType; -import org.apache.logging.log4j.plugins.name.NameProvider; import org.apache.logging.log4j.util.Strings; /** @@ -37,7 +36,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.FIELD}) -@NameProvider(PluginAttributeNameProvider.class) +@NameProvider @QualifierType @Deprecated(since = "3.0", forRemoval = true) @SuppressWarnings("removal") diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginBuilderAttribute.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginBuilderAttribute.java index 1255b01c1b6..952f57a6938 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginBuilderAttribute.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginBuilderAttribute.java @@ -21,9 +21,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.apache.logging.log4j.core.config.plugins.util.PluginBuilderAttributeNameProvider; +import org.apache.logging.log4j.plugins.NameProvider; import org.apache.logging.log4j.plugins.QualifierType; -import org.apache.logging.log4j.plugins.name.NameProvider; import org.apache.logging.log4j.util.Strings; /** @@ -33,7 +32,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.FIELD}) -@NameProvider(PluginBuilderAttributeNameProvider.class) +@NameProvider @QualifierType @Deprecated(since = "3.0", forRemoval = true) @SuppressWarnings("removal") diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginElement.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginElement.java index 112853c4947..a09240e528d 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginElement.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginElement.java @@ -21,9 +21,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.apache.logging.log4j.core.config.plugins.util.PluginElementNameProvider; +import org.apache.logging.log4j.plugins.NameProvider; import org.apache.logging.log4j.plugins.QualifierType; -import org.apache.logging.log4j.plugins.name.NameProvider; /** * Identifies a parameter as a Plugin and corresponds with an XML element (or equivalent) in configuration files. @@ -32,7 +31,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.FIELD}) -@NameProvider(PluginElementNameProvider.class) +@NameProvider @QualifierType @Deprecated(since = "3.0", forRemoval = true) @SuppressWarnings("removal") diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginValue.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginValue.java index d26b5351137..b42e1bf20e3 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginValue.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginValue.java @@ -21,9 +21,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.apache.logging.log4j.core.config.plugins.util.PluginValueNameProvider; +import org.apache.logging.log4j.plugins.NameProvider; import org.apache.logging.log4j.plugins.QualifierType; -import org.apache.logging.log4j.plugins.name.NameProvider; /** * Identifies a parameter as a value. These correspond with property values generally, but are meant as values to be @@ -35,7 +34,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.FIELD}) -@NameProvider(PluginValueNameProvider.class) +@NameProvider @QualifierType @Deprecated(since = "3.0", forRemoval = true) @SuppressWarnings("removal") diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginAliasesProvider.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginAliasesProvider.java deleted file mode 100644 index b83d62165f4..00000000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginAliasesProvider.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.core.config.plugins.util; - -import java.util.Collection; -import java.util.List; -import org.apache.logging.log4j.core.config.plugins.PluginAliases; -import org.apache.logging.log4j.plugins.name.AnnotatedElementAliasesProvider; - -@Deprecated(since = "3.0", forRemoval = true) -@SuppressWarnings("removal") -public class PluginAliasesProvider implements AnnotatedElementAliasesProvider { - @Override - public Collection getAliases(final PluginAliases annotation) { - return List.of(annotation.value()); - } -} diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginAttributeNameProvider.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginAttributeNameProvider.java deleted file mode 100644 index 91b34613db6..00000000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginAttributeNameProvider.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.core.config.plugins.util; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.core.config.plugins.PluginAttribute; -import org.apache.logging.log4j.plugins.name.AnnotatedElementNameProvider; - -@Deprecated(since = "3.0", forRemoval = true) -@SuppressWarnings("removal") -public class PluginAttributeNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final PluginAttribute annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilderAttributeNameProvider.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilderAttributeNameProvider.java deleted file mode 100644 index e4a3747722e..00000000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilderAttributeNameProvider.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.core.config.plugins.util; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; -import org.apache.logging.log4j.plugins.name.AnnotatedElementNameProvider; - -@Deprecated(since = "3.0", forRemoval = true) -@SuppressWarnings("removal") -public class PluginBuilderAttributeNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final PluginBuilderAttribute annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginElementNameProvider.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginElementNameProvider.java deleted file mode 100644 index e4efb370c71..00000000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginElementNameProvider.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.core.config.plugins.util; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.core.config.plugins.PluginElement; -import org.apache.logging.log4j.plugins.name.AnnotatedElementNameProvider; - -@Deprecated(since = "3.0", forRemoval = true) -@SuppressWarnings("removal") -public class PluginElementNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final PluginElement annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginValueNameProvider.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginValueNameProvider.java deleted file mode 100644 index 18d849066e2..00000000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginValueNameProvider.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.core.config.plugins.util; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.core.config.plugins.PluginValue; -import org.apache.logging.log4j.plugins.name.AnnotatedElementNameProvider; - -@Deprecated(since = "3.0", forRemoval = true) -@SuppressWarnings("removal") -public class PluginValueNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final PluginValue annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/di/KeyTest.java b/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/di/KeyTest.java index 0bdf7249772..c50ddbc9f61 100644 --- a/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/di/KeyTest.java +++ b/log4j-plugins-test/src/test/java/org/apache/logging/log4j/plugins/di/KeyTest.java @@ -25,13 +25,11 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Parameter; -import java.util.Optional; +import org.apache.logging.log4j.plugins.NameProvider; import org.apache.logging.log4j.plugins.Named; import org.apache.logging.log4j.plugins.Namespace; import org.apache.logging.log4j.plugins.Ordered; import org.apache.logging.log4j.plugins.QualifierType; -import org.apache.logging.log4j.plugins.name.AnnotatedElementNameProvider; -import org.apache.logging.log4j.plugins.name.NameProvider; import org.junit.jupiter.api.Test; // TODO: add tests for more complex types with generics etc @@ -107,18 +105,11 @@ void forField() { @Retention(RetentionPolicy.RUNTIME) @QualifierType - @NameProvider(CustomQualifierNameProvider.class) + @NameProvider @interface CustomQualifier { String value(); } - static class CustomQualifierNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final CustomQualifier annotation) { - return Optional.of(annotation.value()); - } - } - @Retention(RetentionPolicy.RUNTIME) @Namespace("logical") @interface LogicalNamespace {} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/NameProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/AliasesProvider.java similarity index 72% rename from log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/NameProvider.java rename to log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/AliasesProvider.java index 38ab42a2321..02df1a67c0b 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/NameProvider.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/AliasesProvider.java @@ -14,9 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.plugins.name; +package org.apache.logging.log4j.plugins; -import java.lang.annotation.Annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -24,14 +23,16 @@ import java.lang.annotation.Target; /** - * Meta annotation for specifying named annotations and how to get a specified name from the named annotation. + * Marks another annotation as one providing aliases for an object. The annotation must define an element + * named {@code value} of type {@code String[]} that contains the aliases. An offset can define where to + * begin reading the aliases. */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) -public @interface NameProvider { +public @interface AliasesProvider { /** - * Strategy for extracting a specified name from an annotated element. + * Offset within the {@code value} array to read aliases from. */ - Class> value(); + int offset() default 0; } diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/AliasesProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/NameProvider.java similarity index 76% rename from log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/AliasesProvider.java rename to log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/NameProvider.java index d93b947a953..3073520a21b 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/AliasesProvider.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/NameProvider.java @@ -14,18 +14,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.plugins.name; +package org.apache.logging.log4j.plugins; -import java.lang.annotation.Annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * Marks another annotation as one providing a name for an object. The name is obtained from + * the annotation element named {@code value}. This element can be a {@code String} or {@code String[]}. + * When specified as an array, the first element is used. + */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) -public @interface AliasesProvider { - Class> value(); -} +public @interface NameProvider {} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Named.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Named.java index 6f284f9b4b6..4df623283c0 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Named.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Named.java @@ -21,9 +21,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.apache.logging.log4j.plugins.name.AliasesProvider; -import org.apache.logging.log4j.plugins.name.NameProvider; -import org.apache.logging.log4j.plugins.name.NamedQualifierNameProvider; /** * General naming qualifier for injectable objects. The first value given is the primary name while subsequent values are @@ -32,8 +29,8 @@ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE_USE}) @Documented -@NameProvider(NamedQualifierNameProvider.class) -@AliasesProvider(NamedQualifierNameProvider.class) +@NameProvider +@AliasesProvider(offset = 1) @QualifierType public @interface Named { /** diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Namespace.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Namespace.java index 4612cc1dfcd..7980e1b6ac2 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Namespace.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Namespace.java @@ -23,7 +23,7 @@ import java.lang.annotation.Target; /** - * Annotations to separate {@link org.apache.logging.log4j.plugins.name.NameProvider} names into namespaces. + * Annotations to separate {@link NameProvider} names into namespaces. * For example, the {@linkplain Configurable Core namespace} is used with the {@link Node} API, while the TypeConverter * namespace is used with the {@link org.apache.logging.log4j.plugins.convert.TypeConverter} API. * This can be annotated on another annotation to create an alias annotation for a common namespace. diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Plugin.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Plugin.java index 29ad60bee13..013ea7727e3 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Plugin.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/Plugin.java @@ -22,8 +22,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.apache.logging.log4j.plugins.model.PluginService; -import org.apache.logging.log4j.plugins.name.NameProvider; -import org.apache.logging.log4j.plugins.name.PluginNameProvider; import org.apache.logging.log4j.util.Strings; /** @@ -44,7 +42,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) -@NameProvider(PluginNameProvider.class) +@NameProvider public @interface Plugin { /** diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginAliases.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginAliases.java index 0e9f22668bf..9562d88b0f5 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginAliases.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginAliases.java @@ -21,8 +21,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.apache.logging.log4j.plugins.name.AliasesProvider; -import org.apache.logging.log4j.plugins.name.PluginAliasesProvider; /** * Identifies a list of aliases for an annotated plugin element. This is supported by plugin classes and other element @@ -31,7 +29,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) -@AliasesProvider(PluginAliasesProvider.class) +@AliasesProvider public @interface PluginAliases { /** diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginAttribute.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginAttribute.java index 9ac1128a0af..e248e6aeb7d 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginAttribute.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginAttribute.java @@ -24,8 +24,6 @@ import java.util.function.Supplier; import org.apache.logging.log4j.plugins.convert.TypeConverter; import org.apache.logging.log4j.plugins.di.resolver.PluginAttributeFactoryResolver; -import org.apache.logging.log4j.plugins.name.NameProvider; -import org.apache.logging.log4j.plugins.name.PluginAttributeNameProvider; import org.apache.logging.log4j.util.Strings; /** @@ -57,7 +55,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE_USE}) -@NameProvider(PluginAttributeNameProvider.class) +@NameProvider @QualifierType public @interface PluginAttribute { diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginBuilderAttribute.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginBuilderAttribute.java index b539e59cfd6..9d5fe39daf5 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginBuilderAttribute.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginBuilderAttribute.java @@ -21,8 +21,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.apache.logging.log4j.plugins.name.NameProvider; -import org.apache.logging.log4j.plugins.name.PluginBuilderAttributeNameProvider; import org.apache.logging.log4j.util.Strings; /** @@ -33,7 +31,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.TYPE, ElementType.TYPE_USE}) -@NameProvider(PluginBuilderAttributeNameProvider.class) +@NameProvider @QualifierType public @interface PluginBuilderAttribute { diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginElement.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginElement.java index 1ddba1a4153..c5d0dcd1786 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginElement.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginElement.java @@ -22,8 +22,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.apache.logging.log4j.plugins.di.resolver.PluginElementFactoryResolver; -import org.apache.logging.log4j.plugins.name.NameProvider; -import org.apache.logging.log4j.plugins.name.PluginElementNameProvider; import org.apache.logging.log4j.util.Strings; /** @@ -36,7 +34,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE_USE}) -@NameProvider(PluginElementNameProvider.class) +@NameProvider @QualifierType @Configurable public @interface PluginElement { diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginValue.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginValue.java index 8d4f98d5753..e724a940fe5 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginValue.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/PluginValue.java @@ -21,8 +21,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.apache.logging.log4j.plugins.name.NameProvider; -import org.apache.logging.log4j.plugins.name.PluginValueNameProvider; /** * Identifies a Plugin Value and its corresponding attribute alias for configuration formats that don't distinguish @@ -37,7 +35,7 @@ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE_USE}) -@NameProvider(PluginValueNameProvider.class) +@NameProvider @QualifierType public @interface PluginValue { diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/Keys.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/Keys.java index d4f2ae7f8d8..564448e51e7 100644 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/Keys.java +++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/Keys.java @@ -20,21 +20,19 @@ import java.lang.reflect.AnnotatedElement; import java.lang.reflect.AnnotatedType; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Parameter; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Optional; +import org.apache.logging.log4j.plugins.AliasesProvider; +import org.apache.logging.log4j.plugins.NameProvider; import org.apache.logging.log4j.plugins.Namespace; import org.apache.logging.log4j.plugins.internal.util.AnnotatedAnnotation; import org.apache.logging.log4j.plugins.internal.util.AnnotationUtil; import org.apache.logging.log4j.plugins.internal.util.BeanUtils; -import org.apache.logging.log4j.plugins.name.AliasesProvider; -import org.apache.logging.log4j.plugins.name.AnnotatedElementAliasesProvider; -import org.apache.logging.log4j.plugins.name.AnnotatedElementNameProvider; -import org.apache.logging.log4j.plugins.name.NameProvider; -import org.apache.logging.log4j.plugins.util.ReflectionUtil; -import org.apache.logging.log4j.util.Cast; import org.apache.logging.log4j.util.Strings; public final class Keys { @@ -66,9 +64,9 @@ public static boolean hasName(final AnnotatedElement element) { } /** - * Gets the name of the annotated field using the corresponding {@link AnnotatedElementNameProvider} + * Gets the name of the annotated field using the corresponding {@link NameProvider} * strategy for the named annotation on the field. If no named annotations are present, then an empty string - * is returned. If no {@linkplain AnnotatedElementNameProvider#getSpecifiedName(Annotation) specified name} is given + * is returned. If no specified name is given * by the name provider, the {@linkplain Field#getName() field name} is returned. * * @param field annotated field to find name for @@ -79,9 +77,9 @@ public static String getName(final Field field) { } /** - * Gets the name of the given annotated parameter using the corresponding {@link AnnotatedElementNameProvider} + * Gets the name of the given annotated parameter using the corresponding {@link NameProvider} * strategy for the named annotation on the parameter. If no named annotations are present, then an empty string - * is returned. If no {@linkplain AnnotatedElementNameProvider#getSpecifiedName(Annotation) specified name} is given + * is returned. If no specified name is given * by the name provider, the {@linkplain Parameter#getName() parameter name} is returned. * * @param parameter annotated parameter to find name for @@ -92,9 +90,9 @@ public static String getName(final Parameter parameter) { } /** - * Gets the name of the given annotated method using the corresponding {@link AnnotatedElementNameProvider} + * Gets the name of the given annotated method using the corresponding {@link NameProvider} * strategy for the named annotation on the method. If no named annotations are present, then an empty string - * is returned. If no {@linkplain AnnotatedElementNameProvider#getSpecifiedName(Annotation) specified name} is given by + * is returned. If no specified name is given by * the name provider, the {@linkplain Method#getName() method name} is used with {@code is}, {@code set}, {@code get}, * and {@code with} prefixes removed and the result being de-capitalized. * @@ -120,9 +118,9 @@ public static String getName(final Method method) { } /** - * Gets the name of the given annotated type using the corresponding {@link AnnotatedElementNameProvider} + * Gets the name of the given annotated type using the corresponding {@link NameProvider} * strategy for the named annotation on the type. If no named annotations are present or if no - * {@linkplain AnnotatedElementNameProvider#getSpecifiedName(Annotation) specified name} is given by the name provider, + * specified name is given by the name provider, * then an empty string is returned. * * @param annotatedType annotated type to find name for @@ -133,9 +131,9 @@ public static String getName(final AnnotatedType annotatedType) { } /** - * Gets the name of the given annotated class using the corresponding {@link AnnotatedElementNameProvider} + * Gets the name of the given annotated class using the corresponding {@link NameProvider} * strategy for the named annotation on the class. If no named annotations are present, then an empty - * string is returned. If no {@linkplain AnnotatedElementNameProvider#getSpecifiedName(Annotation) specified name} is given + * string is returned. If no specified name is given * by the name provider, then the {@linkplain Class#getSimpleName() simple name} of the annotated class is returned. * * @param type annotated class to find name for @@ -145,19 +143,24 @@ public static String getName(final Class type) { return hasName(type) ? getSpecifiedName(type).orElseGet(type::getSimpleName) : Strings.EMPTY; } - // TODO(ms): these name and alias providers should work via DI private static Optional getSpecifiedName(final AnnotatedElement element) { - return AnnotationUtil.findAnnotatedAnnotations(element, NameProvider.class) - .findFirst() - .flatMap(Keys::getSpecifiedName); + var annotation = AnnotationUtil.getElementAnnotationHavingMetaAnnotation(element, NameProvider.class); + return Optional.ofNullable(annotation).flatMap(Keys::getSpecifiedName); } - private static Optional getSpecifiedName( - final AnnotatedAnnotation annotatedAnnotation) { - final Class> providerType = - Cast.cast(annotatedAnnotation.metaAnnotation().value()); - final AnnotatedElementNameProvider provider = ReflectionUtil.instantiate(providerType); - return provider.getSpecifiedName(annotatedAnnotation.annotation()); + private static Optional getSpecifiedName(final Annotation annotation) { + try { + final Method nameProvidingElement = annotation.annotationType().getDeclaredMethod("value"); + final Object value = nameProvidingElement.invoke(annotation); + if (value instanceof final String string) { + return Optional.ofNullable(Strings.trimToNull(string)); + } + if (value instanceof final String[] array && array.length > 0) { + return Optional.ofNullable(Strings.trimToNull(array[0])); + } + } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { + } + return Optional.empty(); } public static Collection getAliases(final AnnotatedElement element) { @@ -169,9 +172,18 @@ public static Collection getAliases(final AnnotatedElement element) { private static Collection getAliases( final AnnotatedAnnotation annotatedAnnotation) { - final Class> providerType = - Cast.cast(annotatedAnnotation.metaAnnotation().value()); - final AnnotatedElementAliasesProvider provider = ReflectionUtil.instantiate(providerType); - return provider.getAliases(annotatedAnnotation.annotation()); + final A annotation = annotatedAnnotation.annotation(); + try { + final Method valueMethod = annotation.annotationType().getDeclaredMethod("value"); + final String[] value = (String[]) valueMethod.invoke(annotation); + final List list = Arrays.asList(value); + if (!list.isEmpty()) { + final AliasesProvider aliasesProvider = annotatedAnnotation.metaAnnotation(); + final int offset = aliasesProvider.offset(); + return offset > 0 ? list.subList(offset, list.size()) : list; + } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { + } + return List.of(); } } diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/AnnotatedElementAliasesProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/AnnotatedElementAliasesProvider.java deleted file mode 100644 index 36695b0fae2..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/AnnotatedElementAliasesProvider.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import java.lang.annotation.Annotation; -import java.util.Collection; - -public interface AnnotatedElementAliasesProvider { - Collection getAliases(final A annotation); -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/AnnotatedElementNameProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/AnnotatedElementNameProvider.java deleted file mode 100644 index 78bacb86927..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/AnnotatedElementNameProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import java.lang.annotation.Annotation; -import java.util.Optional; - -/** - * Extracts a specified name for some configurable annotated element. A specified name is one given in a non-empty - * string in an annotation as opposed to relying on the default name taken from the annotated element itself. - * - * @param plugin configuration annotation - */ -public interface AnnotatedElementNameProvider { - /** - * Returns the specified name from this annotation if given or {@code Optional.empty()} if none given. - * - * @param annotation annotation value of configuration element - * @return specified name of configuration element or empty if none specified - */ - Optional getSpecifiedName(final A annotation); -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/CategoryNameProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/CategoryNameProvider.java deleted file mode 100644 index 8772f7d217e..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/CategoryNameProvider.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.plugins.Namespace; - -public class CategoryNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final Namespace annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/NamedQualifierNameProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/NamedQualifierNameProvider.java deleted file mode 100644 index edeb7a6d934..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/NamedQualifierNameProvider.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Collection; -import java.util.List; -import java.util.Optional; -import org.apache.logging.log4j.plugins.Named; - -public class NamedQualifierNameProvider - implements AnnotatedElementNameProvider, AnnotatedElementAliasesProvider { - @Override - public Optional getSpecifiedName(final Named annotation) { - final String[] names = annotation.value(); - if (names == null || names.length == 0) { - return Optional.empty(); - } - return ofNullable(trimToNull(names[0])); - } - - @Override - public Collection getAliases(final Named annotation) { - final String[] names = annotation.value(); - if (names == null || names.length <= 1) { - return List.of(); - } - final String[] aliases = new String[names.length - 1]; - for (int i = 0; i < aliases.length; i++) { - aliases[i] = names[i + 1].trim(); - } - return List.of(aliases); - } -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginAliasesProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginAliasesProvider.java deleted file mode 100644 index 79dcdf2dcdb..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginAliasesProvider.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import java.util.Collection; -import java.util.List; -import org.apache.logging.log4j.plugins.PluginAliases; - -public class PluginAliasesProvider implements AnnotatedElementAliasesProvider { - @Override - public Collection getAliases(final PluginAliases annotation) { - return List.of(annotation.value()); - } -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginAttributeNameProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginAttributeNameProvider.java deleted file mode 100644 index 37f8a1544b6..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginAttributeNameProvider.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.plugins.PluginAttribute; - -public class PluginAttributeNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final PluginAttribute annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginBuilderAttributeNameProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginBuilderAttributeNameProvider.java deleted file mode 100644 index c61cb4dd4c9..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginBuilderAttributeNameProvider.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.plugins.PluginBuilderAttribute; - -public class PluginBuilderAttributeNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final PluginBuilderAttribute annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginElementNameProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginElementNameProvider.java deleted file mode 100644 index ec3c3dd10d9..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginElementNameProvider.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.plugins.PluginElement; - -public class PluginElementNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final PluginElement annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginNameProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginNameProvider.java deleted file mode 100644 index 5072ed6347e..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginNameProvider.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.plugins.Plugin; - -public class PluginNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final Plugin annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginValueNameProvider.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginValueNameProvider.java deleted file mode 100644 index 2bcaa27d076..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/PluginValueNameProvider.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.logging.log4j.plugins.name; - -import static java.util.Optional.ofNullable; -import static org.apache.logging.log4j.util.Strings.trimToNull; - -import java.util.Optional; -import org.apache.logging.log4j.plugins.PluginValue; - -public class PluginValueNameProvider implements AnnotatedElementNameProvider { - @Override - public Optional getSpecifiedName(final PluginValue annotation) { - return ofNullable(trimToNull(annotation.value())); - } -} diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/package-info.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/package-info.java deleted file mode 100644 index 6458d1520ff..00000000000 --- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/name/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ - -/** - * Strategies for extracting names and aliases from annotations. - */ -@Export -@Version("1.0.0") -package org.apache.logging.log4j.plugins.name; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.versioning.Version; diff --git a/src/changelog/.3.x.x/3870_simplify_name_aliases_providers.xml b/src/changelog/.3.x.x/3870_simplify_name_aliases_providers.xml new file mode 100644 index 00000000000..ee97e70c221 --- /dev/null +++ b/src/changelog/.3.x.x/3870_simplify_name_aliases_providers.xml @@ -0,0 +1,11 @@ + + + + + Simplifies how the `@NameProvider` and `@AliasesProvider` plugin meta-annotations work. + This reduces the need for reflection and simplifies name/alias extraction based on how they normally work. + + diff --git a/src/site/antora/modules/ROOT/pages/manual/dependencyinjection.adoc b/src/site/antora/modules/ROOT/pages/manual/dependencyinjection.adoc index e6686c541a6..e2b8619fea1 100644 --- a/src/site/antora/modules/ROOT/pages/manual/dependencyinjection.adoc +++ b/src/site/antora/modules/ROOT/pages/manual/dependencyinjection.adoc @@ -53,7 +53,7 @@ Injection points are injectable fields or parameters where a dependency should b _Injectable fields_ are fields annotated with `@Inject` or a qualifier annotation. _Injectable methods_ are methods annotated with `@Inject` or are not annotated with a factory annotation and have at least one parameter annotated with a qualifier annotation. _Injectable constructors_ are constructors annotated with `@Inject`; only one such constructor should exist per class. -When a field or parameter is annotated with a name-providing annotation (i.e., an annotation annotated with `@org.apache.logging.log4j.plugins.name.NameProvider`), then the provided name or name of the field or parameter are included in the `Key` for the injection point. +When a field or parameter is annotated with a name-providing annotation (i.e., an annotation annotated with `@org.apache.logging.log4j.plugins.NameProvider`), then the provided name or name of the field or parameter are included in the `Key` for the injection point. When these elements are annotated with a `@Namespace` annotation or meta-annotation, then that namespace name is included in the `Key` for the injection point. Similarly, when a field or parameter is annotated with a qualifier annotation, then that qualifier annotation type is included in the `Key` for the injection point. @@ -114,8 +114,8 @@ Qualifiers provide a way to match dependencies and factories based on more than For example, the `@org.apache.logging.log4j.plugins.Named` qualifier allows for creating different bindings of the same type with different names (along with support for aliases). Qualifiers on an injection point request a binding with that qualifier type and name. Qualifiers on a factory method register a binding with that qualifier type and name. -The name for a qualifier is provided via an `org.apache.logging.log4j.plugins.name.AnnotatedElementNameProvider` strategy class given in the `@NameProvider` annotation declared on the qualifier annotation. -Aliases are likewise provided via an `org.apache.logging.log4j.plugins.name.AnnotatedElementAliasesProvider` strategy class given in the `@AliasesProvider` annotated declared on the qualifier annotation. +The name for a qualifier is provided via a `@NameProvider` annotation declared on the qualifier annotation. +Aliases are likewise provided via a `@AliasesProvider` annotated declared on the qualifier annotation. === Scopes