Search in sources :

Example 1 with ClassPath

use of com.massivecraft.massivecore.xlib.guava.reflect.ClassPath in project MassiveCore by MassiveCraft.

the class ReflectionUtil method getPackageClasses.

// -------------------------------------------- //
// GET PACKAGE CLASSES
// -------------------------------------------- //
@SuppressWarnings("unchecked")
public static List<Class<?>> getPackageClasses(String packageName, ClassLoader classLoader, boolean recursive, Predicate<Class<?>>... predicates) {
    // Create ret
    List<Class<?>> ret = new MassiveList<>();
    try {
        // Get info
        ClassPath classPath = ClassPath.from(classLoader);
        Predicate<Class<?>> predicateCombined = PredicateAnd.get(predicates);
        Collection<ClassInfo> classInfos = recursive ? classPath.getTopLevelClassesRecursive(packageName) : classPath.getTopLevelClasses(packageName);
        for (ClassInfo classInfo : classInfos) {
            // Get name of class
            String className = classInfo.getName();
            // Apparently it found a "EngineMassiveCoreCollTick 3" which we don't want
            if (className.contains(" "))
                continue;
            // Try and load it
            Class<?> clazz;
            try {
                clazz = classInfo.load();
            } catch (NoClassDefFoundError ex) {
                // Just skip it
                continue;
            }
            // And it must not be ignored
            if (!predicateCombined.apply(clazz))
                continue;
            ret.add(clazz);
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    Collections.sort(ret, new Comparator<Class<?>>() {

        @Override
        public int compare(Class<?> class1, Class<?> class2) {
            return ComparatorNaturalOrder.get().compare(class1.getName(), class2.getName());
        }
    });
    return ret;
}
Also used : ClassPath(com.massivecraft.massivecore.xlib.guava.reflect.ClassPath) MassiveList(com.massivecraft.massivecore.collections.MassiveList) IOException(java.io.IOException) ClassInfo(com.massivecraft.massivecore.xlib.guava.reflect.ClassPath.ClassInfo)

Aggregations

MassiveList (com.massivecraft.massivecore.collections.MassiveList)1 ClassPath (com.massivecraft.massivecore.xlib.guava.reflect.ClassPath)1 ClassInfo (com.massivecraft.massivecore.xlib.guava.reflect.ClassPath.ClassInfo)1 IOException (java.io.IOException)1