Skip to content

Commit c287c63

Browse files
committed
Added overloads
1 parent 91ac071 commit c287c63

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

ph-base/src/main/java/com/helger/base/reflection/GenericReflection.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Constructor;
2020
import java.lang.reflect.InvocationTargetException;
2121
import java.lang.reflect.Method;
22+
import java.util.function.Consumer;
2223

2324
import org.slf4j.Logger;
2425
import org.slf4j.LoggerFactory;
@@ -309,6 +310,16 @@ public static <DATATYPE> DATATYPE newInstance (@Nonnull final ClassLoader aClass
309310
@Nullable
310311
public static <DATATYPE> DATATYPE newInstance (@Nullable final String sClassName,
311312
@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)
312323
{
313324
if (sClassName != null && aDesiredType != null)
314325
try
@@ -321,7 +332,8 @@ public static <DATATYPE> DATATYPE newInstance (@Nullable final String sClassName
321332
* Catch all exceptions because any exception thrown from the constructor (indirectly
322333
* invoked by newInstance) may also end up in this catch block
323334
*/
324-
LOGGER.error ("Failed to instantiate '" + sClassName + "'", ex);
335+
if (aExHdl != null)
336+
aExHdl.accept (ex);
325337
}
326338
return null;
327339
}
@@ -330,6 +342,19 @@ public static <DATATYPE> DATATYPE newInstance (@Nullable final String sClassName
330342
public static <DATATYPE> DATATYPE newInstance (@Nullable final String sClassName,
331343
@Nullable final Class <? extends DATATYPE> aDesiredType,
332344
@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)
333358
{
334359
if (sClassName != null && aDesiredType != null && aClassLoaderToUse != null)
335360
try
@@ -344,7 +369,8 @@ public static <DATATYPE> DATATYPE newInstance (@Nullable final String sClassName
344369
* Catch all exceptions because any exception thrown from the constructor (indirectly
345370
* invoked by newInstance) may also end up in this catch block
346371
*/
347-
LOGGER.error ("Failed to instantiate '" + sClassName + "' with CL " + aClassLoaderToUse, ex);
372+
if (aExHdl != null)
373+
aExHdl.accept (ex);
348374
}
349375
return null;
350376
}

0 commit comments

Comments
 (0)