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.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath; import com.google.common.reflect.ClassPath;
@ -37,6 +39,29 @@ public class ClassPathUtils
{ {
private static ImmutableSet<ClassPath.ClassInfo> topLevelClasses; 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;
}
});
}
/******************************************************************************* /*******************************************************************************

View File

@ -0,0 +1,47 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2025. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.kingsrook.qqq.backend.core.utils;
import com.kingsrook.qqq.backend.core.BaseTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/*******************************************************************************
** Unit test for ClassPathUtils
*******************************************************************************/
class ClassPathUtilsTest extends BaseTest
{
/*******************************************************************************
**
*******************************************************************************/
@Test
void test()
{
assertTrue(ClassPathUtils.isClassAvailable(ClassPathUtils.class.getName()));
assertFalse(ClassPathUtils.isClassAvailable("NOT-" + ClassPathUtils.class.getName()));
}
}