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 2553e13..e0797a7 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 @@ -141,10 +141,23 @@ return Class.forName(className); } } /** + * Loads a class by name using a given ClassLoader. Does NOT initialize Class. + * + * @param className The class name. + * @param loader The class loader. + * @return The class, or null if loader is null. + * @throws ClassNotFoundException if the class could not be found. + */ + public static Class loadClass(final String className, final ClassLoader loader) + throws ClassNotFoundException { + return loader!=null ? loader.loadClass(className) : null; + } + + /** * 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 diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/util/LoaderUtilTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/util/LoaderUtilTest.java new file mode 100644 index 0000000..dfa4106 --- /dev/null +++ b/log4j-api/src/test/java/org/apache/logging/log4j/util/LoaderUtilTest.java @@ -0,0 +1,41 @@ +/* + * 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.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +/** + * Tests the Loader class. + */ +public class LoaderUtilTest { + + @Test + public void testLoadClassWithNullClassloaderReturnNull() throws Exception + { + assertNull("Expect null return value for null ClassLoader.", LoaderUtil.loadClass(LoaderUtil.class.getCanonicalName(), null)); + } + + @Test + public void testLoadClassReturnClassForExistingClass() throws Exception + { + assertEquals("Expect Class return value for null ClassLoader.", LoaderUtil.class,LoaderUtil.loadClass(LoaderUtil.class.getCanonicalName(), LoaderUtil.class.getClassLoader())); + } +} diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java index e12e14a..5bf3c70 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java @@ -554,11 +554,11 @@ return clazz; } private Class loadClass(final String className) { try { - return this.getClass().getClassLoader().loadClass(className); + return LoaderUtil.loadClass(className,this.getClass().getClassLoader()); } catch (final ClassNotFoundException | NoClassDefFoundError | SecurityException e) { return null; } }