19
19
import java .lang .reflect .Constructor ;
20
20
import java .lang .reflect .InvocationTargetException ;
21
21
import java .lang .reflect .Method ;
22
+ import java .util .function .Consumer ;
22
23
23
24
import org .slf4j .Logger ;
24
25
import org .slf4j .LoggerFactory ;
@@ -309,6 +310,16 @@ public static <DATATYPE> DATATYPE newInstance (@Nonnull final ClassLoader aClass
309
310
@ Nullable
310
311
public static <DATATYPE > DATATYPE newInstance (@ Nullable final String sClassName ,
311
312
@ Nullable final Class <? extends DATATYPE > aDesiredType )
313
+ {
314
+ return newInstance (sClassName ,
315
+ aDesiredType ,
316
+ ex -> LOGGER .error ("Failed to instantiate '" + sClassName + "'" , ex ));
317
+ }
318
+
319
+ @ Nullable
320
+ public static <DATATYPE > DATATYPE newInstance (@ Nullable final String sClassName ,
321
+ @ Nullable final Class <? extends DATATYPE > aDesiredType ,
322
+ @ Nullable final Consumer <Exception > aExHdl )
312
323
{
313
324
if (sClassName != null && aDesiredType != null )
314
325
try
@@ -321,7 +332,8 @@ public static <DATATYPE> DATATYPE newInstance (@Nullable final String sClassName
321
332
* Catch all exceptions because any exception thrown from the constructor (indirectly
322
333
* invoked by newInstance) may also end up in this catch block
323
334
*/
324
- LOGGER .error ("Failed to instantiate '" + sClassName + "'" , ex );
335
+ if (aExHdl != null )
336
+ aExHdl .accept (ex );
325
337
}
326
338
return null ;
327
339
}
@@ -330,6 +342,19 @@ public static <DATATYPE> DATATYPE newInstance (@Nullable final String sClassName
330
342
public static <DATATYPE > DATATYPE newInstance (@ Nullable final String sClassName ,
331
343
@ Nullable final Class <? extends DATATYPE > aDesiredType ,
332
344
@ Nullable final ClassLoader aClassLoaderToUse )
345
+ {
346
+ return newInstance (sClassName ,
347
+ aDesiredType ,
348
+ aClassLoaderToUse ,
349
+ ex -> LOGGER .error ("Failed to instantiate '" + sClassName + "' with CL " + aClassLoaderToUse ,
350
+ ex ));
351
+ }
352
+
353
+ @ Nullable
354
+ public static <DATATYPE > DATATYPE newInstance (@ Nullable final String sClassName ,
355
+ @ Nullable final Class <? extends DATATYPE > aDesiredType ,
356
+ @ Nullable final ClassLoader aClassLoaderToUse ,
357
+ @ Nullable final Consumer <Exception > aExHdl )
333
358
{
334
359
if (sClassName != null && aDesiredType != null && aClassLoaderToUse != null )
335
360
try
@@ -344,7 +369,8 @@ public static <DATATYPE> DATATYPE newInstance (@Nullable final String sClassName
344
369
* Catch all exceptions because any exception thrown from the constructor (indirectly
345
370
* invoked by newInstance) may also end up in this catch block
346
371
*/
347
- LOGGER .error ("Failed to instantiate '" + sClassName + "' with CL " + aClassLoaderToUse , ex );
372
+ if (aExHdl != null )
373
+ aExHdl .accept (ex );
348
374
}
349
375
return null ;
350
376
}
0 commit comments