mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 05:01:07 +00:00
Replace random ChatGPT method with random StackOverflow method for getting classes from jar
This commit is contained in:
@ -22,12 +22,13 @@
|
|||||||
package com.kingsrook.qqq.backend.core.model.metadata;
|
package com.kingsrook.qqq.backend.core.model.metadata;
|
||||||
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import com.google.common.reflect.ClassPath;
|
||||||
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
||||||
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
|
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
|
||||||
|
|
||||||
@ -47,7 +48,7 @@ public class MetaDataProducerHelper
|
|||||||
**
|
**
|
||||||
** Note - they'll be sorted by the sortOrder they provide.
|
** Note - they'll be sorted by the sortOrder they provide.
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
public static void processAllMetaDataProducersInPackage(QInstance instance, String packageName)
|
public static void processAllMetaDataProducersInPackage(QInstance instance, String packageName) throws IOException
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// find all the meta data producer classes in the package //
|
// find all the meta data producer classes in the package //
|
||||||
@ -111,40 +112,19 @@ public class MetaDataProducerHelper
|
|||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** Thanks, Chat GPT.
|
** from https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection
|
||||||
|
** (since the original, from ChatGPT, didn't work in jars, despite GPT hallucinating that it would)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
private static List<Class<?>> getClassesInPackage(String packageName)
|
private static List<Class<?>> getClassesInPackage(String packageName) throws IOException
|
||||||
{
|
{
|
||||||
List<Class<?>> classes = new ArrayList<>();
|
List<Class<?>> classes = new ArrayList<>();
|
||||||
|
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||||
|
|
||||||
String path = packageName.replace('.', '/');
|
for(ClassPath.ClassInfo info : ClassPath.from(loader).getTopLevelClasses())
|
||||||
File directory = new File(Thread.currentThread().getContextClassLoader().getResource(path).getFile());
|
|
||||||
|
|
||||||
if(directory.exists())
|
|
||||||
{
|
{
|
||||||
File[] files = directory.listFiles();
|
if(info.getName().startsWith(packageName))
|
||||||
if(files != null)
|
|
||||||
{
|
{
|
||||||
for(File file : files)
|
classes.add(info.load());
|
||||||
{
|
|
||||||
if(file.isFile() && file.getName().endsWith(".class"))
|
|
||||||
{
|
|
||||||
String className = packageName + '.' + file.getName().substring(0, file.getName().length() - 6);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
classes.add(Class.forName(className));
|
|
||||||
}
|
|
||||||
catch(ClassNotFoundException e)
|
|
||||||
{
|
|
||||||
// Ignore, class not found
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(file.isDirectory())
|
|
||||||
{
|
|
||||||
List<Class<?>> subClasses = getClassesInPackage(packageName + "." + file.getName());
|
|
||||||
classes.addAll(subClasses);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
package com.kingsrook.qqq.backend.core.model.metadata;
|
package com.kingsrook.qqq.backend.core.model.metadata;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import com.kingsrook.qqq.backend.core.model.metadata.producers.TestMetaDataProducer;
|
import com.kingsrook.qqq.backend.core.model.metadata.producers.TestMetaDataProducer;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
@ -37,7 +38,7 @@ class MetaDataProducerHelperTest
|
|||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
@Test
|
@Test
|
||||||
void test()
|
void test() throws IOException
|
||||||
{
|
{
|
||||||
QInstance qInstance = new QInstance();
|
QInstance qInstance = new QInstance();
|
||||||
MetaDataProducerHelper.processAllMetaDataProducersInPackage(qInstance, "com.kingsrook.qqq.backend.core.model.metadata.producers");
|
MetaDataProducerHelper.processAllMetaDataProducersInPackage(qInstance, "com.kingsrook.qqq.backend.core.model.metadata.producers");
|
||||||
|
Reference in New Issue
Block a user