Skip to content

Simplify name and alias annotation processing #3877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Named> {
@Override
public Optional<String> getSpecifiedName(final Named annotation) {
return ofNullable(trimToNull(annotation.value()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -107,18 +105,11 @@ void forField() {

@Retention(RetentionPolicy.RUNTIME)
@QualifierType
@NameProvider(CustomQualifierNameProvider.class)
@NameProvider
@interface CustomQualifier {
String value();
}

static class CustomQualifierNameProvider implements AnnotatedElementNameProvider<CustomQualifier> {
@Override
public Optional<String> getSpecifiedName(final CustomQualifier annotation) {
return Optional.of(annotation.value());
}
}

@Retention(RetentionPolicy.RUNTIME)
@Namespace("logical")
@interface LogicalNamespace {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@
* 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;

/**
* 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<? extends AnnotatedElementNameProvider<? extends Annotation>> value();
int offset() default 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends AnnotatedElementAliasesProvider<? extends Annotation>> value();
}
public @interface NameProvider {}
Loading
Loading