Add isClassAvailable method (idea being to support optional maven dependencies!)

This commit is contained in:
2025-05-23 12:00:36 -05:00
parent 75fc016a4b
commit 5ad6354e15
2 changed files with 72 additions and 0 deletions

View File

@ -25,6 +25,8 @@ package com.kingsrook.qqq.backend.core.utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
@ -37,6 +39,29 @@ public class ClassPathUtils
{
private static ImmutableSet<ClassPath.ClassInfo> topLevelClasses;
private static final Map<String, Boolean> cache = new ConcurrentHashMap<>();
/***************************************************************************
**
***************************************************************************/
public static boolean isClassAvailable(String className)
{
return cache.computeIfAbsent(className, c ->
{
try
{
Class.forName(c, false, ClassPathUtils.class.getClassLoader());
return true;
}
catch(ClassNotFoundException e)
{
return false;
}
});
}
/*******************************************************************************