mirror of
				https://github.com/PlaceholderAPI/PlaceholderAPI
				synced 2025-10-30 18:03:43 +01:00 
			
		
		
		
	Added method to get all class files of a certain type from a specific jar
This commit is contained in:
		| @@ -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<Class<?>> getClasses(String folder, 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"); | ||||
|             }; | ||||
|             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<Class<?>> getClasses(String folder, Class<?> type) { | ||||
|     return getClasses(folder, null, type); | ||||
|   } | ||||
|  | ||||
| 	private static List<Class<?>> gather(URL jar, List<Class<?>> 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<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()) { | ||||
|         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<Class<?>> gather(URL jar, List<Class<?>> 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; | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user