Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. public static String[] getClasses(String pckgname) {
  2. try {
  3. java.util.ArrayList<String> classes = new java.util.ArrayList<String>();
  4. // Get a File object for the package
  5. File directory = null;
  6. try {
  7. ClassLoader cld = Thread.currentThread().getContextClassLoader();
  8. if (cld == null) {
  9. throw new ClassNotFoundException("Can't get class loader.");
  10. }
  11. String path = pckgname.replace('.', '/');
  12. URL resource = cld.getResource(path);
  13. if (resource == null) {
  14. throw new ClassNotFoundException("No resource for " + path);
  15. }
  16. directory = new File(resource.getFile());
  17. } catch (NullPointerException x) {
  18. throw new ClassNotFoundException(pckgname + " (" + directory
  19. + ") does not appear to be a valid package");
  20. }
  21. if (directory.exists()) {
  22. // Get the list of the files contained in the package
  23. String[] files = directory.list();
  24. for (int i = 0; i < files.length; i++) {
  25. // we are only interested in .class files
  26. if (files[i].endsWith(".class")&&!findText(files[i],"$")) {
  27. // removes the .class extension
  28. classes.add(files[i].substring(0,files[i].length()-6));
  29. // classes.add(Class.forName(pckgname + '.'
  30. // + files[i].substring(0, files[i].length() - 6)));
  31. }
  32. }
  33. } else {
  34. throw new ClassNotFoundException(pckgname
  35. + " does not appear to be a valid package");
  36. }
  37. String[] classesA = new String[classes.size()];
  38. classes.toArray(classesA);
  39. return classesA;
  40. }catch(Exception e) {
  41. e.printStackTrace();
  42. return null;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement