Consider a refactoring (commented out).

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

Branch: 
refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure
Commit: 924445dbbb1057441247afe7b0f2ab8146caaa5d
Parents: 1467acc
Author: Gary Gregory <ggreg...@apache.org>
Authored: Thu Aug 4 22:50:10 2016 -0700
Committer: Gary Gregory <ggreg...@apache.org>
Committed: Thu Aug 4 22:50:10 2016 -0700

----------------------------------------------------------------------
 .../apache/logging/log4j/util/LoaderUtil.java   | 28 +++++++++++++++-----
 .../logging/log4j/junit/LoggerContextRule.java  |  2 ++
 2 files changed, 23 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/924445db/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
----------------------------------------------------------------------
diff --git 
a/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java 
b/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
index b5f626d..6f08545 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
@@ -126,6 +126,26 @@ public final class LoaderUtil {
     /**
      * Loads and instantiates a Class using the default constructor.
      *
+     * @param clazz The class.
+     * @return new instance of the class.
+     * @throws IllegalAccessException if the class can't be instantiated 
through a public constructor
+     * @throws InstantiationException if there was an exception whilst 
instantiating the class
+     * @throws InvocationTargetException if there was an exception whilst 
constructing the class
+     * @since 2.7
+     */
+    public static <T> T newInstanceOf(final Class<T> clazz)
+            throws InstantiationException, IllegalAccessException, 
InvocationTargetException {
+        try {
+            return clazz.getConstructor().newInstance();
+        } catch (final NoSuchMethodException ignored) {
+            // FIXME: looking at the code for Class.newInstance(), this seems 
to do the same thing as above
+            return clazz.newInstance();
+        }
+    }
+
+    /**
+     * Loads and instantiates a Class using the default constructor.
+     *
      * @param className The class name.
      * @return new instance of the class.
      * @throws ClassNotFoundException if the class isn't available to the 
usual ClassLoaders
@@ -138,13 +158,7 @@ public final class LoaderUtil {
     @SuppressWarnings("unchecked")
     public static <T> T newInstanceOf(final String className) throws 
ClassNotFoundException, IllegalAccessException,
             InstantiationException, NoSuchMethodException, 
InvocationTargetException {
-        final Class<?> clazz = loadClass(className);
-        try {
-            return (T) clazz.getConstructor().newInstance();
-        } catch (final NoSuchMethodException ignored) {
-            // FIXME: looking at the code for Class.newInstance(), this seems 
to do the same thing as above
-            return (T) clazz.newInstance();
-        }
+        return newInstanceOf((Class<T>) loadClass(className));
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/924445db/log4j-core/src/test/java/org/apache/logging/log4j/junit/LoggerContextRule.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/junit/LoggerContextRule.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/junit/LoggerContextRule.java
index 3f33616..3468e3f 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/junit/LoggerContextRule.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/junit/LoggerContextRule.java
@@ -87,6 +87,8 @@ public class LoggerContextRule implements TestRule {
                 if (contextSelectorClass != null) {
                     System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, 
contextSelectorClass.getName());
                 }
+                // TODO Consider instead of the above:
+                // LogManager.setFactory(new 
Log4jContextFactory(LoaderUtil.newInstanceOf(contextSelectorClass)));
                 System.setProperty(SYS_PROP_KEY_CLASS_NAME, 
description.getClassName());
                 System.setProperty(SYS_PROP_KEY_DISPLAY_NAME, 
description.getDisplayName());
                 context = Configurator.initialize(description.getDisplayName(),

Reply via email to