From adca215ea78ca3a74355e72d4bc4b28ccf6444e9 Mon Sep 17 00:00:00 2001 From: extendedclip Date: Fri, 29 Jun 2018 14:40:48 -0400 Subject: [PATCH] Added method to get all class files of a certain type from a specific jar --- .../me/clip/placeholderapi/util/FileUtil.java | 116 +++++++++--------- 1 file changed, 60 insertions(+), 56 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/util/FileUtil.java b/src/main/java/me/clip/placeholderapi/util/FileUtil.java index 417cb31..fae1731 100644 --- a/src/main/java/me/clip/placeholderapi/util/FileUtil.java +++ b/src/main/java/me/clip/placeholderapi/util/FileUtil.java @@ -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,64 +28,70 @@ 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> getClasses(String folder, Class type) { - List> 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"); - }; - File[] jars = f.listFiles(fileNameFilter); - if (jars == null) { - return list; - } - for (File file : jars) { - list = gather(file.toURI().toURL(), list, type); - } - return list; - } catch (Throwable t) {} - return null; - } + public static List> getClasses(String folder, Class type) { + return getClasses(folder, null, type); + } - private static List> gather(URL jar, List> list, Class clazz) { - if (list == null) { - list = new ArrayList<>(); - } - try { - URLClassLoader cl = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader()); - JarInputStream jis = new JarInputStream(jar.openStream()); - while (true) { - JarEntry j = jis.getNextJarEntry(); - if (j == null) { - break; - } - String name = j.getName(); - if (name == null || name.isEmpty()) { - continue; - } - if (name.endsWith(".class")) { - name = name.replace("/", "."); - String cname = name.substring(0, name.lastIndexOf(".class")); - Class c = cl.loadClass(cname); - if (clazz.isAssignableFrom(c)) { - list.add(c); - } - } - } - cl.close(); - jis.close(); - } catch (Throwable t) { - } + public static List> getClasses(String folder, String fileName, Class type) { + List> list = new ArrayList<>(); + try { + File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), folder); + if (!f.exists()) { return list; + } + FilenameFilter fileNameFilter = (dir, name) -> { + 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) { + return list; + } + for (File file : jars) { + list = gather(file.toURI().toURL(), list, type); + } + return list; + } catch (Throwable t) { } + return null; + } + + private static List> gather(URL jar, List> list, Class clazz) { + if (list == null) { + list = new ArrayList<>(); + } + try { + URLClassLoader cl = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader()); + JarInputStream jis = new JarInputStream(jar.openStream()); + while (true) { + JarEntry j = jis.getNextJarEntry(); + if (j == null) { + break; + } + String name = j.getName(); + if (name == null || name.isEmpty()) { + continue; + } + if (name.endsWith(".class")) { + name = name.replace("/", "."); + String cname = name.substring(0, name.lastIndexOf(".class")); + Class c = cl.loadClass(cname); + if (clazz.isAssignableFrom(c)) { + list.add(c); + } + } + } + cl.close(); + jis.close(); + } catch (Throwable t) { + } + return list; + } }