You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2020/03/09 00:07:44 UTC

[logging-log4j2] branch mean-bean-machine updated (3aeec75 -> 37e122b)

This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a change to branch mean-bean-machine
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git.


    from 3aeec75  Remove BeanInstance class
     new 03be319  Remove stereotypes
     new 37e122b  Simplify InjectionFactory into Injector

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../logging/log4j/plugins/api/Stereotype.java      | 34 --------
 .../plugins/defaults/bean/AbstractProducer.java    | 11 ++-
 .../plugins/defaults/bean/DefaultBeanManager.java  |  6 +-
 .../defaults/bean/DefaultInjectionFactory.java     | 92 ----------------------
 .../defaults/bean/DefaultInjectionTarget.java      | 18 ++---
 .../bean/DefaultInjectionTargetFactory.java        | 10 +--
 .../plugins/defaults/bean/DefaultInjector.java     | 82 +++++++++++++++++++
 .../plugins/defaults/bean/MethodProducer.java      |  5 +-
 .../defaults/model/DefaultElementManager.java      | 35 +-------
 .../log4j/plugins/spi/bean/InjectionFactory.java   | 47 -----------
 .../logging/log4j/plugins/spi/bean/Injector.java   | 43 ++++++++++
 .../log4j/plugins/test/BeanJUnit4Runner.java       | 12 +--
 12 files changed, 156 insertions(+), 239 deletions(-)
 delete mode 100644 log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/api/Stereotype.java
 delete mode 100644 log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionFactory.java
 create mode 100644 log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjector.java
 delete mode 100644 log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/spi/bean/InjectionFactory.java
 create mode 100644 log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/spi/bean/Injector.java


[logging-log4j2] 02/02: Simplify InjectionFactory into Injector

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a commit to branch mean-bean-machine
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 37e122b9c33b0a085d45cdc888383a4764553fce
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sun Mar 8 18:58:09 2020 -0500

    Simplify InjectionFactory into Injector
    
    Signed-off-by: Matt Sicker <bo...@gmail.com>
---
 .../plugins/defaults/bean/AbstractProducer.java    | 11 ++-
 .../plugins/defaults/bean/DefaultBeanManager.java  |  6 +-
 .../defaults/bean/DefaultInjectionFactory.java     | 92 ----------------------
 .../defaults/bean/DefaultInjectionTarget.java      | 18 ++---
 .../bean/DefaultInjectionTargetFactory.java        | 10 +--
 .../plugins/defaults/bean/DefaultInjector.java     | 82 +++++++++++++++++++
 .../plugins/defaults/bean/MethodProducer.java      |  5 +-
 .../log4j/plugins/spi/bean/InjectionFactory.java   | 47 -----------
 .../logging/log4j/plugins/spi/bean/Injector.java   | 43 ++++++++++
 .../log4j/plugins/test/BeanJUnit4Runner.java       | 12 +--
 10 files changed, 155 insertions(+), 171 deletions(-)

diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/AbstractProducer.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/AbstractProducer.java
index b7069fb..9882f9e 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/AbstractProducer.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/AbstractProducer.java
@@ -19,11 +19,11 @@ package org.apache.logging.log4j.plugins.defaults.bean;
 
 import org.apache.logging.log4j.plugins.spi.bean.Bean;
 import org.apache.logging.log4j.plugins.spi.bean.BeanManager;
-import org.apache.logging.log4j.plugins.spi.bean.InjectionFactory;
+import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
+import org.apache.logging.log4j.plugins.spi.bean.Injector;
 import org.apache.logging.log4j.plugins.spi.bean.Producer;
 import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
 import org.apache.logging.log4j.plugins.spi.model.MetaMethod;
-import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
 
 import java.lang.reflect.Type;
 import java.util.Collection;
