You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2016/08/12 12:12:26 UTC

[40/50] logging-log4j2 git commit: [LOG4J2-1507] Allow Builders to be completely generic.

[LOG4J2-1507] Allow Builders to be completely generic.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/2404f7cb
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/2404f7cb
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/2404f7cb

Branch: refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure
Commit: 2404f7cb6877f42ddc4818480c13ecec4d1d2739
Parents: e9cec77
Author: Gary Gregory <gg...@apache.org>
Authored: Tue Aug 9 17:36:10 2016 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Tue Aug 9 17:36:10 2016 -0700

----------------------------------------------------------------------
 .../core/config/plugins/util/PluginBuilder.java |  2 +-
 .../ValidatingPluginWithGenericBuilder.java     | 75 ++++++++++++++++++++
 .../ValidatingPluginWithGenericBuilderTest.java | 66 +++++++++++++++++
 3 files changed, 142 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2404f7cb/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
index 1827caf..7ba9981 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
@@ -147,7 +147,7 @@ public class PluginBuilder implements Builder<Object> {
         for (final Method method : clazz.getDeclaredMethods()) {
             if (method.isAnnotationPresent(PluginBuilderFactory.class) &&
                 Modifier.isStatic(method.getModifiers()) &&
-                TypeUtil.isAssignable(Builder.class, method.getGenericReturnType())) {
+                TypeUtil.isAssignable(Builder.class, method.getReturnType())) {
                 ReflectionUtil.makeAccessible(method);
                 return (Builder<?>) method.invoke(null);
             }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2404f7cb/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPluginWithGenericBuilder.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPluginWithGenericBuilder.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPluginWithGenericBuilder.java
new file mode 100644
index 0000000..34297e2
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPluginWithGenericBuilder.java
@@ -0,0 +1,75 @@
+/*
+ * 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.validation;
+
+import java.util.Objects;
+
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
+
+/**
+ *
+ */
+@Plugin(name = "ValidatingPluginWithGenericBuilder", category = "Test")
+public class ValidatingPluginWithGenericBuilder {
+
+    private final String name;
+
+    public ValidatingPluginWithGenericBuilder(final String name) {
+        this.name = Objects.requireNonNull(name, "name");
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    @PluginFactory
+    public static ValidatingPluginWithGenericBuilder newValidatingPlugin(
+        @Required(message = "The name given by the factory is null") final String name) {
+        return new ValidatingPluginWithGenericBuilder(name);
+    }
+
+    @PluginBuilderFactory
+    public static <B extends Builder<B>> B newBuilder() {
+        return new Builder<B>().asBuilder();
+    }
+
+    public static class Builder<B extends Builder<B>> implements org.apache.logging.log4j.core.util.Builder<ValidatingPluginWithGenericBuilder> {
+
+        @PluginBuilderAttribute
+        @Required(message = "The name given by the builder is null")
+        private String name;
+
+        public B withName(final String name) {
+            this.name = name;
+            return asBuilder();
+        }
+
+        @SuppressWarnings("unchecked")
+        private B asBuilder() {
+            return (B) this;
+        }
+
+        @Override
+        public ValidatingPluginWithGenericBuilder build() {
+            return new ValidatingPluginWithGenericBuilder(name);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2404f7cb/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidatingPluginWithGenericBuilderTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidatingPluginWithGenericBuilderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidatingPluginWithGenericBuilderTest.java
new file mode 100644
index 0000000..8e02f20
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidatingPluginWithGenericBuilderTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.validation.validators;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.NullConfiguration;
+import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
+import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
+import org.apache.logging.log4j.core.config.plugins.util.PluginType;
+import org.apache.logging.log4j.core.config.plugins.validation.ValidatingPluginWithGenericBuilder;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ValidatingPluginWithGenericBuilderTest {
+
+    private PluginType<ValidatingPluginWithGenericBuilder> plugin;
+    private Node node;
+
+    @SuppressWarnings("unchecked")
+    @Before
+    public void setUp() throws Exception {
+        final PluginManager manager = new PluginManager("Test");
+        manager.collectPlugins();
+        plugin = (PluginType<ValidatingPluginWithGenericBuilder>) manager.getPluginType("ValidatingPluginWithGenericBuilder");
+        assertNotNull("Rebuild this module to make sure annotaion processing kicks in.", plugin);
+        node = new Node(null, "Validator", plugin);
+    }
+
+    @Test
+    public void testNullDefaultValue() throws Exception {
+        final ValidatingPluginWithGenericBuilder validatingPlugin = (ValidatingPluginWithGenericBuilder) new PluginBuilder(plugin)
+            .withConfiguration(new NullConfiguration())
+            .withConfigurationNode(node)
+            .build();
+        assertNull(validatingPlugin);
+    }
+
+    @Test
+    public void testNonNullValue() throws Exception {
+        node.getAttributes().put("name", "foo");
+        final ValidatingPluginWithGenericBuilder validatingPlugin = (ValidatingPluginWithGenericBuilder) new PluginBuilder(plugin)
+            .withConfiguration(new NullConfiguration())
+            .withConfigurationNode(node)
+            .build();
+        assertNotNull(validatingPlugin);
+        assertEquals("foo", validatingPlugin.getName());
+    }
+}