PackageHelper.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package me.km;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipInputStream;
  8. public class PackageHelper
  9. {
  10. public static ArrayList<Class> getClasses(String packageName)
  11. {
  12. URL jar = getPath();
  13. if (jar != null)
  14. {
  15. try
  16. {
  17. ZipInputStream zip = new ZipInputStream(jar.openStream());
  18. ArrayList<Class> classes = new ArrayList<>();
  19. String path = packageName.replace(".", "/");
  20. while(true)
  21. {
  22. ZipEntry e = zip.getNextEntry();
  23. if (e == null)
  24. {
  25. break;
  26. }
  27. String name = e.getName();
  28. if(name.startsWith(path))
  29. {
  30. name = name.replace("/", ".").substring(0, e.getName().length() - 6);
  31. try
  32. {
  33. classes.add(Class.forName(name));
  34. }
  35. catch (ClassNotFoundException ex)
  36. {
  37. }
  38. }
  39. }
  40. return classes;
  41. }
  42. catch(IOException ex)
  43. {
  44. }
  45. }
  46. return new ArrayList<>();
  47. }
  48. private static URL getPath()
  49. {
  50. try
  51. {
  52. String s = KajetansMod.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  53. s = s.substring(5, s.length() - 25);
  54. File f = new File(s);
  55. if(!f.exists())
  56. {
  57. for(int i = 0; i < 20; i++)
  58. {
  59. System.out.println(f + " does not exist");
  60. }
  61. }
  62. return new URL("file:" + s);
  63. }
  64. catch(Exception ex)
  65. {
  66. System.out.println(ex);
  67. }
  68. return null;
  69. }
  70. }