You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReflectionUtil.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.uqac.rthoni.java_rmi.common;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.JarURLConnection;
  6. import java.net.URL;
  7. import java.net.URLDecoder;
  8. import java.util.*;
  9. import java.util.jar.JarEntry;
  10. import java.util.jar.JarFile;
  11. import java.util.stream.Collectors;
  12. /**
  13. * Created by robin on 9/16/16.
  14. */
  15. public class ReflectionUtil {
  16. public static Vector<Class> getClassesForPackage(String pckgname)
  17. throws ClassNotFoundException {
  18. Vector<Class> classes = new Vector<>();
  19. ArrayList<File> directories = new ArrayList<>();
  20. try {
  21. ClassLoader cld = Thread.currentThread().getContextClassLoader();
  22. Enumeration<URL> resources = cld.getResources(pckgname.replace('.', '/'));
  23. while (resources.hasMoreElements()) {
  24. URL res = resources.nextElement();
  25. if (res.getProtocol().equalsIgnoreCase("jar")){
  26. JarURLConnection conn = (JarURLConnection) res.openConnection();
  27. JarFile jar = conn.getJarFile();
  28. for (JarEntry e: Collections.list(jar.entries())){
  29. if(e.getName().startsWith(pckgname.replace('.', '/'))
  30. && e.getName().endsWith(".class") && !e.getName().contains("$")){
  31. String className = e.getName().replace("/",".").substring(0,e.getName().length() - 6);
  32. classes.add(Class.forName(className));
  33. }
  34. }
  35. }else
  36. directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8")));
  37. }
  38. } catch (Exception e) {
  39. throw new ClassNotFoundException(String.format("Failed to get jar info: %s", e.getMessage()));
  40. }
  41. // For every directory identified capture all the .class files
  42. for (File directory : directories) {
  43. if (directory.exists()) {
  44. // Get the list of the files contained in the package
  45. String[] files = directory.list();
  46. for (String file : files) {
  47. // we are only interested in .class files
  48. if (file.endsWith(".class")) {
  49. // removes the .class extension
  50. classes.add(Class.forName(pckgname + '.' + file.substring(0, file.length() - 6)));
  51. }
  52. }
  53. } else {
  54. throw new ClassNotFoundException(String.format("Failed to get class file for %s", directory.getName()));
  55. }
  56. }
  57. return classes;
  58. }
  59. public static Vector<Class> getClassesOfSuperClass(String thePackage, Class superClass)
  60. throws ClassNotFoundException {
  61. Vector<Class> classList = new Vector<>();
  62. classList.addAll(getClassesForPackage(thePackage).stream()
  63. .filter(discovered -> Collections.singletonList(discovered.getSuperclass())
  64. .contains(superClass)).collect(Collectors.toList()));
  65. return classList;
  66. }
  67. }