PackageHelper.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. public static ArrayList<Class> getClasses(String packageName) {
  10. URL jar = getPath();
  11. if(jar != null) {
  12. try {
  13. ZipInputStream zip = new ZipInputStream(jar.openStream());
  14. ArrayList<Class> classes = new ArrayList<>();
  15. String path = packageName.replace(".", "/");
  16. while(true) {
  17. ZipEntry e = zip.getNextEntry();
  18. if(e == null) {
  19. break;
  20. }
  21. String name = e.getName();
  22. if(name.startsWith(path)) {
  23. name = name.replace("/", ".").substring(0, e.getName().length() - 6);
  24. try {
  25. classes.add(Class.forName(name));
  26. } catch(ClassNotFoundException ex) {
  27. }
  28. }
  29. }
  30. return classes;
  31. } catch(IOException ex) {
  32. }
  33. }
  34. return new ArrayList<>();
  35. }
  36. private static URL getPath() {
  37. try {
  38. String s;
  39. try {
  40. s = KajetansMod.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  41. s = s.substring(5, s.length() - 25);
  42. } catch(Exception ex) {
  43. s = "../build/libs/km-1.0.jar";
  44. }
  45. File f = new File(s);
  46. if(!f.exists()) {
  47. for(int i = 0; i < 20; i++) {
  48. System.out.println(f + " does not exist");
  49. }
  50. }
  51. return new URL("file:" + s);
  52. } catch(Exception ex) {
  53. ex.printStackTrace();
  54. }
  55. return null;
  56. }
  57. }