@@ -35,7 +35,7 @@ abstract class AbstractProducer<D, T> implements Producer<T> {
     private final Bean<D> declaringBean;
     private final MetaMethod<D, ?> disposerMethod;
     private final Collection<InjectionPoint<?>> disposerInjectionPoints;
-    final InjectionFactory injectionFactory;
+    final Injector injector;
 
     AbstractProducer(final BeanManager beanManager, final Bean<D> declaringBean, final MetaMethod<D, ?> disposerMethod,
                      final Collection<InjectionPoint<?>> disposerInjectionPoints) {
@@ -43,7 +43,7 @@ abstract class AbstractProducer<D, T> implements Producer<T> {
         this.declaringBean = declaringBean;
         this.disposerMethod = disposerMethod;
         this.disposerInjectionPoints = Objects.requireNonNull(disposerInjectionPoints);
-        this.injectionFactory = new DefaultInjectionFactory(beanManager);
+        this.injector = new DefaultInjector(beanManager);
     }
 
     // context is managed separately as the declaring instance is only used for producing the object and is not a dependent of the bean
@@ -68,8 +68,7 @@ abstract class AbstractProducer<D, T> implements Producer<T> {
             // as producer and disposer bean is unrelated to this bean, we need to recreate it on demand
             try (final InitializationContext<D> context = createContext()) {
                 final D declaringInstance = disposerMethod.isStatic() ? null : getDeclaringInstance(context);
-                injectionFactory.forDisposerMethod(disposerMethod, declaringInstance, disposerInjectionPoints, instance)
-                        .accept(context);
+                injector.dispose(declaringInstance, disposerMethod, disposerInjectionPoints, instance, context);
             }
         }
     }
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultBeanManager.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultBeanManager.java
index b1a5334..d759f72 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultBeanManager.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultBeanManager.java
@@ -32,8 +32,8 @@ import org.apache.logging.log4j.plugins.spi.ValidationException;
 import org.apache.logging.log4j.plugins.spi.bean.Bean;
 import org.apache.logging.log4j.plugins.spi.bean.BeanManager;
 import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
-import org.apache.logging.log4j.plugins.spi.bean.InjectionFactory;
 import org.apache.logging.log4j.plugins.spi.bean.InjectionTargetFactory;
+import org.apache.logging.log4j.plugins.spi.bean.Injector;
 import org.apache.logging.log4j.plugins.spi.bean.ProducerFactory;
 import org.apache.logging.log4j.plugins.spi.bean.ScopeContext;
 import org.apache.logging.log4j.plugins.spi.model.ElementManager;
@@ -71,7 +71,7 @@ import java.util.stream.Stream;
 public class DefaultBeanManager implements BeanManager {
 
     private final ElementManager elementManager;
-    private final InjectionFactory injectionFactory = new DefaultInjectionFactory(this);
+    private final Injector injector = new DefaultInjector(this);
 
     private final Collection<Bean<?>> enabledBeans = ConcurrentHashMap.newKeySet();
     private final Map<Type, Collection<Bean<?>>> beansByType = new ConcurrentHashMap<>();
@@ -105,7 +105,7 @@ public class DefaultBeanManager implements BeanManager {
         if (elementManager.isInjectable(metaClass)) {
             final Variable<T> variable = elementManager.createVariable(metaClass);
             final InjectionTargetFactory<T> factory =
-                    new DefaultInjectionTargetFactory<>(elementManager, injectionFactory, metaClass);
+                    new DefaultInjectionTargetFactory<>(elementManager, injector, metaClass);
             created = addBean(new InjectionTargetBean<>(variable, metaClass, factory));
         } else {
             created = null;
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionFactory.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionFactory.java
deleted file mode 100644
index 48fd80a..0000000
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionFactory.java
+++ /dev/null
@@ -1,92 +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.defaults.bean;
-
-import org.apache.logging.log4j.plugins.api.Disposes;
-import org.apache.logging.log4j.plugins.spi.bean.BeanManager;
-import org.apache.logging.log4j.plugins.spi.bean.InjectionFactory;
-import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
-import org.apache.logging.log4j.plugins.spi.model.MetaConstructor;
-import org.apache.logging.log4j.plugins.spi.model.MetaField;
-import org.apache.logging.log4j.plugins.spi.model.MetaMethod;
-import org.apache.logging.log4j.plugins.spi.model.MetaParameter;
-import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.function.Consumer;
-import java.util.function.Function;
-
-public class DefaultInjectionFactory implements InjectionFactory {
-
-    private final BeanManager beanManager;
-
-    public DefaultInjectionFactory(final BeanManager beanManager) {
-        this.beanManager = beanManager;
-    }
-
-    private Object[] createArguments(final List<MetaParameter<?>> parameters,
-                                     final Collection<InjectionPoint<?>> injectionPoints,
-                                     final InitializationContext<?> context, final Object producedInstance) {
-        return parameters.stream().map(parameter ->
-                parameter.isAnnotationPresent(Disposes.class) ? producedInstance : injectionPoints.stream()
-                        .filter(point -> parameter.equals(point.getElement()))
-                        .findAny()
-                        .flatMap(point -> beanManager.getInjectableValue(point, context))
-                        .orElseThrow(() -> new UnsupportedOperationException("TODO: primitives and defaults")))
-                .toArray();
-    }
-
-    @Override
-    public <T> Function<InitializationContext<T>, T> forConstructor(final MetaConstructor<T> constructor,
-                                                                    final Collection<InjectionPoint<?>> injectionPoints) {
-        return context -> constructor.construct(
-                createArguments(constructor.getParameters(), injectionPoints, context, null));
-    }
-
-    @Override
-    public <D, T> Function<InitializationContext<T>, T> forProducerMethod(final MetaMethod<D, T> producerMethod,
-                                                                          final D declaringInstance,
-                                                                          final Collection<InjectionPoint<?>> injectionPoints) {
-        return context -> producerMethod.invoke(declaringInstance,
-                createArguments(producerMethod.getParameters(), injectionPoints, context, null));
-    }
-
-    @Override
-    public <T> Consumer<InitializationContext<T>> forDisposerMethod(final MetaMethod<T, ?> disposerMethod,
-                                                                    final T declaringInstance,
-                                                                    final Collection<InjectionPoint<?>> injectionPoints,
-                                                                    final Object producedInstance) {
-        return context -> disposerMethod.invoke(declaringInstance,
-                createArguments(disposerMethod.getParameters(), injectionPoints, context, producedInstance));
-    }
-
-    @Override
-    public <D, T> Consumer<InitializationContext<D>> forField(final MetaField<D, T> field, final D declaringInstance,
-                                                              final InjectionPoint<T> injectionPoint) {
-        return context -> beanManager.getInjectableValue(injectionPoint, context)
-                .ifPresent(value -> field.set(declaringInstance, value));
-    }
-
-    @Override
-    public <D, T> Consumer<InitializationContext<D>> forMethod(final MetaMethod<D, T> method, final D declaringInstance,
-                                                               final Collection<InjectionPoint<?>> injectionPoints) {
-        return context -> method.invoke(declaringInstance,
-                createArguments(method.getParameters(), injectionPoints, context, null));
-    }
-}
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionTarget.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionTarget.java
index 0b8e308..869fa00 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionTarget.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionTarget.java
@@ -20,8 +20,9 @@ package org.apache.logging.log4j.plugins.defaults.bean;
 import org.apache.logging.log4j.plugins.api.Disposes;
 import org.apache.logging.log4j.plugins.api.Inject;
 import org.apache.logging.log4j.plugins.api.Produces;
-import org.apache.logging.log4j.plugins.spi.bean.InjectionFactory;
+import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
 import org.apache.logging.log4j.plugins.spi.bean.InjectionTarget;
+import org.apache.logging.log4j.plugins.spi.bean.Injector;
 import org.apache.logging.log4j.plugins.spi.model.ElementManager;
 import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
 import org.apache.logging.log4j.plugins.spi.model.MetaClass;
@@ -30,7 +31,6 @@ import org.apache.logging.log4j.plugins.spi.model.MetaElement;
 import org.apache.logging.log4j.plugins.spi.model.MetaField;
 import org.apache.logging.log4j.plugins.spi.model.MetaMember;
 import org.apache.logging.log4j.plugins.spi.model.MetaMethod;
-import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
 import org.apache.logging.log4j.plugins.util.TypeUtil;
 
 import java.util.Collection;
@@ -42,17 +42,17 @@ import java.util.Set;
 import java.util.stream.Collectors;
 
 class DefaultInjectionTarget<T> implements InjectionTarget<T> {
-    private final InjectionFactory injectionFactory;
+    private final Injector injector;
     private final ElementManager elementManager;
     private final Collection<InjectionPoint<?>> injectionPoints;
     private final MetaConstructor<T> constructor;
     private final List<MetaMethod<T, ?>> postConstructMethods;
     private final List<MetaMethod<T, ?>> preDestroyMethods;
 
-    DefaultInjectionTarget(final InjectionFactory injectionFactory, final ElementManager elementManager,
+    DefaultInjectionTarget(final Injector injector, final ElementManager elementManager,
                            final Collection<InjectionPoint<?>> injectionPoints, final MetaConstructor<T> constructor,
                            final List<MetaMethod<T, ?>> postConstructMethods, final List<MetaMethod<T, ?>> preDestroyMethods) {
-        this.injectionFactory = injectionFactory;
+        this.injector = injector;
         this.elementManager = elementManager;
         this.injectionPoints = Objects.requireNonNull(injectionPoints);
         this.constructor = Objects.requireNonNull(constructor);
@@ -65,7 +65,7 @@ class DefaultInjectionTarget<T> implements InjectionTarget<T> {
         final Set<InjectionPoint<?>> constructorInjectionPoints = injectionPoints.stream()
                 .filter(point -> constructor.equals(point.getMember()))
                 .collect(Collectors.toSet());
-        return injectionFactory.forConstructor(constructor, constructorInjectionPoints).apply(context);
+        return injector.construct(constructor, constructorInjectionPoints, context);
     }
 
     @Override
@@ -76,7 +76,7 @@ class DefaultInjectionTarget<T> implements InjectionTarget<T> {
         final MetaClass<T> metaClass = elementManager.getMetaClass(clazz);
         for (final MetaMethod<T, ?> method : metaClass.getMethods()) {
             if (method.isAnnotationPresent(Inject.class) && method.getParameters().isEmpty()) {
-                injectionFactory.forMethod(method, instance, Collections.emptySet()).accept(context);
+                injector.invoke(instance, method, Collections.emptySet(), context);
             }
         }
     }
@@ -91,7 +91,7 @@ class DefaultInjectionTarget<T> implements InjectionTarget<T> {
         final MetaElement<F> element = point.getElement();
         if (element instanceof MetaField<?, ?> && ((MetaField<?, F>) element).getDeclaringClass().getJavaClass().isInstance(instance)) {
             final MetaField<T, F> field = (MetaField<T, F>) element;
-            injectionFactory.forField(field, instance, point).accept(context);
+            injector.set(instance, field, point, context);
         }
     }
 
@@ -107,7 +107,7 @@ class DefaultInjectionTarget<T> implements InjectionTarget<T> {
                 final Set<InjectionPoint<?>> methodInjectionPoints = injectionPoints.stream()
                         .filter(p -> method.equals(p.getMember()))
                         .collect(Collectors.toSet());
-                injectionFactory.forMethod(method, instance, methodInjectionPoints).accept(context);
+                injector.invoke(instance, method, methodInjectionPoints, context);
                 injectedMethods.add(method);
             }
         }
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionTargetFactory.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionTargetFactory.java
index 029911e..d048a14 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionTargetFactory.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjectionTargetFactory.java
@@ -22,9 +22,9 @@ import org.apache.logging.log4j.plugins.api.PostConstruct;
 import org.apache.logging.log4j.plugins.api.PreDestroy;
 import org.apache.logging.log4j.plugins.spi.InjectionException;
 import org.apache.logging.log4j.plugins.spi.bean.Bean;
-import org.apache.logging.log4j.plugins.spi.bean.InjectionFactory;
 import org.apache.logging.log4j.plugins.spi.bean.InjectionTarget;
 import org.apache.logging.log4j.plugins.spi.bean.InjectionTargetFactory;
+import org.apache.logging.log4j.plugins.spi.bean.Injector;
 import org.apache.logging.log4j.plugins.spi.model.ElementManager;
 import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
 import org.apache.logging.log4j.plugins.spi.model.MetaClass;
@@ -40,13 +40,13 @@ import java.util.stream.Collectors;
 
 class DefaultInjectionTargetFactory<T> implements InjectionTargetFactory<T> {
     private final ElementManager elementManager;
-    private final InjectionFactory injectionFactory;
+    private final Injector injector;
     private final MetaClass<T> type;
 
-    DefaultInjectionTargetFactory(final ElementManager elementManager, final InjectionFactory injectionFactory,
+    DefaultInjectionTargetFactory(final ElementManager elementManager, final Injector injector,
                                   final MetaClass<T> type) {
         this.elementManager = elementManager;
-        this.injectionFactory = injectionFactory;
+        this.injector = injector;
         this.type = type;
     }
 
@@ -75,7 +75,7 @@ class DefaultInjectionTargetFactory<T> implements InjectionTargetFactory<T> {
         final List<MetaMethod<T, ?>> preDestroyMethods = methods.stream()
                 .filter(method -> method.isAnnotationPresent(PreDestroy.class))
                 .collect(Collectors.toList());
-        return new DefaultInjectionTarget<>(injectionFactory, elementManager, injectionPoints, constructor,
+        return new DefaultInjectionTarget<>(injector, elementManager, injectionPoints, constructor,
                 postConstructMethods, preDestroyMethods);
     }
 
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjector.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjector.java
new file mode 100644
index 0000000..0c9a2e6
--- /dev/null
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/DefaultInjector.java
@@ -0,0 +1,82 @@
+/*
+ * 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.defaults.bean;
+
+import org.apache.logging.log4j.plugins.api.Disposes;
+import org.apache.logging.log4j.plugins.spi.bean.BeanManager;
+import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
+import org.apache.logging.log4j.plugins.spi.bean.Injector;
+import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
+import org.apache.logging.log4j.plugins.spi.model.MetaConstructor;
+import org.apache.logging.log4j.plugins.spi.model.MetaField;
+import org.apache.logging.log4j.plugins.spi.model.MetaMethod;
+import org.apache.logging.log4j.plugins.spi.model.MetaParameter;
+
+import java.util.Collection;
+import java.util.List;
+
+public class DefaultInjector implements Injector {
+    private final BeanManager beanManager;
+
+    public DefaultInjector(final BeanManager beanManager) {
+        this.beanManager = beanManager;
+    }
+
+    private Object[] createArguments(final List<MetaParameter<?>> parameters,
+                                     final Collection<InjectionPoint<?>> injectionPoints,
+                                     final InitializationContext<?> context, final Object producedInstance) {
+        return parameters.stream().map(parameter ->
+                parameter.isAnnotationPresent(Disposes.class) ? producedInstance : injectionPoints.stream()
+                        .filter(point -> parameter.equals(point.getElement()))
+                        .findAny()
+                        .flatMap(point -> beanManager.getInjectableValue(point, context))
+                        .orElseThrow(() -> new UnsupportedOperationException("TODO: primitives and defaults")))
+                .toArray();
+    }
+
+    @Override
+    public <T> T construct(final MetaConstructor<T> constructor, final Collection<InjectionPoint<?>> points,
+                           final InitializationContext<T> context) {
+        return constructor.construct(createArguments(constructor.getParameters(), points, context, null));
+    }
+
+    @Override
+    public <D, T> T produce(final D producerInstance, final MetaMethod<D, T> producerMethod,
+                            final Collection<InjectionPoint<?>> points, final InitializationContext<T> context) {
+        return producerMethod.invoke(producerInstance, createArguments(producerMethod.getParameters(), points, context, null));
+    }
+
+    @Override
+    public <T> void dispose(final T disposerInstance, final MetaMethod<T, ?> disposerMethod,
+                            final Collection<InjectionPoint<?>> points, final Object instance,
+                            final InitializationContext<T> context) {
+        disposerMethod.invoke(disposerInstance, createArguments(disposerMethod.getParameters(), points, context, instance));
+    }
+
+    @Override
+    public <T> void invoke(final T instance, final MetaMethod<T, ?> method, final Collection<InjectionPoint<?>> points,
+                           final InitializationContext<T> context) {
+        method.invoke(instance, createArguments(method.getParameters(), points, context, null));
+    }
+
+    @Override
+    public <D, T> void set(final D instance, final MetaField<D, T> field, final InjectionPoint<T> point,
+                           final InitializationContext<D> context) {
+        beanManager.getInjectableValue(point, context).ifPresent(value -> field.set(instance, value));
+    }
+}
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/MethodProducer.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/MethodProducer.java
index 927ab5b..4261951 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/MethodProducer.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/bean/MethodProducer.java
@@ -20,9 +20,9 @@ package org.apache.logging.log4j.plugins.defaults.bean;
 import org.apache.logging.log4j.plugins.spi.InjectionException;
 import org.apache.logging.log4j.plugins.spi.bean.Bean;
 import org.apache.logging.log4j.plugins.spi.bean.BeanManager;
+import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
 import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
 import org.apache.logging.log4j.plugins.spi.model.MetaMethod;
-import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
 
 import java.lang.reflect.Type;
 import java.util.Collection;
@@ -52,8 +52,7 @@ class MethodProducer<D, T> extends AbstractProducer<D, T> {
     public T produce(final InitializationContext<T> context) {
         try (final InitializationContext<D> parentContext = createContext()) {
             final D declaringInstance = producerMethod.isStatic() ? null : getDeclaringInstance(parentContext);
-            return injectionFactory.forProducerMethod(producerMethod, declaringInstance, producerInjectionPoints)
-                    .apply(context);
+            return injector.produce(declaringInstance, producerMethod, producerInjectionPoints, context);
         }
     }
 
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/spi/bean/InjectionFactory.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/spi/bean/InjectionFactory.java
deleted file mode 100644
index b118d67..0000000
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/spi/bean/InjectionFactory.java
+++ /dev/null
@@ -1,47 +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.spi.bean;
-
-import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
-import org.apache.logging.log4j.plugins.spi.model.MetaConstructor;
-import org.apache.logging.log4j.plugins.spi.model.MetaField;
-import org.apache.logging.log4j.plugins.spi.model.MetaMethod;
-
-import java.util.Collection;
-import java.util.function.Consumer;
-import java.util.function.Function;
-
-public interface InjectionFactory {
-    <T> Function<InitializationContext<T>, T> forConstructor(final MetaConstructor<T> constructor,
-                                                             final Collection<InjectionPoint<?>> injectionPoints);
-
-    <D, T> Function<InitializationContext<T>, T> forProducerMethod(final MetaMethod<D, T> producerMethod,
-                                                                   final D declaringInstance,
-                                                                   final Collection<InjectionPoint<?>> injectionPoints);
-
-    <T> Consumer<InitializationContext<T>> forDisposerMethod(final MetaMethod<T, ?> disposerMethod,
-                                                             final T declaringInstance,
-                                                             final Collection<InjectionPoint<?>> injectionPoints,
-                                                             final Object producedInstance);
-
-    <D, T> Consumer<InitializationContext<D>> forField(final MetaField<D, T> field, final D declaringInstance,
-                                                       final InjectionPoint<T> injectionPoint);
-
-    <D, T> Consumer<InitializationContext<D>> forMethod(final MetaMethod<D, T> method, final D declaringInstance,
-                                                        final Collection<InjectionPoint<?>> injectionPoints);
-}
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/spi/bean/Injector.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/spi/bean/Injector.java
new file mode 100644
index 0000000..0d86baf
--- /dev/null
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/spi/bean/Injector.java
@@ -0,0 +1,43 @@
+/*
+ * 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.spi.bean;
+
+import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
+import org.apache.logging.log4j.plugins.spi.model.MetaConstructor;
+import org.apache.logging.log4j.plugins.spi.model.MetaField;
+import org.apache.logging.log4j.plugins.spi.model.MetaMethod;
+
+import java.util.Collection;
+
+public interface Injector {
+    <T> T construct(final MetaConstructor<T> constructor, final Collection<InjectionPoint<?>> points,
+                    final InitializationContext<T> context);
+
+    <D, T> T produce(final D producerInstance, final MetaMethod<D, T> producerMethod,
+                     final Collection<InjectionPoint<?>> points, final InitializationContext<T> context);
+
+    <T> void dispose(final T disposerInstance, final MetaMethod<T, ?> disposerMethod,
+                     final Collection<InjectionPoint<?>> points, final Object instance,
+                     final InitializationContext<T> context);
+
+    <T> void invoke(final T instance, final MetaMethod<T, ?> method, final Collection<InjectionPoint<?>> points,
+                    final InitializationContext<T> context);
+
+    <D, T> void set(final D instance, final MetaField<D, T> field, final InjectionPoint<T> point,
+                    final InitializationContext<D> context);
+}
diff --git a/log4j-plugins/src/test/java/org/apache/logging/log4j/plugins/test/BeanJUnit4Runner.java b/log4j-plugins/src/test/java/org/apache/logging/log4j/plugins/test/BeanJUnit4Runner.java
index c671ef1..31977d8 100644
--- a/log4j-plugins/src/test/java/org/apache/logging/log4j/plugins/test/BeanJUnit4Runner.java
+++ b/log4j-plugins/src/test/java/org/apache/logging/log4j/plugins/test/BeanJUnit4Runner.java
@@ -18,18 +18,18 @@
 package org.apache.logging.log4j.plugins.test;
 
 import org.apache.logging.log4j.plugins.defaults.bean.DefaultBeanManager;
-import org.apache.logging.log4j.plugins.defaults.bean.DefaultInjectionFactory;
+import org.apache.logging.log4j.plugins.defaults.bean.DefaultInjector;
 import org.apache.logging.log4j.plugins.defaults.model.DefaultElementManager;
 import org.apache.logging.log4j.plugins.spi.InjectionException;
 import org.apache.logging.log4j.plugins.spi.UnsatisfiedBeanException;
 import org.apache.logging.log4j.plugins.spi.bean.Bean;
 import org.apache.logging.log4j.plugins.spi.bean.BeanManager;
-import org.apache.logging.log4j.plugins.spi.bean.InjectionFactory;
+import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
+import org.apache.logging.log4j.plugins.spi.bean.Injector;
 import org.apache.logging.log4j.plugins.spi.model.ElementManager;
 import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
 import org.apache.logging.log4j.plugins.spi.model.MetaClass;
 import org.apache.logging.log4j.plugins.spi.model.MetaMethod;
-import org.apache.logging.log4j.plugins.spi.bean.InitializationContext;
 import org.apache.logging.log4j.plugins.util.TypeUtil;
 import org.junit.Test;
 import org.junit.runners.BlockJUnit4ClassRunner;
@@ -53,7 +53,7 @@ public class BeanJUnit4Runner extends BlockJUnit4ClassRunner {
     private MetaClass<?> testMetaClass;
 
     private BeanManager beanManager;
-    private InjectionFactory injectionFactory;
+    private Injector injector;
     private Bean<?> testClassBean;
 
     public BeanJUnit4Runner(final Class<?> clazz) throws InitializationError {
@@ -89,7 +89,7 @@ public class BeanJUnit4Runner extends BlockJUnit4ClassRunner {
 
     private <T> T createTestInstance() {
         beanManager = new DefaultBeanManager(elementManager);
-        injectionFactory = new DefaultInjectionFactory(beanManager);
+        injector = new DefaultInjector(beanManager);
         final WithBeans testClassBeans = getTestClass().getAnnotation(WithBeans.class);
         if (testClassBeans != null) {
             beanManager.loadBeans(testClassBeans.value());
@@ -125,7 +125,7 @@ public class BeanJUnit4Runner extends BlockJUnit4ClassRunner {
                     final MetaMethod<T, Void> metaMethod = metaClass.getMetaMethod(method.getMethod());
                     final Collection<InjectionPoint<?>> points =
                             elementManager.createExecutableInjectionPoints(metaMethod, testClassBean);
-                    injectionFactory.forMethod(metaMethod, testInstance, points).accept(context);
+                    injector.invoke(testInstance, metaMethod, points, context);
                 } finally {
                     beanManager.close();
                     beanManager = null;


[logging-log4j2] 01/02: Remove stereotypes

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a commit to branch mean-bean-machine
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 03be319b3fce8b1e14fc35b0db7800e0506625c6
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sun Mar 8 18:20:01 2020 -0500

    Remove stereotypes
    
    These are not as useful as I thought they might be
    
    Signed-off-by: Matt Sicker <bo...@gmail.com>
---
 .../logging/log4j/plugins/api/Stereotype.java      | 34 ---------------------
 .../defaults/model/DefaultElementManager.java      | 35 +---------------------
 2 files changed, 1 insertion(+), 68 deletions(-)

diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/api/Stereotype.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/api/Stereotype.java
deleted file mode 100644
index 280ebb3..0000000
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/api/Stereotype.java
+++ /dev/null
@@ -1,34 +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.api;
-
-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 an annotation type as a stereotype for combining annotations. This can combine zero or more
- * {@linkplain QualifierType qualifier types} and an optional {@linkplain ScopeType scope type}.
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.ANNOTATION_TYPE)
-@Documented
-public @interface Stereotype {
-}
diff --git a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/model/DefaultElementManager.java b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/model/DefaultElementManager.java
index e60fa31..ad83d4e 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/model/DefaultElementManager.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/defaults/model/DefaultElementManager.java
@@ -21,7 +21,6 @@ import org.apache.logging.log4j.plugins.api.AliasFor;
 import org.apache.logging.log4j.plugins.api.Dependent;
 import org.apache.logging.log4j.plugins.api.QualifierType;
 import org.apache.logging.log4j.plugins.api.ScopeType;
-import org.apache.logging.log4j.plugins.api.Stereotype;
 import org.apache.logging.log4j.plugins.spi.bean.Bean;
 import org.apache.logging.log4j.plugins.spi.model.ElementManager;
 import org.apache.logging.log4j.plugins.spi.model.InjectionPoint;
@@ -45,7 +44,7 @@ import java.util.Objects;
 public class DefaultElementManager implements ElementManager {
 
     private enum AnnotationType {
-        QUALIFIER, SCOPE, STEREOTYPE, UNKNOWN
+        QUALIFIER, SCOPE, UNKNOWN
     }
 
     private final Cache<Class<?>, MetaClass<?>> classCache = WeakCache.newWeakRefCache(DefaultMetaClass::newMetaClass);
@@ -62,9 +61,6 @@ public class DefaultElementManager implements ElementManager {
             if (type == ScopeType.class) {
                 return AnnotationType.SCOPE;
             }
-            if (type == Stereotype.class) {
-                return AnnotationType.STEREOTYPE;
-            }
         }
         return AnnotationType.UNKNOWN;
     });
@@ -90,34 +86,11 @@ public class DefaultElementManager implements ElementManager {
             final Class<? extends Annotation> annotationType = annotation.annotationType();
             if (isQualifierType(annotationType)) {
                 qualifiers.add(annotation);
-            } else if (isStereotype(annotationType)) {
-                qualifiers.addAll(filterQualifiers(getStereotypeDefinition(annotationType)));
             }
         }
         return qualifiers;
     }
 
-    /**
-     * Returns all the annotations associated with a {@linkplain Stereotype stereotype} annotation. This contains all
-     * annotation values sans the stereotype annotation values themselves.
-     */
-    private Collection<Annotation> getStereotypeDefinition(final Class<? extends Annotation> annotationType) {
-        final Stereotype stereotype = annotationType.getAnnotation(Stereotype.class);
-        if (stereotype == null) {
-            return Collections.emptySet();
-        }
-        final Annotation[] annotations = annotationType.getAnnotations();
-        final Collection<Annotation> stereotypeDefinition = new LinkedHashSet<>(annotations.length);
-        for (final Annotation annotation : annotations) {
-            if (isStereotype(annotation.annotationType())) {
-                stereotypeDefinition.addAll(getStereotypeDefinition(annotation.annotationType()));
-            } else {
-                stereotypeDefinition.add(annotation);
-            }
-        }
-        return Collections.unmodifiableCollection(stereotypeDefinition);
-    }
-
     private Class<? extends Annotation> getScopeType(final MetaElement<?> element) {
         final Collection<Class<? extends Annotation>> scopeTypes = filterScopeTypes(element.getAnnotations());
         return scopeTypes.isEmpty() ? Dependent.class : scopeTypes.iterator().next();
@@ -130,17 +103,11 @@ public class DefaultElementManager implements ElementManager {
             final Class<? extends Annotation> annotationType = annotation.annotationType();
             if (isScopeType(annotationType)) {
                 scopeTypes.add(annotationType);
-            } else if (isStereotype(annotationType)) {
-                scopeTypes.addAll(filterScopeTypes(getStereotypeDefinition(annotationType)));
             }
         }
         return Collections.unmodifiableCollection(scopeTypes);
     }
 
-    private boolean isStereotype(final Class<? extends Annotation> annotationType) {
-        return getAnnotationType(annotationType) == AnnotationType.STEREOTYPE;
-    }
-
     private boolean isScopeType(final Class<? extends Annotation> annotationType) {
         return getAnnotationType(annotationType) == AnnotationType.SCOPE;
     }