Added method to get all class files of a certain type from a specific jar

This commit is contained in:
extendedclip 2018-06-29 14:40:48 -04:00
parent d0445e231d
commit adca215ea7

View File

@ -20,8 +20,6 @@
*/
package me.clip.placeholderapi.util;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;
@ -30,22 +28,27 @@ import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
public class FileUtil {
public static List<Class<?>> getClasses(String folder, Class<?> type) {
return getClasses(folder, null, type);
}
public static List<Class<?>> getClasses(String folder, String fileName, Class<?> type) {
List<Class<?>> list = new ArrayList<>();
try {
File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), folder);
if (!f.exists()) {
if (!f.mkdir()) {
PlaceholderAPIPlugin.getInstance().getLogger().severe("Failed to create " + folder + " folder!");
}
return list;
}
FilenameFilter fileNameFilter = (dir, name) -> {
int i = name.lastIndexOf('.');
return i > 0 && name.substring(i).equals(".jar");
if (fileName != null) {
return name.endsWith(".jar") && name.replace(".jar", "")
.equalsIgnoreCase(fileName.replace(".jar", ""));
}
return name.endsWith(".jar");
};
File[] jars = f.listFiles(fileNameFilter);
if (jars == null) {
@ -55,7 +58,8 @@ public class FileUtil {
list = gather(file.toURI().toURL(), list, type);
}
return list;
} catch (Throwable t) {}
} catch (Throwable t) {
}
return null;
}