So the short of it is that you get a checked exception instead of an unchecked one ?
G <div>-------- Original message --------</div><div>From: Matt Sicker <[email protected]> </div><div>Date:04/16/2014 19:00 (GMT-05:00) </div><div>To: Log4J Developers List <[email protected]> </div><div>Subject: Re: svn commit: r1587455 - /logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java </div><div> </div>From the docs for Class.newInstance: Note that this method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor.newInstance method avoids this problem by wrapping any exception thrown by the constructor in a (checked) InvocationTargetException. On 16 April 2014 17:40, Remko Popma <[email protected]> wrote: Out of interest, what is the advantage of calling clazz.getConstructor().newInstance(); vs clazz.newInstance(); ? Until now all the code I've seen simply uses newInstance unless the constructor needs parameters. And it is shorter. :-) So just wondering if I missed something... On Tue, Apr 15, 2014 at 4:03 PM, <[email protected]> wrote: Author: mattsicker Date: Tue Apr 15 07:03:55 2014 New Revision: 1587455 URL: http://svn.apache.org/r1587455 Log: Add fallback to Class.newInstance. - In some scenarios, if a class doesn't have a constructor defined, Class.getConstructor() will fail, but Class.newInstance() won't fail. Not sure on the logic behind that one, Java. Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java?rev=1587455&r1=1587454&r2=1587455&view=diff ============================================================================== --- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java (original) +++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Loader.java Tue Apr 15 07:03:55 2014 @@ -21,6 +21,7 @@ import java.lang.ClassCastException; import java.lang.reflect.InvocationTargetException; import java.net.URL; +import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.PropertiesUtil; @@ -248,7 +249,7 @@ public final class Loader { // using the TCCL should work the same as the default ClassLoader (i.e., init or not) return Class.forName(className, true, getTCL()); } catch (final Throwable e) { - LOGGER.catching(e); + LOGGER.catching(Level.DEBUG, e); LOGGER.trace("TCCL didn't work. Trying Class.forName({}).", className); return loadClassWithDefaultClassLoader(className); } @@ -283,7 +284,7 @@ public final class Loader { try { return Class.forName(className, true, ClassLoader.getSystemClassLoader()); } catch (final Throwable t) { - LOGGER.catching(t); + LOGGER.catching(Level.DEBUG, t); LOGGER.trace("Couldn't use SystemClassLoader. Trying Class.forName({}).", className); return Class.forName(className); } @@ -306,7 +307,13 @@ public final class Loader { InstantiationException, NoSuchMethodException, InvocationTargetException { - return loadClass(className).getConstructor().newInstance(); + final Class<?> clazz = loadClass(className); + try { + return clazz.getConstructor().newInstance(); + } catch (final NoSuchMethodException e) { + //noinspection ClassNewInstance + return clazz.newInstance(); + } } /** @@ -342,7 +349,7 @@ public final class Loader { try { final Class<?> clazz = loadClass(className); return clazz != null; - } catch (final Exception ignored) { + } catch (final Throwable ignored) { return false; } } -- Matt Sicker <[email protected]>
