PackageHelper.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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;
  53. try
  54. {
  55. s = KajetansMod.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  56. s = s.substring(5, s.length() - 25);
  57. }
  58. catch(Exception ex)
  59. {
  60. s = "../build/libs/km-1.0.jar";
  61. }
  62. File f = new File(s);
  63. if(!f.exists())
  64. {
  65. for(int i = 0; i < 20; i++)
  66. {
  67. System.out.println(f + " does not exist");
  68. }
  69. }
  70. return new URL("file:" + s);
  71. }
  72. catch(Exception ex)
  73. {
  74. ex.printStackTrace();
  75. }
  76. return null;
  77. }
  78. }