PlasmaVNGaming

Untitled

Apr 12th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. package com.gmail.plasmavn.enchantmentapi.services;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.lang.reflect.Method;
  6. import java.net.MalformedURLException;
  7. import java.net.URI;
  8. import java.net.URL;
  9. import java.net.URLClassLoader;
  10. import java.util.Enumeration;
  11. import java.util.Set;
  12. import java.util.jar.JarEntry;
  13. import java.util.jar.JarFile;
  14.  
  15. import com.google.common.collect.Sets;
  16.  
  17. public class ClassLoaderUtils {
  18.  
  19. private static final Set<URL> loadedURL = Sets.newHashSet();
  20.  
  21. @SuppressWarnings({ "resource", "unlikely-arg-type" })
  22. public static Set<Class<?>> loadClasses(File... files) throws MalformedURLException {
  23. Set<Class<?>> classes = Sets.newHashSet();
  24.  
  25. for (File file : files) {
  26.  
  27. if (file == null || !file.exists() || loadedURL.contains(file.toURI())) continue;
  28.  
  29. URI uri = file.toURI();
  30. JarFile jarFile;
  31.  
  32. try {
  33. jarFile = new JarFile(new File(uri.getPath()));
  34. } catch (IOException e) {
  35. System.out.println("Jar unknown: " + uri.toString());
  36. continue;
  37. }
  38.  
  39. Set<String> preclasses = Sets.newHashSet();
  40. Enumeration<JarEntry> e = jarFile.entries();
  41.  
  42. while (e.hasMoreElements()) {
  43. JarEntry je = e.nextElement();
  44. if (je.isDirectory() || !je.getName().endsWith(".class"))
  45. continue;
  46.  
  47. String className = je.getName().substring(0, je.getName().length() - 6).replace('/', '.');
  48. preclasses.add(className);
  49. }
  50.  
  51. if (preclasses.isEmpty()) {
  52. System.out.println("Empty name");
  53. continue;
  54. }
  55.  
  56.  
  57. addURL(uri.toURL());
  58.  
  59. for (String clazz : preclasses) {
  60. try {
  61. Class<?> c = ClassLoaderUtils.class.getClassLoader().loadClass(clazz);
  62. classes.add(c);
  63. } catch (ClassNotFoundException ex) {
  64. Utils.debug("Fail on load class", ex);
  65. continue;
  66. }
  67. }
  68.  
  69. }
  70.  
  71. return classes;
  72. }
  73.  
  74. private static void addURL(URL u) {
  75.  
  76. URLClassLoader sysloader = (URLClassLoader) ClassLoaderUtils.class.getClassLoader();
  77. Class<?> sysclass = URLClassLoader.class;
  78.  
  79. try {
  80. Method method = sysclass.getDeclaredMethod("addURL", URL.class);
  81. method.setAccessible(true);
  82. method.invoke(sysloader, u);
  83. loadedURL.add(u);
  84. } catch (Throwable t) {
  85. Utils.debug("Error, could not add URL to system classloader", t);
  86. }
  87.  
  88. }
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment